综合测试题一参考答案 一 填空题 ( 表达式求值 )( 本大题共 10 小题, 每小题 1 分, 共 10 分 ) 设各语句的初始化 相同 :int x=3,y=2,z=1; 1. x=y==z x= 0 2. x=!(z>y)&&!x 1 x= 1 3. x=(y++*1/3) x= 0 4. x=((x>y>z)?1:0) x= 0 5. x*=2+3 x= 15 6. x=(++z*y,y++,z%y) x= 2 7. y=--z&&--x x= 3 8. x=5 x!=y x= 1 9. x=1.5+7%3*(int)(2.5+4.7)%2/4 x= 1 10. x='a'-'a' x= 32 二 读下列程序段, 指出错误并改正 ( 改错题 )( 本大题共 5 小题, 每小题 4 分, 共 20 分 ) 说明 : 指出错误的表示形式为错误处加下划线 : 如 int a[n][n]; 改正形式为在错误语句之后加修改内容 : 如 int a[4][4]; 1. #include <stdio.h> #define MAX(a,b) (a>=b?a:b) //1 a 数组无 '\0', 改为 :char a[7]= "a1b2c3" int i; // 或 char a[7]={'a','1','b','2','c','3','\0' char a[ 6 ]={'a','1','b','2','c','3';//2 数组定义过小, 不能存放串结束 char b[ 6 ]="234def"; // 符 \0 ⑻ 至少应改为 a[7],b[7] for(i=0; a[i]!='\0'; i++) printf("i=%d,max=%c\n",i, MAX(a[i],b[i])); 2.#include <stdio.h> int a=9; //1 函数应先定义后使用, 否则事先声明, 因此须加 double m; // 函数原型说明 :double f1(double i,int j); double f1(double i,int j); //2 形实参数的类型不匹配, 应此须互换处理 : m=f1(3.14, a); // m=f1(a,3.14); 或 printf("%f\n",m); // double f1(double i,int j){
double f1(int i,double j){ return(i*i+j*j); 3. #include <stdio.h> // 字符指针数组不能直接存放字符串, int i; // 修改方案有 2: char *str[5]; //1 char s[5][80]; 然后每行的起始地址赋给 char s[5][80]; // 指针数组的相应元素 : for(i=0; i<5; i++)str[i]=s[i]; // for(i=0; i<5; i++)str[i]=s[i]; for(i=0; i<5; i++) //2 或直接定义二维数组 scanf("%s",str[i]); // char str[5][80]; for(i=0; i<5; i++) printf("%s\n",str[i]); 4. #include <stdio.h> int i, r[10],sum; //1 累加器应清零 :sum=0; for(i=0; i<=10; i++){ //2 数组越界使用应改为 : r[i]=i+1; // for(i=0; i<10; i++){ sum+=r[i]; printf("sum=%d",sum); 5. #include <stdio.h> // 结构体类型定义时不能赋初值, 应改为 : int i; // struct student{ struct student{ // int num; int num=0; // float score1,score2,aver; float score1=0,score2=0,aver=0; // stu[3]={{200401,92,83,{200402,85,70,{200403,65,89; for(i=0; i<3;i++){ stu[i].aver=(stu[i].score1+stu[i].score2)/2; printf("%d,%f\n",stu[i].num,stu[i].aver);
三 阅读分析下列程序, 写出运行结果 ( 阅读分析题 ) ( 本大题共 7 小题, 每小题 4 分, 共 28 分 ) 1. #include <stdio.h> int i,f1=1,f2=1; 1 1 2 3 for(i=1;i<=4;i++){ printf("%6d%6d",f1,f2); if(i%2==0) printf("\n"); f1=f1+f2; f2=f2+f1; 2. #include "stdio.h" int i,x; for(i=1, x=1; i<7; i++){ if(x>=7)break; if(x%2==1){ x+=5;continue; x-=3; printf("i=%d,x=%d\n",i,x); printf("i=%d,x=%d\n",i,x); 3. #include <stdio.h> fun(int a){ int b=1; 5 i=2,x=3 i=4,x=8 6 8 8 13 10 21 static int c=1; a++; b++; c++; return(a+b+c); int i=1; for( ; i<=3; i++) printf("%6d",fun(i)); 4. #include <stdio.h> int i,j; computer->retupmoc char c, a[]="computer", *p=a; printf("%s",a); for(i=0,j=7; i<j; i++,j--){ c=p[i];
*(p+i)=*(p+j); p[j]=c; printf("->%s",a); 5.#include <stdio.h> void swap2(int *pa, int *pb){ int *t; t=pa; pa=pb; pb=t; void swap3(int *pa, int *pb){ int t; t=*pa; *pa=*pb; *pb=t; int x=1,y=3,*p1=&x,*p2=&y; swap2(p1,p2); printf("%d,%d\n",x,y); swap3(p1,p2); printf("%d,%d\n",x,y); 6. #include "stdio.h" static struct st1{ char c[4], *s; s1={"abc","def"; static struct st2{ char *cp; struct st1 ss1; s2={"ghi",{"jkl","mno"; printf("%c\t%c\n",s1.c[0], *s1.s); ++s2.cp; ++s2.ss1.s; printf("%s\t%s\n",s2.cp, s2.ss1.s); 7. 有以下程序, 编译后生成可执行程序文件 display 写出在执行: display how are you! 命令行后的结果 1,3 3,1 a d hi no how are you! #include <stdio.h> main(int argc, char *argv[]){ int i; for(i=1; i<argc; i++) printf("%s%c",*++argv,(i<argc-1?' ':'\n'));
四 程序填空题 ( 本大题共 2 小题, 每空 1 分, 共 15 分 ) 1. 本程序功能为建立链表及统计链表中所有节点的数据域 n 之和 #include <stdio.h> #include <malloc.h> struct node{ int n; struct node *next; ; struct node *creat( ){ // 建立链表 struct node *head, *p1, *p2; head= NULL ; p1=p2=(struct node *)malloc(sizeof(struct node)); scanf("%d", &p1->n); while( p1->n!=0 ){ if( head==null ) head= p1 ; else p2->next= p1 ; p2=p1; p1=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p1->n); p2->next=null; return( head ); add(struct node *head ){ struct node *p; int sum=0; p=head; while(p!=null){ sum=sum+p->n; p=p->next; return(sum); void main( ){ // 统计链表中所有节点的数据域 n 之和 struct node *head; int m; head=creat(); m=add(head); printf("%d\n", m); 2. 已知正文有文件 abc.txt, 本题统计该文件中词的个数并求出最长词的长度 一个词与另一个词之间由空格符, 制表符, 换行符分隔 其中 :word 存放词的个数,maxLen 存放最长词的长度,length 存放当前词的长度,flag 为状态标记 (flag=1 表示当前字符在单词中, flag=0 表示当前字符不在某词中 )
#include <stdio.h> #include <process.h> FILE *fp; char ch; int word=0,maxlen=0, length=0, flag=0; if((fp=fopen("abc.txt", "r"))==null){ printf("cannot open the file!\n"); exit(0); ch=fgetc(fp); while(ch!=eof){ if(ch==' ' ch=='\t' ch=='\n'){ if(flag==1){ flag=0; if(length>maxlen) maxlen=length; else if(flag==0){ flag= 1 ; length=1; word ++ ; else length++; ch= fgetc(fp) ; fclose(fp); printf("word=%d,maxlen=%d\n",word,maxlen); 五 程序设计题 ( 本大题共 2 小题, 第 1 小题 12 分, 第 2 小题 15 分, 共 27 分 ) 1. 根据主函数的定义, 编写以下两个函数 : 1 求数组 a[4][4] 主对角线上元素的和 ;(6 分 ) 2 实现将数组 a[4][4] 的矩阵转置处理 (6 分 ) static int a[4][4]={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4; int *p, i, sum; sum=total(a,4); printf("%d\n",sum); // 调用 total 函数求主对角线上元素的和 p=*a; convert(p); // 调用 convert 实现矩阵转置 for(i=0; i<4; i++) printf("%d %d %d %d\n",a[i][0],a[i][1],a[i][2],a[i][3]);
// 参考程序清单如下 : #include <stdio.h> void convert(int *p){ 本函数 6 分 int i, j, t; for(i=0; i<4; i++) for(j=i; j<4; j++){ t=*(p+4*i+j); *(p+4*i+j)=*(p+4*j+i); *(p+4*j+i)=t; int total(int a[][4], int n){ 本函数 6 分 int i,sum=0; for(i=0; i<n; i++) if(a[i][i]!=0) sum+=a[i][i]; return(sum); static int a[4][4]={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4; int *p, i, sum; p=*a; sum=total(a,4); printf("%d\n",sum); convert(p); for(i=0; i<4; i++) printf("%d %d %d %d\n",a[i][0],a[i][1],a[i][2],a[i][3]); (2). 编一个程序, 用结构体数组描述 60 位学生的信息, 包括 : 学号 姓名 三门课的成绩和总成绩 并以函数形式实现以下功能 :(15 分 ) 1 输入 60 位学生的前五项信息, 并计算每个学生的总成绩 ;(3 分 ) 2 按总成绩递减顺序对成绩表排序 ;(6 分 ) 3 输出排序后的成绩表 (3 分 ) 注 : 类型定义和主函数 (3 分 ) // 参考程序清单如下 : #define N 60 #define STUDENT struct student STUDENT{ int num; char name[16]; int score[4]; ;
STUDENT stu[n]; void read(student *p, int n); void sort(student s[], int n); void print(student *p, int n); read(stu,n); sort(stu,n); print(stu,n); void read(student *p, int n){ int i,j,s; for(i=0; i<n; i++,p++){ scanf("%d%s",&p->num,p->name); for(s=j=0; j<3; j++){ scanf("%d",&p->score[j]); s=s+p->score[j]; p->score[3]=s; void sort(student s[], int n){ int i,j,k; STUDENT temp; for(i=0; i<n-1; i++){ k=i; for(j=i+1; j<n; j++){ if(s[k].score[3]<s[j].score[3]) k=j; if(k!=i){ temp=s[i]; s[i]=s[k]; s[k]=temp; void print(student *p, int n){ int i,j; for(i=0; i<n; i++,p++){ printf("%6d%16s",p->num,p->name); for(j=0; j<4; j++) printf("%6d",p->score[j]); printf("\n");