您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 进出口许可 > C语言改错题以及参考答案
1改错参考答案:(3)将字符串yy在屏幕上输出#includestdio.hvoidmain(){/*********Found************/charyy[100]=ok??\n;/*********Found************/f0r(;*yy;yy++){putchar(*yy);}}#includestdio.hvoidmain(){/*********Found************/char*yy=ok??\n;/*********Found************/for(;*yy;yy++){putchar(*yy);}}(4)计算半径为2+3的圆的面积#includestdio.h#definePI3.14/*********Found************/#defineS(r)PI*r*rvoidmain(){/*********Found************/intmianJi;mianJi=S(2+3);printf(mianji=%5.2f\n,mianJi);}#includestdio.h#definePI3.14/*********Found************/#defineS(r)PI*(r)*(r)voidmain(){/*********Found************/2floatmianJi;mianJi=S(2+3);printf(mianji=%5.2f\n,mianJi);}(8)打开文件d:\te.c用于读并判断打开是否成功#includestdio.hvoidmain(){FILE*fp;/*********Found************/charfileName[]=d:\te.c;/*********Found************/fp=fopen(fileName,w);/*********Found************/if(fp==EOF){puts(FileOpenError!);exit(1);}putchar(fgetc(fp));fclose(fp);}#includestdio.hvoidmain(){FILE*fp;/*********Found************/charfileName[]=d:\\te.c;/*********Found************/fp=fopen(fileName,r);/*********Found************/if(fp==NULL){puts(FileOpenError!);exit(1);}putchar(fgetc(fp));fclose(fp);}(9)申请100个字节的内存空间,显示其首地址,然后释放申请到的内存空间#includestdio.h#includealloc.hvoidmain(){/*********Found************/charp[100];3/*********Found************/if(p=(char*)malloc(100)==NULL){printf(mallocmemoryfail!\n);return;}printf(%p\n,p);/*********Found************/fclose(p);}#includestdio.h#includealloc.hvoidmain(){/*********Found************/char*p;/*********Found************/if((p=(char*)malloc(100))==NULL){printf(mallocmemoryfail!\n);exit(1);}printf(%p\n,p);/*********Found************/free(p);}(10)将字符串p显示在屏幕上#includestdio.h/*********Found************/#defineBEGIN/*********Found************/#defineENDvoidmain(){char*p=13125176247;constinti=0;for(printf(\n);p[i];)BEGINputchar(p[i]);/*********Found************/i++;END}#includestdio.h/*********Found************/#defineBEGIN{/*********Found************/#defineEND}4voidmain(){char*p=13125176247;constinti=0;for(printf(\n);p[i];)BEGINputchar(p[i]);/*********Found************/p++;END}(14)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#includestdio.h/*********Found************/voidswap(intx,inty){inttmp;/*********Found************/___________*x=*y;/*********Found************/y=x;}voidmain(){inta=3,b=4;/*********Found************/swap(a,b);printf(a=%db=%d\n,a,b);}#includestdio.h/*********Found************/voidswap(int*x,int*y){inttmp;/*********Found************/tmp=*x;*x=*y;/*********Found************/*y=tmp;}voidmain(){inta=3,b=4;/*********Found************/swap(&a,&b);printf(a=%db=%d\n,a,b);}5(15)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#includestdio.hvoidswap(int*x,int*y){int*tmp,xy;/*********Found************/*tmp=x;*x=*y;/*********Found************/y=*tmp;}voidmain(){inta=3,b=4;/*********Found************/swap(*a,*b);/*********Found************/printf(a=%db=%d\n,&a,&b);}#includestdio.hvoidswap(int*x,int*y){int*tmp,xy;/*********Found************/xy=*x;*x=*y;/*********Found************/*y=xy;}voidmain(){inta=3,b=4;/*********Found************/swap(&a,&b);/*********Found************/printf(a=%db=%d\n,a,b);}(16)worker的信息使用结构体存储,从键盘读入其各项信息并显示#includestdio.hvoidmain(){structWKER{longID;longintnum;charname[20];6charsex;/*********Found************/};worker.ID=1L;/*********Found************/scanf(%d%s%s,&worker.num,worker.name,&worker.sex);/*********Found************/printf(worker'sinfo:num=%dname=%ssex=%s\n,worker.num,worker.name,worker.sex);}#includestdio.hvoidmain(){structWKER{longID;longintnum;charname[20];charsex;/*********Found************/}worker;worker.ID=1L;/*********Found************/scanf(%ld%s%c,&worker.num,worker.name,&worker.sex);/*********Found************/printf(worker'sinfo:num=%ldname=%ssex=%c\n,worker.num,worker.name,worker.sex);}(17)函数userLogin的功能主要是统计并返回登录的用户数,用户名最长30字节#includestdio.hlonguserLogin(char*userName){/*********Found************/longuserCount=0;userCount++;printf(welcomeuser%s.Therehave%ldusersinsystem.\n,userName,userCount);/*********Found************/return;}voidmain(){/*********Found************/char*userName;inti;while(1){printf(userName:);scanf(%s,userName);i=userLogin(userName);7}}#includestdio.hlonguserLogin(char*userName){/*********Found************/staticlonguserCount=0;userCount++;printf(welcomeuser%s.Therehave%ldusersinsystem.\n,userName,userCount);/*********Found************/returnuserCount;}voidmain(){/*********Found************/charuserName[31];inti;while(1){printf(userName:);scanf(%s,userName);i=userLogin(userName);}}(19)将inBuf中字符串拆分成一个个的单词/*单词之间的分隔符由串divChar,程序中定义为“;?!,./\”。函数getStrFromBuf的功能是从Buf的开始处寻找第一个单词,将找到的单词作为一个字符串复制到Str处,divStr指明单词间的分隔符。GetStrFromBuf的返回值指向已找到单词的下一个字符。函数charInStr的功能是判断字符t是否出现在串Str中。主程序的功能是将inBuf中的所有单词在屏幕上显示出来。*/#includestdio.h#defineYes1#defineNo0char*getStrFromBuf(char*Buf,char*Str,char*divStr);intcharInStr(char*Str,chart);voidmain(){charinBuf[100]=howoldare/you?,*p,oneWord[20];chardivChar[]=;?!,./\\;p=inBuf;while(*p){/*********Found************/getStrFromBuf(p,oneWor
本文标题:C语言改错题以及参考答案
链接地址:https://www.777doc.com/doc-2907964 .html