1、改错参考答案:(3)将字符串 yy 在屏幕上输出#includevoid main()/*Found*/char yy100 = “ok?n“;/*Found*/f0r (; *yy; yy+)putchar(*yy);#includevoid main()/*Found*/char *yy=“ok?n“;/*Found*/for (; *yy; yy+)putchar(*yy);(4)计算半径为 2+3 的圆的面积#include#define PI 3.14/*Found*/#define S(r) PI*r*rvoid main()/*Found*/int mianJi;mianJi =
2、 S(2+3);printf(“mian ji=%5.2fn“, mianJi);#include#define PI 3.14/*Found*/#define S(r) PI*(r)*(r)void main()/*Found*/float mianJi;mianJi = S(2+3);printf(“mian ji=%5.2fn“, mianJi);(8)打开文件 d:te.c 用于读并判断打开是否成功#includevoid main()FILE *fp;/*Found*/char fileName = “d:te.c“;/*Found*/fp = fopen(fileName, “w“
3、);/*Found*/if (fp = EOF)puts(“File Open Error!“);exit(1);putchar(fgetc(fp);fclose(fp);#includevoid main()FILE *fp;/*Found*/char fileName = “d:te.c“;/*Found*/fp = fopen(fileName, “r“);/*Found*/if (fp = NULL)puts(“File Open Error!“);exit(1);putchar(fgetc(fp);fclose(fp);(9)申请 100 个字节的内存空间,显示其首地址,然后释放申请
4、到的内存空间#include#includevoid main()/*Found*/char p100;/*Found*/if (p = (char *)malloc(100) = NULL)printf(“malloc memory fail!n“);return ;printf(“%pn“, p);/*Found*/fclose(p);#include#includevoid main()/*Found*/char *p;/*Found*/if (p = (char *)malloc(100) = NULL)printf(“malloc memory fail!n“);exit(1);pr
5、intf(“%pn“, p);/*Found*/free(p);(10)将字符串 p 显示在屏幕上#include/*Found*/#define BEGIN/*Found*/#define ENDvoid main()char *p = “13125176247“;const int i = 0;for (printf(“n“); pi; )BEGINputchar(pi);/*Found*/i+;END#include/*Found*/#define BEGIN /*Found*/#define END void main()char *p = “13125176247“;const in
6、t i = 0;for (printf(“n“); pi; )BEGINputchar(pi);/*Found*/p+;END(14)调用函数 swap,将 a 和 b 的值交换,最后在屏幕上显示交换后的 a,b 之值#include/*Found*/void swap(int x, int y)int tmp;/*Found*/_*x = *y;/*Found*/y = x;void main()int a = 3, b = 4;/*Found*/swap(a, b);printf(“a=%d b=%dn“, a, b);#include/*Found*/void swap(int *x,
7、int *y)int tmp;/*Found*/tmp = *x;*x = *y;/*Found*/*y = tmp;void main()int a = 3, b = 4;/*Found*/swap(printf(“a=%d b=%dn“, a, b);(15)调用函数 swap,将 a 和 b 的值交换,最后在屏幕上显示交换后的 a,b 之值#includevoid swap(int *x, int *y)int *tmp, xy;/*Found*/*tmp = x;*x = *y;/*Found*/y = *tmp;void main()int a = 3, b = 4;/*Found*
8、/swap(*a, *b);/*Found*/printf(“a=%d b=%dn“, #includevoid swap(int *x, int *y)int *tmp, xy;/*Found*/xy = *x;*x = *y;/*Found*/*y = xy;void main()int a = 3, b = 4;/*Found*/swap(/*Found*/printf(“a=%d b=%dn“, a, b);(16)worker 的信息使用结构体存储,从键盘读入其各项信息并显示#includevoid main()struct WKERlong ID;long int num;char
9、 name20;char sex;/*Found*/ ;worker.ID = 1L;/*Found*/scanf(“%d %s %s“, /*Found*/printf(“workers info: num=%d name=%s sex=%sn“,worker.num, worker.name, worker.sex);#includevoid main()struct WKERlong ID;long int num;char name20;char sex;/*Found*/ worker;worker.ID = 1L;/*Found*/scanf(“%ld %s %c“, /*Foun
10、d*/printf(“workers info: num=%ld name=%s sex=%cn“,worker.num, worker.name, worker.sex);(17)函数 userLogin 的功能主要是统计并返回登录的用户数,用户名最长 30 字节#include long userLogin(char *userName)/*Found*/long userCount = 0;userCount+;printf(“welcome user %s. There have %ld users in system.n“,userName, userCount);/*Found*/
11、return ;void main( )/*Found*/char *userName;int i;while (1)printf(“userName: “);scanf(“%s“, userName);i = userLogin(userName);#include long userLogin(char *userName)/*Found*/static long userCount = 0;userCount+;printf(“welcome user %s. There have %ld users in system.n“,userName, userCount);/*Found*/
12、return userCount;void main( )/*Found*/char userName31;int i;while (1)printf(“userName: “);scanf(“%s“, userName);i = userLogin(userName);(19)将 inBuf 中字符串拆分成一个个的单词/* 单词之间的分隔符由串 divChar,程序中定义为“;?!,./” 。函数 getStrFromBuf 的功能是从 Buf 的开始处寻找第一个单词,将找到的单词作为一个字符串复制到 Str 处,divStr 指明单词间的分隔符。GetStrFromBuf 的返回值指向已找
13、到单词的下一个字符。函数 charInStr 的功能是判断字符 t 是否出现在串 Str 中。主程序的功能是将 inBuf 中的所有单词在屏幕上显示出来。*/#include#define Yes 1#define No 0char *getStrFromBuf(char *Buf, char *Str, char *divStr);int charInStr(char *Str, char t);void main()char inBuf100 = “how old are/ you?“, *p, oneWord20;char divChar = “ ;?!,./“;p = inBuf;wh
14、ile (*p)/*Found*/getStrFromBuf(p, oneWord, divChar);if (*oneWord)puts(oneWord);int charInStr(char *Str, char t)for (; *Str; Str+)/*Found*/if (*Str = *t)/*Found*/return No;/*Found*/return Yes;char *getStrFromBuf(char *Buf, char *Str, char *divStr)for (; *Buf; Buf+)if (charInStr(divStr, *Buf) = No)bre
15、ak;for (; *Buf; Buf+)if (charInStr(divStr, *Buf) = Yes)break;/*Found*/Str = *Buf;Str+;/*Found*/*Str = 0;/*Found*/return ;#include#define Yes 1#define No 0char *getStrFromBuf(char *Buf, char *Str, char *divStr);int charInStr(char *Str, char t);void main()char inBuf100 = “how old are/ you?“, *p, oneWo
16、rd20;char divChar = “ ;?!,./“;p = inBuf;while (*p) /*当 p 未指向字符串结束符*/*Found*/p = getStrFromBuf(p, oneWord, divChar); /*让 p 指向当前单词后*/*如果找到了一个单词,即 oneWord 第 0 字符不为0 。有可能找不到,例如找到前一单词 you 后,p 指向?且未结束,再找则没有*/if (*oneWord)puts(oneWord);/* 判断字符 t 是否出现在串 Str 中,是则返回 Yes,否则返回 No */int charInStr(char *Str, char
17、 t)for (; *Str; Str+)/*Found*/if (*Str = t)/*Found*/return Yes;/*Found*/return No;/* 从 Buf 的开始处寻找第一个单词,将找到的单词作为一个字符串复制到 Str 处,divStr 指明单词间的分隔符。返回值指向已找到单词的下一个字符。*/char *getStrFromBuf(char *Buf, char *Str, char *divStr)for (; *Buf; Buf+) /*本循环将指针移至单词的第一字符上,即跳过分隔符 */if (charInStr(divStr, *Buf) = No)break;for (; *Buf; Buf+)if (charInStr(divStr, *Buf) = Yes) /*若遇分隔符,则当前单词结束*/break;/*Found*/*Str = *Buf;Str+;/*Found*/*Str = 0;/*Found*/return Buf; /*Buf 现已指向当前单词后,返回该位置 */
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。