1、C语言程序设计实验与习题上机实C语言程序设计实验与习题上机实验参考答案(此答案仅作为教师指导学生上机实验时参考使用,不得转发给学生!)第2章实验一 基本数据类型1、float c; double d; c=211.5f; d=211.5; printf(a/b=%dn,a/b);2、printf(%d,%o,%xn,0x65,0x65,0x65);3、 e=o+2; f=m+2; g=e+2;4、ra=de*PI/180;实验二 算术运算符和表达式1、 printf(%fn,x+a%3*(int)(x+y)%2/4); printf(%fn,(float)(a+b)/2+(int)x%(int
2、)y);2、 printf(%dn,(x=1,y=100,z=(x+y)*5); printf(%dn,x=(i=10,j=5,i*j); printf(%dn,(x=i=10,j=5,i*j);3、 double a,b,c,s,area; area=sqrt(s*(s-a)*(s-b)*(s-c);第3章实验一 数据的格式化输出略实验二 数据的格式化输入1、 a=b; B=g;2、 1012410.34 3、 a=3b=7x=8.57y=71.82c1=Ac2=a 4、略第4章实验一 if语句2、wan=ge&qian=shi3、 c=a&c=A&c=0&c=94、#include #in
3、clude void main ( ) double x, y, z; printf(输入x,y的值: n); scanf(%f%f, &x,&y ); if ( x = 0&y=0 ) z = log10(x)+log10(y); else if ( x = 0 ) z = sin(x)+cos(y); else if ( x 0&y 0 ) z = exp(2*x)+exp(2*y); else z = tan(x+y); printf(Z=%.2fn, z ); 实验二 switch语句2、 #include void main()float m,y,s;printf(“请输入本金和存款
4、年限:n”);scanf(%f%f,&m,&y);if(y8) n=y-8; y=8;switch(y)case 1: s = m + m * 0.63% * 12; break;case 2: s = m + m * 0.66% * 12 * 2; break;case 3: case 4: s = m + m * 0.69% * 12 * y; break;case 5: case 6: case 7: s = m + m * 0.75% * 12 * y; break;case 8: s = m + m * 0.84% * (y+n); break;defaut:printf(wrong
5、n); printf(“本金利息合计: %fn”,s);第5章实验一 循环语句的使用1、temp =m;m=n;n=temp; m=n;n=r2、 tn=tn+a; a=a*10;3、【程序如下:】#include void main() float s,f0,h,x; int n,i; printf(“输入区间分隔数n:”); scanf(“%d”,&n); h=1.0/n; f0=4.0; /初值,x=0.0时f(0.0)=4.0s=0.0; for(i=1;in;i+) s+=f0*h; x=i*h; f0=4/(1+x*x); printf(“s=%fn”,s); 实验二 循环的嵌套1
6、、【程序如下:】#include #include void main() long int sum,temp; int n,i,j=1; printf(“Please input how many terms you want the computer to calculate(19):n”); scanf(“%d”,&n); if(n=10) printf(“Overflow !n”); exit(0); sum=0; for(i=1;i=4)3、【程序如下:】#include void main()int m,i,s;for ( m=2;m=1000;m+) t=0;for (n=1;n
7、=m/2;n+)if ( m%n=0 ) t+=n;if (m=t) printf(“%6d”,m);printf(“n);4、【程序如下:】#include void main() int x,y,z; for(x=0;x=3;x+) for(y=1;y=5;y+) for(z=0;z=6;z+) if (x+y+z=8) printf(“x=%d,y=%d,z=%d”,x,y,z);5、if (x/y=s) t+=10;实验三 continue 语句和break1、x=10 y=62、a=12 y=12 a=16 y=283、a=84、k=45、money10000 break第6章实验一
8、 一维数组1、mid=(low+high)/2; if(x=amid) printf(%d,mid); break; else if(xamid) high=mid-1; else low=mid+1;2、for(i=0;iN;i+) for(j=0;jaj+1) temp=aj; aj=aj+1; aj+1=temp; 3、 scanf(%d,&base);for(i=0;n!=0;i+) numi=n%base; n=n/base; printf(the result is:);for(i-;i=0;i-) if(numi=0&numi=9) c=0+numi; else c=a+numi
9、-10; printf(%c,c);实验二 二维数组1、 int cur_i=0, cur_j; int count, size=0, i, j; while(size%2)=0) printf(n enter square size(ODD number):); scanf(%d,&size); cur_j=(size-1)/2; for(count=1;count=size*size;count+) magiccur_icur_j=count; if(count%size)=0) cur_i+;continue; cur_i-; cur_j+; if(cur_i0) cur_i+=size
10、; else if(cur_j=size) cur_j-=size; for(i=0;isize;i+) printf(n); for(j=0;jsize;j+) printf(%4d,magicij); 2、 for(i=0;i10;i+) ai0=1;aii=1;for(i=2;i10;i+)for(j=1;ji;j+)aij=ai-1j-1+ai-1j;3、 int i,j,high=1,low=1,sh,sl;printf(input score:n);for(i=1;i5;i+)for(j=1;j100|scoreij0) printf(输入成绩应在0100之间,请重新输入:n);
11、scanf(%d,&scoreij); for(i=1;i5;i+) sumi=0; for(j=1;j7;j+) sumi+=scoreij; averi=sumi/6; sh=sl=sum1;for(i=2;ish) sh=sumi;high=i; else if(sumisl) sl=sumi;low=i; printf(%d总分最高,n%d总分最低n,high,low);printf(请输入学生学号:);scanf(%d,&i);while(i4) printf(请重新输入:n);scanf(%d,&i);for(j=1;j7;j+) printf(第%d次考试成绩为:%d,n,j,s
12、coreij);printf(平均成绩为:%dn,averi);printf(请输入考试次数:);scanf(%d,&j);while(j6) printf(请重新输入:n);scanf(%d,&j);aver0=0;for(i=1;i=a&chi=A&chi=0&chi*p2) *m=*p1; *n=*p2; else *m=*p2; *n=*p1; if(*m*p3) *n=*p3;3、switch(c)case u: *(unsigned *)p3)=*(unsigned *)p1);*(unsigned *)p1)=*(unsigned *)p2);*(unsigned *)p2)=*
13、(unsigned *)p3);break; case i: *(int *)p3)=*(int *)p1);*(int *)p1)=*(int *)p2);*(int *)p2)=*(int *)p3);break; case f: *(float *)p3)=*(float *)p1);*(float *)p1)=*(float *)p2);*(float *)p2)=*(float *)p3);break; case d: *(double *)p3)=*(double *)p1);*(double *)p1)=*(double *)p2);*(double *)p2)=*(double
14、 *)p3);break; case c: *(char *)p3)=*(char *)p1);*(char *)p1)=*(char *)p2);*(char *)p2)=*(char *)p3); 实验二 指针与数组1、while(pq) t=*p; *p=*q; *q=t; p+; q-;2、scorei=max=*(*a+i);for(p=a+1;pa+N;p+)scorei+=*(*p+i);if(max*(*p+i)max=*(*p+i);3、int *p,count=0,t;while(count=a;p-)*(p+1)=*p;*a=t;count+;实验三 指针数组与指向指针的
15、指针1、for(i=1;i5;i+) for(j=0;j*p0) p0=&aij; else for(m=4;m=1;m-) if(aij*pm) for(n=1;nm;n+) pn=pn+1;pm=&aij;break; 2、for(p=pa;ppa+M;p+)for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(*(*p+j)*(*p+j+1)t=*(*p+j);*(*p+j)=*(*p+j+1);*(*p+j+1)=t;实验四 指针与函数1、第一处:switch(op) case +:func=add;break; case -:func=sub;break; cas
16、e *:func=mul;break; case /:func=div;break; default: done=0;第二处:printf(%f%c%f=%fn,op1,op,op2,(*func)(op1,op2);2、第一处:return (*func)(a,b);第二处:switch(op)case +:result=compute(add,op1,op2);break;case -:result=compute(sub,op1,op2);break;case *:result=compute(mul,op1,op2);break;case /:result=compute(div,op
17、1,op2);break;default: done=0;3、int i;*p=0;for(i=1;i*(a+*p) *p=i;return a+*p;第9章实验一 字符数组1、for(j=0;j=A&aij=a&aij=0&aij0)sj-1=s0;for(j=1;sj!=0;j+) sj-1=sj;i-;sj-1=0;3、for(len=0;silen!=0;len+);half=len/2;for(j=0;jhalf-1;j+)for(k=j+1;khalf;k+)if(sijsik)ch=sij;sij=sik;sik=ch;if(len%2) half+;for(j=half;jle
18、n-1;j+)for(k=j+1;ksik)ch=sij;sij=sik;sik=ch;实验二 字符串指针变量1、for(p=str;*p!=0;p+)if(*p= ) word=0;else if(word=0)word=1;if(*p=a&*p=z) *p-=32;2、char ch=*s,*p=s;if(ch!=0)for(;*(s+1)!=0;s+) *s+=*(s+1);*s+=ch;for(;p=0&*s=9)find=1; *p=*p*10+*s-48;else if(find)find=0; p+; num+;if(find) num+;return num;实验三 字符串处理
19、函数1、for(s=str;*s= &*s!=0;s+);strcpy(str,s);if(*s!=0)for(s=str+strlen(str)-1;*s= ;s-);*(s+1)=0;2、for(p=str;pstr+4;p+)for(q=p+1;qstr+5;q+)if(strcmp(*p,*q)=1)s=*p;*p=*q;*q=s;第10章实验一 结构体1、在VC中的运行结果(以下同)The size of int is 4.The size of char is 1.The size of float is 4.The size of struct Student is 40.The
20、 size of s is 40.2、for (i = 0; i N; i+) if (xuehao = stui.number) l = i;break;if (l != -1) printf(%ld %s %c %6.2f %6.2f %6.2f %6.2fn,stul.number, stul.name, stul.sex, stui.score0 , stui.score1 , stui.score2, stul.aver);elseprintf(The searched student is not existent!n);3、 #include #define N 10struct
21、 Book char name40; float price;void sort(struct Book b, int n);void main( ) struct Book bN; int i;printf(Please input %d books information as follow: n, N);printf(Name Pricen);for (i = 0; i N; i+) printf(Please input %dth books information: n, i+1);scanf(%s%f, bi.name, &bi.price);sort(b, N);for (i =
22、 0; i N; i+) printf(%s %6.2fn, bi.name, bi.price);void sort(struct Book b, int n) struct Book k; int i, j; for (i = 0; i N-1; i+) for(j = 0; j bj+1.price) k = bj; bj = bj+1; bj+1 = k; 实验二 共用体1、The size of int is 4.The size of char is 1.The size of float is 4.The size of union U_value is 4.The size of u1 is 4.The value of u1.iv is 100The value of u1.cv is dThe value of u1.fv is 0.000000The size of u2 is 4.The value of u2.iv is -858993599The value of u2.cv is AThe value of u2.fv is -107373064.000000The size of u3 is 4.The value of u3.iv is 1078523331The value of u3.cv is
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2