- 论坛徽章:
- 0
|
函数的返回值为字符串时该如何处理?
我手头没有tc,所以大致改了一下,还是换个borland c++5.5或者vc,还有dev-c++吧,16位的东西,现在比较少用
- #include<stdio.h>;
- /* #include<conio.h>; */
- #include<string.h>;
- char str[32];
- char binary(int b){
- int i,j;
- char *pstr=&str[0],temp;
- for(i=0;i<32;i++)
- str[i]=0;
- while(b>;0){
- if(b%2==0)
- *pstr='0';
- if(b%2==1)
- *pstr='1';
- b/=2;
- pstr++;
- }
- for(i=0,j=strlen(str)-1;i<j;i++,j--){
- temp=str[i];
- str[i]=str[j];
- str[j]=temp;
- }
- return 0;
- }
- int startup(void){
- int a,flag=1;
- char ch;
- while(flag){
- /* clrscr(); */
- printf("\nEnter a choice: ");
- printf("\n(1) Exit\t(2) Continue\n");
- ch=getchar();
- if(ch=='1'){
- /* clrscr(); */
- printf("\nBye!\n");
- getchar();
- flag=0;
- }
- else if(ch=='2'){
- /* clrscr(); */
- printf("\nInput a positive integer: ");
- scanf("%d",&a);
- binary(a); //就是这里,填充全局变量。执行完毕后,str里面是转换后的字符串
- printf("\nThe binary code for this positive integer is %s", str);
- getchar();
- }
- else{
- printf("\nError Input! Press any key to input again.\n");
- getchar();
- startup();
- }
- }
- return 0;
- }
- int main(void){
- startup();
- return 0;
- }
复制代码 |
|