华中科技大学2003-2006级信息学院c语言程序设计考试试题及答案(编辑修改稿)内容摘要:

7。 m++) { scanf(%s,name)。 for(n=0。 n3。 n++) if (!strcmp(name,leader[n].name)) { leader[n].count++。 break。 } } printf(\n)。 for(m=0。 m 3。 m++) printf(%s:%d\n,leader[m].name,leader[m].count)。 } 当程序运行时,键入情况如下: Li Wang Lei Li Wang Zhang 写出程序的输出结果。 (基本上是书中例题) Li:2 Zhang:1 Wang:2 (5) include include void main() { char *name[]={capital,index,large,small}。 int a,b,n=4。 char *temp。 for(a=0。 an1。 a++) for(b=a+1。 bn。 b++) { if(strcmp(name[a],name[b])0) { temp=name[a]。 name[a]=name[b]。 name[b]=temp。 } } 在此之前是书中的例题 for(a=0。 an。 a++) printf(%s\n,name[a]+a)。 输出时应该能够识别指针及偏移 情况 } capital ndex rge ll 六、 编写程序 ( 35 分) ( 1) 求一元二次方程 ax2 +bx+c=0 的根,实系数 a,b,c 从终端输入,只考虑两个不同实根和两个相同的实根 ( 9分) (书中例题 , p66`67) include include void main() { float a,b,c。 float x1,x2。 float x3,x4。 float m。 printf(input the numbers:a,b,c)。 scanf(%f%f%f,amp。 a,amp。 b,amp。 c)。 if(a==0) { printf(the input is error!\n)。 return。 } m=b*b4*a*c。 if(m0) { x1=(b+sqrt(m))/(2*a)。 x2=(bsqrt(m))/(2*a)。 printf(x1:%.2f x2:%.2f\n,x1,x2)。 } else if(m==0) { x1=x2=(b+sqrt(m))/(2*a)。 printf(x1=x2=%.2f\n,x1)。 } else { x3=b/(2*a)。 x4=sqrt(m)/(2*a)。 printf(x1=%.2f+%.2fi\n,x3,x4)。 printf(x2=%.2f%.2fi\n,x3,x4)。 } } ( 2)编写一个函数,求 s=a+aa+aaa++aaaaaaaaaa,其中 a是一个数字,例如 2+22+222+2222(此时 n=4)。 主函数 a和 n的输入,调用所函数和输出所求的 累加和;编写的函数完成计算。 ( 9分) 注意:不得使用全局变量,注意程序结构 (书中习题 3: 4。 16) include include long Cal(long a,long n)。 main() { long sn=0。 long a,n。 printf(please input a n:)。 scanf(%d%d,amp。 a,amp。 n)。 sn = Cal(a,n)。 printf(a+aa+aaa+...+aa...a=%ld\n\n,sn)。 } long int Cal(long a,long n) { int i。 long sn=0,m=0。 for(i=0。 in。 i++) { m=m*10+a。 sn+=m。 } return sn。 } ( 3)从十个字符串中找出所要的某个字符串,若能找到则把他删 除,然后输出新字符串;若未找到则输出“” can not fond” . ( 9分) include include main() { char s[10][80]。 char s2[80]。 int i,j。 int num=0。 printf(please enter 10 string:\n)。 for(i=0。 i10。 i++) gets(s[i])。 printf(please enter s2 string:)。 gets(s2)。 for(i=0。 i10。 i++) { if(strcmp(s[i],s2)==0) { for(j=i。 j9num。 j++) { strcpy(s[j],s[j+1])。 } i。 num++。 } } for(i=0。 i10num。 i++) puts(s[i])。 } ( 4)一个班有 N个同学,修 5门课从键盘输入他们的性名、学号、性别和成绩。 1)按平均成绩从高到底打印全班的成绩单。 2)求第三门课的平均分 3)找出平均分在 90以上或全部功课在 85以上的女生。 要求:输入、输出、计算、排序和查找分别用函数实现,主函数只是调用这些函数。 不得使用全局变量。 include define N 5 struct Student { char name[20]。 int number。 int sex。 float score[5]。 float aver。 }。 void Input(struct Student *stu)。 void Average(struct Student *stu,float *aver)。 void Sort(struct Student *stu)。 void Search(struct Student *stu,float score)。 main() { struct Student stu[N]。 float score=。 float aver3。 Input(stu)。 Sort(stu)。 Average(stu,amp。 aver3)。 printf(average3 is %.2f\n,aver3)。 Search(stu,score)。 } void Input(struct Student *stu) { int i,j。 float aver=0。 for(i=0。 iN。 i++) { aver=0。 printf(please enter name:)。 gets(stu[i].name)。 printf(please enter number sex:)。 scanf(%d%d,amp。 stu[i].number,amp。 stu[i].sex)。 printf(please enter score(5):)。 for(j=0。 j5。 j++) { scanf(%f,amp。 stu[i].score[j])。 aver +=stu[i].score[j]。 } stu[i].aver = aver/5。 getchar()。 } } void Sort(struct Student *stu) { int i,j。 struct Student temp。 for(i=0。 iN1。 i++) { for(j=i+1。 jN。 j++) { if(stu[i].averstu[j].aver) { temp=stu[i]。 stu[i]=stu[j]。 stu[j]=temp。 } } } printf(Name Numb Sex score(5) aver\n)。 for(i=0。 iN。 i++) { printf(%8s %d %d %.2f %.2f %.2f %.2f %.2f %.2f\n, stu[i].name, stu[i].number, stu[i].sex, stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4], stu[i].aver)。 } } void Average(struct Student *stu,float *average3) { float sum3=0。 int i。 for(i=0。 iN。 i++) sum3 += stu[i].score[2]。 *average3=sum3/N。 } void Search(struct Student *stu,float score) { int i,j。 int flag =1。 printf(\naver90 score85\n)。 printf(Name Numb Sex score(5) aver\n)。 for(i=0。 iN。 i++) { for(j=0。 j5。 j++) flag = flagamp。 amp。 (stu[i].score[j]score)。 if(stu[i].averamp。 amp。 flag) { printf(%8s %d %d %.2f %.2f %.2f %.2f %.2f %.2f\n, stu[i].name, stu[i].number, stu[i].sex, stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4], stu[i].aver)。 } } printf(\n\n)。 } 20xx 级信息学院《 C 语言程序设计》考试试题 一、 判断下列语句或程序的对错。 (“ ╳ ”表示错,“ √ ”表示对)( 10 分) 1 float s=0,=0。 ( ╳ ) 2 define M=100 ( ╳ ) int a[M]。 3 char *p[]=”\”c test\” ”。 ( ╳ ) 4 if((a=b)0)t=a。 ( √ ) 5 char str1[20]==” china”,str2[20]。 ( ╳ ) str2=str1。 6 int i,*p=amp。 i。 ( √ ) 7 float a[100],*p。 ( √ ) p=a+1。 8 printf(“%d\n”,(30,50,80))。 ( √ ) 9 int x,y。 ( √ ) y=20,x=y+‟a‟。 10 int (*p)[20],a[20]。 ( √ ) p=(int (*)[20])a。 二、 计算下列表达式的值( 10 分) 设 unsigned int a=7,b=17,c=5,d=3。 float x=,y=。 (1)x+a%3。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。