/*非常感谢度娘以及各位网上C语言高手的支持,才能让敝人完成此文档的整理。
本文档集合了本人、度娘、众网友的力量,其中代码的正确率约为90%(不正确的有标注)。
为回报度娘及众网友的帮助,本文档免费下载。 */
/*配“电子科技大学出版社(周信东主编)”的C语言程序设计实验*/
/*努力吧,骚年以及学妹们!*/
/*整理ed by 口玉刀一 of GUET.*/
}
(2)
#include ===================== 实验一 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=%d\\n,s=%d\ } 2.------------------------------- 输入一个数字求他的平方 #include 3.-------------------------------- (1) #include 1 ================================= 实验二 数据类型、运算符和表达式 ================================= 1. (1)-------------------------------------------------- //总觉得打印结果怪怪的,DO YOU THINK SO? main() { char s1='3',s2='4',s3='5'; int c1=101,c2=102; printf(\"3%c\4%c\5%c\\n\//3%c为输出3和%c printf(\"s1=%d\s2=%d\s3=%d\\n\注意哦,s1,s2,s3是char!而%d:输入输出为整形 %ld 长整型 %hd短整型 %hu无符号整形 %u %lu%s:输入输出为字符串 %c字符%f:输入输出为浮点型 %lf双精度浮点型 printf(\"c1=%d\~%c\\n\//换码符'\',表示水平制表位(horizeontal tab),它的作用是将光标移到最接近8的倍数的位置 printf(\"c2=%d\~%c\\n\注意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\ } (3) main() { int i,j,p,q; i=3;j=6; p=i++; q=--j; printf(\"%d,%d,%d,%d\\n\ p=i--+3; q=++j-4; printf(\"%d,%d,%d,%d\ } int a=3,b=6,t; t=MM/(3+6); printf(\"%d%d\ } //原来printf语句打成了print,printf语句中少了一个%d. (2) #include //注意分号,%f转义 (4) main() { int m=15,n=9,s; m*=3+2; printf(\"%d\ n+=n-=n*=n; printf(\"%d\ printf(\"%d\\n\ printf(\"%d\\n\ } (3) main() { int m=8,n=5; printf(\"%d\ printf(\"%d,%d,%d\} 3.---------------------------------------------------- main() { int a=9,b=5; a+=b; b=a-b; a-=b; printf(\"%d%d\ } 4.---------------------------------------------------- //4.输入a,b,c三个变量,计算数学公式(a*b)/c main() { 2 2.---------------------------------------------------- (1)#include int a,b,d; float s,c; scanf(\"%d%d%f\ d=a*b; s=d/c; printf(\"%f\ } ================================= 实验三 顺序结构和输入输出方法 ================================= 1.--------------------------------------------------- (1)main() { int m=345,t; float n=2.56; t=2.56*100; printf(\"m=%d,n*100=%d\\n\ } (2) main() { int a,b,c,s,v; scanf(\"%d%d%d\ s=a*b; v=a*b*c; printf(\"%d,%d,%d\\n\ printf(\"s=%d,v=%d\\n\ } 2.---------------------------------------------------- (1) main() { int m,n; float x=3.5,y=6.2; scanf(\"%d,%d\ printf(\"%6d,%6d\/*每个数的输出宽度为6,两个数之间逗号建个。*/ printf(\"x=%7.2f,y=%7.2f\/*个数的输出宽度7,小数位2*/ } (2) main() { int a,b; long m,n; float p,q; scanf(\"a=%d,b=%d\\n%f,%f\ scanf(\"%ld\ scanf(\"%ld\ printf(\"a=%d,b=%d\\n m=%ld,n=%ld\\n\ printf(\"p=%f,q=%f\\n\ } //打印结果有误!! 3.---------------------------------------------------- main() { int i,j,k,ss,m=1,n=1; scanf(\"%d%d\ m+=i++; n*=--j; ss=(k=m+2,n-5,k+n); printf(\"%d\ } 4.---------------------------------------------------- #include 3 c1=getchar(); c2=getchar(); putchar(c1); putchar(c2); printf(\"c1=%c,c2=%c\ printf(\"%d.%d\\n\ } 5.--------------------------------------------------- #include 6.--------------------------------------------------- #include 7.-------------------------------------------------- #include ================================= 实验四 逻辑运算判断选取控制 ================================= 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\ if (x>-5&&x<0) y=x ; if (x==0) y=x-1 ; if(x>0&&x<10) y=x+1; printf (\"%d\} (2) main() { int x,y; scanf(\"%d\ if((x>-5)&&(x<0)) y=x; 4 else if (x==0) y=x-1; else if(x>0&&x<10) y=x+1; printf(\"%d\} } else if (a == b || b == c || a == c) printf(\"等腰三角形\\n\"); else printf(\"不等边三角形\\n\"); return 0; 3.---------------------------------------------------- main ( ) { int a,M; scanf(\"%d\ M=a>0?a:-a; printf(\"abs(a)=%d\} 4.---------------------------------------------------- main() { int c,t,m; printf(\"input the number of coat and trousers your want buy:\\n\"); scanf(\"%d%d\ 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\ } 6.-------------------------------------------------- #include\"stdio.h\" main() { long a; scanf(\"%ld\ if(a%5==0) if(a%7==0) printf(\"yes\\n|\"); else printf(\"no\\n\"); } 5.--------------------------------------------------- #include main(void) { int a, b, c; scanf(\"%d%d%d\ if(a + b <= c || a + c <= b || b + c <= a) printf(\"不构成三角形\\n\"); else if (a == b && b == c) printf(\"等边三角形\\n\"); 5 ================================= 实验五 循环结构 ================================= 1.--------------------------------------------------- #include\"stdio.h\" main() { int n; while(1) { printf(\"Enter a number:\"); scanf(\"%d\ if(n%2==1) { printf(\"I said\"); continue; } break; } printf(\"Thanks.Ineeded that!\"); } 2.---------------------------------------------------- #include 3.---------------------------------------------------- #include 4.---------------------------------------------------- #include { int a,b,c; printf(\" * 1 2 3 4 5 6 7 8 9\\n\"); printf(\" -----------------------------\\n\"); for(a=1;a<=9;a++) {printf(\"%3d\ for(b=1;b<=a;b++) {c=a*b; printf(\"%3d\ printf(\"\\n\");} } 5.-------------------------------------------------- #include 6.-------------------------------------------------------- #include { 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;jif(a==6) printf(\"%d\ else printf(\"+%d\ } s+=a; } printf(\"=%d\\n\ } ================================= 实验六 数组 ================================= 1.--------------------------------------------------- #include ++grade[mumber]; } printf(\"\\n\\nresult of search\\n\"); printf(\"---------------\\n\"); for(i=1;i<=5;++i) printf(\"%4d %d\\n\ } 2.---------------------------------------------------- #include\"stdio.h\" main() { int n,k,xx[20]; int i,j,t; printf(\"\\nPlease enter a number\"); scanf(\"%d\ printf(\"\\nPlease enter %d numbers:\ for(i=0;i for(i=0;i 3.---------------------------------------------------- #include i,a[100]={90,180,270,380,590,530,140,750,380},b[6]; for(i=0;i<6;i++) b[i]=0; for(i=0;i<9;i++) { switch(a[i]/100) { case 0:b[0]++;break; case 1:b[1]++;break; case 2:b[2]++;break; case 3:b[3]++;break; case 4:b[4]++;break; default :b[5]++;break; } } printf(\"The result is:\"); for(i=0;i<6;i++) printf(\"%d\ printf(\"\\n\"); } for(i=0;i<10;i++) { for(j=i+1;j<10;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } printf(\"%d \ }