C语言程序设计实验 答案.doc
-前言-/*非常感谢度娘以及各位网上C语言高手的支持,才能让敝人完成此文档的整理。本文档集合了本人、度娘、众网友的力量,其中代码的正确率约为90%(不正确的有标注)。为回报度娘及众网友的帮助,本文档免费下载。*/*配“电子科技大学出版社(周信东主编)”的C语言程序设计实验*/*努力吧,骚年以及学妹们!*/*整理ed by 口玉刀一 of GUET.*/=实验一 C语言程序初步=1.-已知a=5,b=4,c=6,求出s并换行#include"stdio.h" main() int a,b,c,s; a=5;b=4;c=6; s=(a+b+c)/3; printf("a=%d,b=%d,c=%dn,s=%d",a,b,c,s); 2.-输入一个数字求他的平方#include<stdio.h>main() int r,s;scanf("%d",&r);s=r*r;printf("s=%dn",s); 3.-(1)#include<stdio.h> main() printf(" *n"); printf("*n"); printf(" *n"); (2)#include<stdio.h> main() int v; int a,b,c; /a,b,c aer sides,v is volume of cubea=3;b=3;c=5; v=a*b*c; printf("v=%dn",v);=实验二 数据类型、运算符和表达式=1.(1)-/总觉得打印结果怪怪的,DO YOU THINK SO?main() char s1='3',s2='4',s3='5' int c1=101,c2=102; printf("3%ct4%ct5%cn",s1,s2,s3); /3%c为输出3和%c printf("s1=%dts2=%dts3=%dn",s1,s2,s3);/注意哦,s1,s2,s3是char!而%d:输入输出为整形 %ld 长整型 %hd短整型 %hu无符号整形 %u %lu%s:输入输出为字符串 %c字符%f:输入输出为浮点型 %lf双精度浮点型 printf("c1=%dt%cn",c1,c1); /换码符't',表示水平制表位(horizeontal tab),它的作用是将光标移到最接近8的倍数的位置 printf("c2=%dt%cn",c2,c2); /注意c1,c2的类型 (2)/运行结果为8.300000 %是求余数 先运算x-y,把结果转换为int型的有利于四则运算main() float x=8.3,y=4.2,s; int a=7; s=x+a%5*(int)(x-y)%2/3; printf("s=%f",s); (3)main() int i,j,p,q; i=3;j=6; p=i+;q=-j; printf("%d,%d,%d,%dn",i,j,p,q); p=i-+3;q=+j-4; printf("%d,%d,%d,%d",i,j,p,q); (4)main() int m=15,n=9,s; m*=3+2; printf("%d",m); n+=n-=n*=n; printf("%d",n); printf("%dn",(s=6*5,m+n+s); printf("%dn",s); 2.-(1)#include<stdio.h> #define MM 40 main() int a=3,b=6,t; t=MM/(3+6); printf("%d%d",t,MM); /原来printf语句打成了print,printf语句中少了一个%d.(2)#include<stdio.h> main() int m; double x; scanf("%d",&m); x=3.14*m*m; printf("%f",x); /注意分号,%f转义(3)main() int m=8,n=5; printf("%d",m*=m-n);printf("%d,%d,%d",m=m+1,n-1,m+n);3. -main() int a=9,b=5; a+=b; b=a-b; a-=b; printf("%d%d",a,b); 4. - /4.输入a,b,c三个变量,计算数学公式(a*b)/c main() int a,b,d; float s,c;scanf("%d%d%f",&a,&b,&c); d=a*b; s=d/c; printf("%f",s); =实验三 顺序结构和输入输出方法=1.-(1)main() int m=345,t; float n=2.56; t=2.56*100; printf("m=%d,n*100=%dn",m,t); (2)main() int a,b,c,s,v; scanf("%d%d%d",&a,&b,&c); s=a*b; v=a*b*c; printf("%d,%d,%dn",a,b,c); printf("s=%d,v=%dn",s,v); 2.-(1)main() int m,n; float x=3.5,y=6.2;scanf("%d,%d",&m,&n); printf("%6d,%6d",m,n); /*每个数的输出宽度为6,两个数之间逗号建个。*/ printf("x=%7.2f,y=%7.2f",x,y); /*个数的输出宽度7,小数位2*/ (2)main() int a,b; long m,n; float p,q; scanf("a=%d,b=%dn%f,%f",&a,&b,&p,&q); scanf("%ld",&m); scanf("%ld",&n); printf("a=%d,b=%dn m=%ld,n=%ldn",a,b,m,n); printf("p=%f,q=%fn",p,q); /打印结果有误!3.-main() int i,j,k,ss,m=1,n=1; scanf("%d%d",&i,&j); m+=i+; n*=-j; ss=(k=m+2,n-5,k+n); printf("%d",ss); 4.-#include<stdio.h> main() char c1,c2; c1=getchar();c2=getchar(); putchar(c1);putchar(c2); printf("c1=%c,c2=%c",c1,c2); printf("%d.%dn",c1,c2); 5. -#include<stdio.h> main() int a=790,b,c; b=790/60; c=790%60; printf("%d小时%d分钟",b,c); 6. -#include<stdio.h> main() int a,b,c,d; scanf("%d%d%d",&a,&b,&c); d=a; a=c; c=b; b=d; printf("%d,%d,%dn",a,b,c); 7. -#include<stdio.h> main() double a; scanf("%lf",&a); printf("%.2fn",a); =实验四 逻辑运算判断选取控制=1.-1. a. 错误原因分析:Switch分号错误 switch(a*a+b*b) case 3: case 1:y=a+b;break; case 0:y=b-a;break; b. 错误原因分析:switch语句使用错误,后跟括号。Default放在后边。 switch (a) case 10:y=a-b;break; case 11:x=a*b;break; default:x=a+b; c. 错误原因分析:没有输出语句。 switch(a+b) case 10:x=a+b;break; case 11:y=a-b;break; 2.-(1)main ( ) int x,y; scanf("%d",&x ); if (x>-5&&x<0) y=x ;if (x=0) y=x-1 ;if(x>0&&x<10) y=x+1;printf ("%d",y);(2)main() int x,y;scanf("%d",&x);if(x>-5)&&(x<0) y=x;else if (x=0) y=x-1; else if(x>0&&x<10) y=x+1; printf("%d",y);3.-main ( ) int a,M;scanf("%d",&a);M=a>0?a:-a;printf("abs(a)=%d",M);4.-main()int c,t,m;printf("input the number of coat and trousers your want buy:n");scanf("%d%d",&c,&t);if(c=t) if(c>=50) m=c*80; else m=c*90; else if(c>t)if(t>=50) m=t*80+(c-t)*60; else 90*t+(c-t)*60;else if(c>=50) 80*t+(t-c)*45;else m=c*90+(t-c)*45;printf("%d",m); 5. -#include<stdio.h>int main(void) int a, b, c;scanf("%d%d%d",&a,&b,&c); if(a + b <= c | a + c <= b | b + c <= a) printf("不构成三角形n");else if (a = b && b = c) printf("等边三角形n"); else if (a = b | b = c | a = c) printf("等腰三角形n"); else printf("不等边三角形n"); return 0; 6. -#include"stdio.h" main() long a;scanf("%ld",&a);if(a%5=0)if(a%7=0)printf("yesn|"); else printf("non"); =实验五 循环结构=1. -#include"stdio.h"main() int n; while(1) printf("Enter a number:"); scanf("%d",&n); if(n%2=1) printf("I said"); continue;break;printf("Thanks.Ineeded that!");2. -#include <stdio.h>main() int c;while ( (c=getchar () ) !='n')if ( (c<='X'&&c>='A'|c<='x'&&c>='a') )c += 2;else if (c='y'|c='Y'|c='z'|c='Z') c=c-24;printf("%c",c); putchar ('n') ;3. -#include<stdio.h>int main() int m, k; for( m = 1; m <= 4; m +) if ( m = 1 | m = 4 ) for( k = 1; k <= 6; k+) printf("*");printf("n"); else printf("* *n"); return 0; 4.-#include<stdio.h> main() int a,b,c; printf(" * 1 2 3 4 5 6 7 8 9n");printf(" -n"); for(a=1;a<=9;a+)printf("%3d",a); for(b=1;b<=a;b+)c=a*b;printf("%3d",c);printf("n");5. -#include<stdio.h>main()double e=2,w=0.000001,t;double n=1,s=1;t=1/w;while(s<t)s=s*(n+1);e=e+1/s;n+;printf("e=%lf,n=%dn",e,(int)(n-1);6. -#include <stdio.h> void main() int i,j,k=0,r,s=0,t=1000,a=0; for(i=2;i<=t;i+) a=0; r=0; for(j=1;j<i;j+) k=0; if(i%j=0) k=j; r+=k; if(r=i) a=r; if(a!=0) if(a=6) printf("%d",a); else printf("+%d",a); s+=a; printf("=%dn",s); =实验六 数组=1.-#include<stdio.h>main()int grade6;int i,mumber;for(i=1;i<=5;+i) gradei=0;printf("enter your numbern");for(i=1;i<=20;+i)scanf("%d",&mumber);if(!mumber) break;+grademumber;printf("nnresult of searchn");printf("-n");for(i=1;i<=5;+i)printf("%4d %dn",i,gradei);2.-#include"stdio.h"main() int n,k,xx20; int i,j,t; printf("nPlease enter a number"); scanf("%d",&n); printf("nPlease enter %d numbers:",n); for(i=0;i<n;i+) scanf("%d",&xxi); printf("%nPlease enter another number:"); scanf("%d",&k); for(i=0;i<k;i+) t=xx0; for(j=1;j<n;j+) xxj-1=xxj; xxn-1=t; printf("nAfter moving:n"); for(i=0;i<n;i+) printf("%3d",xxi); printf("n");3.-#include <stdio.h>main()int i,a100=90,180,270,380,590,530,140,750,380,b6; for(i=0;i<6;i+) bi=0;for(i=0;i<9;i+) switch(ai/100) case 0:b0+;break; case 1:b1+;break; case 2:b2+;break; case 3:b3+;break; case 4:b4+;break; default :b5+;break; printf("The result is:");for(i=0;i<6;i+)printf("%d",bi);printf("n");4.-#include <stdio.h>main() int i,j,t,p,q,s,n,a11=6,3,42,23,35,71,98,67,56,38; for(i=0;i<10;i+) for(j=i+1;j<10;j+) if(ai>aj) t=ai; ai=aj; aj=t; printf("%d ",ai); printf("ninput number:n"); scanf("%d",&n); for(i=0;i<10;i+) if(n<ai) for(j=9;j>=i;j-) aj+1=aj; break; ai=n; for(i=0;i<=10;i+) printf("%d ",ai); printf("n"); 5. -#include<stdio.h>int main() char a100, b100;int i=0, j=0, n=0;gets(a);gets(b);while(aj!=0)while(aj+i=bi)&&bi!=0) i+;if(bi=0) n+;i=0;j+;printf("%d", n);=实验七 字符处理=1.-该程序的功能是:输入字符串,删除其中的数字,保留剩下的字符。#include<stdio.h>main()char a40,b40;int i,j; printf(" Enter the string: ") ; scanf("%s",a) ; i=j=0; while (ai!='0') if (! (ai>='0'&&ai<='9') ) bj=ai; j+; +i; bj='0'printf (" %s",b) ;2.-#include "string.h"#include "stdio.h"main()char str150="every one!" char str250="hello " strcpy(str2+strlen(str2),str1); printf("%s",str2);3. -/本代码有些奇怪,在有的vc上可以执行,有的却不行。#include "stdio.h"main()int i,pos; char str="This is a program" for(i=0;stri!='0'i+)if (stri='a') pos=i;break; printf("The position is %dn",pos);4.-#include "stdio.h"main()int i,pos; char str="This is a program" for(i=0;stri!='0'i+)if (stri='a') pos=i;break; printf("The position is %dn",pos);程序运行结果如下图所示:5. -#include "stdio.h"main()int i,j,k=0;char s255;while(sk=getchar()!='') k+;for(j=0;sj!=''j+)if(sj<='Z'&&sj>='A') sj+=32;if(sj<='w'&&sj>='a') sj+=3;else if(sj='x'|sj='y'|sj='z')sj-=23;printf("译码后结果:");for(i=0;si!=''i+)printf("%c",si);printf("n");getchar();6. -#include "stdio.h"main()int i=0;char str1255,str2255;printf("n Input string 1:");scanf("%s",str1);printf("n Input string 2:");scanf("%s",str2);while(1)if(str1i=str2i) i+;continue;elsebreak; printf("n%dn",str1i-str2i);=实验八 函数=8.1?未成功int a=3,b=5;max(int a,int b)int c;c=a>b?a:b;return (c);main()extern int a,b;printf("%dn",max(a,b);8.2#include <stdio.h>int is_prime(int m)int i;if(m=1)return 0;for(i=2;i<m;i+) if(m%i=0)return 0;return 1;void main() int n,c;scanf("%d",&n); c=is_prime(n); if(c=1)printf("primen");elseprintf("not primen");8.3#include<stdio.h>int main()int m,n,i,sum=1,t;printf("请输入m的值:n");scanf("%d",&m);printf("请输入n的值:n");scanf("%d",&n);if(m<n)t=m;m=n;n=t;for(i=m;i>=m-n+1;i-)sum*=i;for(i=1;i<=n;i+)sum/=i;printf("值为:%dn",sum);=实验九 指针=1./在每句for (j = 0; j < 12; j+)前都加上p = a;这句/将if (j % 4 = 0) 改为 if (j + 1) % 4 = 0)/调试后的程序是:#include<stdio.h>void main( ) int j, k, a12, *p; p = a; for (j = 0; j < 12; j+) scanf("%d", p+); p = a; for (j = 0; j < 12; j+) printf("%d", *p+); if (j+1) % 4 = 0) printf(" n"); 2. #include<stdio.h>void main()void swap(int*p1,int*p2);int n1,n2,n3;int*p1,*p2,*p3;printf("input three interer n1,n2,n3:");scanf("%d%d%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2)swap(p1,p2);if(n1>n3)swap(p1,p3);if(n2>n3)swap(p2,p3);printf("Now,the order is :%d,%d,%dn",n1,n2,n3);void swap(int *p1,int *p2)int p;p=*p1;*p1=*p2;*p2=p;3. main()int a10;int *p,i;p=a;for(i=0;i<10;i+)*p=i;p+;p=a;for(i=0;i<10;i+)printf("n%d=%dn",i,*p+);4.#include <stdio.h>int table10;void lookup(int *t,int *a,int n) int k;*a=t0;for(k=1;k<n;k+)if(*a>tk) *a=tk;void main() int k,min,*p=&min;for(k=0;k<10;k+)scanf("%d",table+k);lookup(table,&min,10);printf("min=%dn",min);5.#include <stdio.h>void main()int a55=0,*p5,i,j;for(i=0;i<5;i+)pi=&