1、Comment lsj1: 由于下面的原因,所以此处的 yy不能定义成数组,所以使用指针变量Comment lsj2: 数组名不能做赋值运算和自增运算Comment lsj3: 注意:宏定义只做替换工作。所以,用 2+3去替换 r,而不是用(2+3)去替换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
2、(; *yy; yy+) putchar(*yy);(4)计算半径为 2+3的圆的面积#include#define PI 3.14/*Found*/#define S(r) PI*r*rvoid main()/*Found*/ int mianJi;mianJi = S(2+3); printf(“mian ji=%5.2fn“, mianJi);#include#define PI 3.14/*Found*/#define S(r) PI*(r)*(r)void main()/*Found*/2float mianJi;mianJi = S(2+3);printf(“mian ji=%5.
3、2fn“, mianJi);(8)打开文件 d:te.c 用于读并判断打开是否成功#includevoid main()FILE *fp;/*Found*/char fileName = “d:te.c“;/*Found*/fp = fopen(fileName, “w“);/*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 = fo
4、pen(fileName, “r“);/*Found*/if (fp = NULL)puts(“File Open Error!“);exit(1);putchar(fgetc(fp);fclose(fp);(9)申请 100 个字节的内存空间,显示其首地址,然后释放申请到的内存空间#include#includevoid main()/*Found*/char p100;Comment lsj4: p能做赋值运算,所以 p不能是数组名,而应该是指针变量。Comment lsj5: 用 const说明的量在程序运行过程中始终保持不变,相当于常量。所以下面的 i+;是错误的!Comment ls
5、j6: 这两条语句都是循环体,所以必须用花括号括起来构成复合语句,那么花括号在哪里?此处只能是宏替换来完成这个任务了。3/*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
6、!n“);exit(1); printf(“%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 Comment lsj7: 函数形参中,能作出口
7、参数的只能是数组或者是指针变量。普通变量作形参,形参的修改,不影响对应的实参。4void main()char *p = “13125176247“; const int 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
8、 = 4;/*Found*/ swap(a, b);printf(“a=%d b=%dn“, a, b);#include/*Found*/void swap(int *x, 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);Comment lsj8: tmp指针变量没有赋初值,所以不能使用*tmp5(15)调用函数 swap,将 a和 b的值交换,最后在屏幕上显示交换后的 a,b之
9、值#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*/ 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
10、*/ swap(/*Found*/ printf(“a=%d b=%dn“, a, b);(16)worker的信息使用结构体存储,从键盘读入其各项信息并显示#includevoid main()struct WKER long ID; long int num;char name20;Comment lsj9: 该局部变量根据题目的功能看,它不能每次从 0开始,换句话说,它的值在该函数调用结束后还应该保留,所以该是静态变量。Comment lsj10: 指针变量userName本身是不能存放字符串的。它可以指向一片空间,由那片空间来存串的内容。但是,此处它没有赋初值,即没有指向一片可用空间。
11、6char 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 WKER long ID; long int num;char name20; char sex;/*Found*/ worker;worker.ID = 1L; /*Found*/scanf(“%ld %s %c“, /*Foun
12、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
13、*/return ;void main( )/*Found*/ char *userName;int i;while (1) printf(“userName: “); scanf(“%s“, userName);i = userLogin(userName);Comment lsj11: 这道题目算法本身难一些,但是改错的部分基本不太涉及复杂的算法,一般是赋值语句类型不匹配等比较明显的错误。Comment lsj12: 该函数有返回值7#include long userLogin(char *userName)/*Found*/ static long userCount = 0;user
14、Count+; printf(“welcome user %s. There have %ld users in system.n“,userName, userCount); /*Found*/return userCount;void main( )/*Found*/ char userName31;int i;while (1) printf(“userName: “); scanf(“%s“, userName);i = userLogin(userName); (19)将 inBuf中字符串拆分成一个个的单词/* 单词之间的分隔符由串 divChar,程序中定义为“;?!,./” 。
15、 函数 getStrFromBuf的功能是从 Buf的开始处寻找第一个单词,将找到的单词作为一个字符串复制到 Str处,divStr 指明单词间的分隔符。GetStrFromBuf 的返回值指向已找到单词的下一个字符。函数 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
16、 main()char inBuf100 = “how old are/ you?“, *p, oneWord20; char divChar = “ ;?!,./“;p = inBuf; while (*p) /*Found*/getStrFromBuf(p, oneWord, divChar); if (*oneWord) puts(oneWord);Comment lsj13: 关系运算左右两个操作数类型不匹配Comment lsj14: 字符串结束标记0表达错误8 int charInStr(char *Str, char t)for (; *Str; Str+) /*Found*/
17、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) break; for (; *Buf; Buf+) if (charInStr(divStr, *Buf) = Yes) break; /*Found*/ Str = *Buf;Str+; /*Found*/ *Str = 0;/*Found*/ return ;#incl
18、ude#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 = “ ;?!,./“;9p = inBuf;while (*p) /*当 p 未指向字符串结束符*/*Found*/p = getStrFromBuf(p, oneWord, divChar); /*让 p 指向
19、当前单词后*/*如果找到了一个单词,即 oneWord 第 0 字符不为0。有可能找不到,例如找到前一单词 you 后,p 指向?且未结束,再找则没有*/if (*oneWord)puts(oneWord);/* 判断字符 t 是否出现在串 Str 中,是则返回 Yes,否则返回 No */int charInStr(char *Str, char t)for (; *Str; Str+)/*Found*/if (*Str = t)/*Found*/return Yes;/*Found*/return No;/* 从 Buf 的开始处寻找第一个单词,将找到的单词作为一个字符串复制到 Str 处
20、,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;10/*Found*/return Buf; /*Buf 现已指向当前单词后,返回该位置*/
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。