1、二级 C 语言-391 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.字符串 str 由数字字符组成(长度不超过 5 个字符),可看作任意进制的数,请补充函数 proc(),该函数的功能是:把 str 字符串从二进制转换成十进制的数,结果保存在数组 xx 中,由函数返回转换后数组xx 的实际长度。其中 x 表示 str 原来的进制,y 表示要转换成的进制。例如,输入str=“1011“,x=2,y=10,结果输出 11。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填
2、入所编写的若干表达式或语句。 试题程序: #includestdio.h #includestdlib.h #includestring.h #define M 8 int xxM; int proc(char*str,int x,int y) int sum; int i=0; char*p=str; for(i=0;iM;i+) xxi=0; sum= 1; p+; while(*p) sum=sum*x+*p-“0“; p+; i=0; while(sum!=0) xxi= 2; 3; i+; return i; void main() char str6; int i; int n;
3、int x; int y; printf(“Enter a string made up of“0“to“9“digits character:“); gets(str); if(strlen(str)5) printf(“Error:string too longer!,please input again!/n/n“); exit(0); for(i=0;stri;i+) if(stri“0“|stri“9“) printf(“Error:%c not is“0“to“9“ digits character!/n/n“,stri); exit(0); printf(“The origina
4、l string:“); puts(str); printf(“/nINPUT x=“); scanf(“%d“, printf(“/nINPUT y=“); scanf(“%d“, n=proc(str,x,y); printf(“/n%s is convered to“,str); for(i=n-1;i=0;i-) printf(“%d/n“,xxi/n); (分数:30.00)二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:根据整型形参 n 的值,计算如下公式的值。 t=1-1/(2*2)-1/(3*3)-1/(n*n) 例如,当 n=7
5、时,t=0.488203。 请修改函数 proc()中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includeconio.h #includestdio.h double proc(int n) double y=1.0; int i; /*found* for(i=2;in;i+) /*found* y-=1/(i*i); return(y); void main() int n=7; system(“CLS“); printf(“/nThe result is%1f/n“,pro
6、c(n); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.M 名学生的成绩已在主函数中放入一个带头结点的链表结构中,h 指向链表的头结点。请编写函数proc(),它的功能是:找出学生的最高分,由函数值返回。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdio.h #includestdlib.h #define M 8 struct slist double s; struct slist * next; ; typedef struct slist
7、 STREC; double proc(STREC*h) STREC*creat(double*s) STREC*h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC); p-s=0; while(iM) /产生 8 个结点的链表,各分数存入链表中 q=(STREC*)maUoc(sizeof(STREC); p-s=si;i+;p-next=q;p=q; p-next=NULL。 return h;/返回链表的首地址 void outlist(STREC*h) STREC*p; p=h; print(“head“); do printf(“-%2
8、.0f“,p-s);p=p-next; /输出各分数 while(p-next!=NULL); printf(“/n/n“); void main() double stuM=60,70,80,90,65,75,85,95, max; STREC*h; h=creat(stu); outlist(h); max=proc(h); printf(“max=%6.1f/n“,max); (分数:40.00)_二级 C 语言-391 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.字符串 str 由数字字符组成(长度不超过 5 个字符),可
9、看作任意进制的数,请补充函数 proc(),该函数的功能是:把 str 字符串从二进制转换成十进制的数,结果保存在数组 xx 中,由函数返回转换后数组xx 的实际长度。其中 x 表示 str 原来的进制,y 表示要转换成的进制。例如,输入str=“1011“,x=2,y=10,结果输出 11。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填入所编写的若干表达式或语句。 试题程序: #includestdio.h #includestdlib.h #includestring.h #define M 8 int xxM; int p
10、roc(char*str,int x,int y) int sum; int i=0; char*p=str; for(i=0;iM;i+) xxi=0; sum= 1; p+; while(*p) sum=sum*x+*p-“0“; p+; i=0; while(sum!=0) xxi= 2; 3; i+; return i; void main() char str6; int i; int n; int x; int y; printf(“Enter a string made up of“0“to“9“digits character:“); gets(str); if(strlen(
11、str)5) printf(“Error:string too longer!,please input again!/n/n“); exit(0); for(i=0;stri;i+) if(stri“0“|stri“9“) printf(“Error:%c not is“0“to“9“ digits character!/n/n“,stri); exit(0); printf(“The original string:“); puts(str); printf(“/nINPUT x=“); scanf(“%d“, printf(“/nINPUT y=“); scanf(“%d“, n=pro
12、c(str,x,y); printf(“/n%s is convered to“,str); for(i=n-1;i=0;i-) printf(“%d/n“,xxi/n); (分数:30.00)解析:sum*x+*p-“0“ sum%y sum=sum/y解析 要完成程序所要求的功能,首先要将字符串转换为 int 型的数据,因此,第一处填“sum*x+*p-“0“”;然后再将变量 sum 转换为所需要的类型,因此,第二处填“sum%y”,第三处填“sum=sum/y”。二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:根据整型形参 n 的值,计算如下
13、公式的值。 t=1-1/(2*2)-1/(3*3)-1/(n*n) 例如,当 n=7 时,t=0.488203。 请修改函数 proc()中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includeconio.h #includestdio.h double proc(int n) double y=1.0; int i; /*found* for(i=2;in;i+) /*found* y-=1/(i*i); return(y); void main() int n=7; syste
14、m(“CLS“); printf(“/nThe result is%1f/n“,proc(n); (分数:30.00)解析:错误:for(i=2;in;i+) 正确:for(j=2;i=n;i+) 错误:y-=1/(i*i); 正确:y-=1.0/(i*i); 解析 根据题目中所给公式 t=1-1/(22)-1/(33)-1/(nn),可知需要循环至 i=n,因此,“for(i=2;in;i+)”应改为“for(i=2;i=n;i+)”;根据 C 语言中的语法规则,整数与整数相除结果仍为整数,要得到浮点数在分数中,分子或分母最少有一个是浮点数,因此,“y-=1/(i*i);”应改为“y-=1.
15、0/(i*i);”。三、程序设计题(总题数:1,分数:40.00)3.M 名学生的成绩已在主函数中放入一个带头结点的链表结构中,h 指向链表的头结点。请编写函数proc(),它的功能是:找出学生的最高分,由函数值返回。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdio.h #includestdlib.h #define M 8 struct slist double s; struct slist * next; ; typedef struct slist STREC
16、; double proc(STREC*h) STREC*creat(double*s) STREC*h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC); p-s=0; while(iM) /产生 8 个结点的链表,各分数存入链表中 q=(STREC*)maUoc(sizeof(STREC); p-s=si;i+;p-next=q;p=q; p-next=NULL。 return h;/返回链表的首地址 void outlist(STREC*h) STREC*p; p=h; print(“head“); do printf(“-%2.0f“,p
17、-s);p=p-next; /输出各分数 while(p-next!=NULL); printf(“/n/n“); void main() double stuM=60,70,80,90,65,75,85,95, max; STREC*h; h=creat(stu); outlist(h); max=proc(h); printf(“max=%6.1f/n“,max); (分数:40.00)_正确答案:()解析:double proc(STREC*h) double max=h-s; while(h!=NULL)/判断其是否为终结点 if(maxh-s)max=h-s;/把大的数放到 max 中 h=h-next; return max; /最后通过返回值返回到主函数中 解析 要找出学生的最高分,就要比较所有学生的成绩。定义一个变量 max 用来存放学生的最高分,初始化为第一个学生的成绩,依次与其后的学生成绩相比较,最后将得到的最高分返回主函数。