您的当前位置:首页正文

“电子科技大学出版社(周信东主编)”的C语言程序设计实验-整理代码1_-9解读

2021-10-02 来源:年旅网
-前言-

/*非常感谢度娘以及各位网上C语言高手的支持,才能让敝人完成此文档的整理。

本文档集合了本人、度娘、众网友的力量,其中代码的正确率约为90%(不正确的有标注)。

为回报度娘及众网友的帮助,本文档免费下载。 */

/*配“电子科技大学出版社(周信东主编)”的C语言程序设计实验*/

/*努力吧,骚年以及学妹们!*/

/*整理ed by 口玉刀一 of GUET.*/

}

(2)

#include main() { int v; int a,b,c; //a,b,c aer sides,v is volume of cube a=3;b=3;c=5; v=a*b*c; printf(\"v=%d\\n\}

===================== 实验一 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 main() { int r,s; scanf(\"%d\ s=r*r; printf(\"s=%d\\n\ }

3.-------------------------------- (1)

#include main() { printf(\" *\\n\"); printf(\"***\\n\"); printf(\" *\\n\");

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 main() { int m; double x; scanf(\"%d\ x=3.14*m*m; printf(\"%f\ }

//注意分号,%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 #define MM 40 main() {

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 main() { char c1,c2;

3

c1=getchar(); c2=getchar(); putchar(c1); putchar(c2);

printf(\"c1=%c,c2=%c\ printf(\"%d.%d\\n\

}

5.--------------------------------------------------- #include main() { int a=790,b,c; b=790/60; c=790%60; printf(\"%d小时%d分钟\ }

6.--------------------------------------------------- #include main() { int a,b,c,d; scanf(\"%d%d%d\ d=a; a=c; c=b; b=d; printf(\"%d,%d,%d\\n\ }

7.--------------------------------------------------

#include main() { double a; scanf(\"%lf\ printf(\"%.2f\\n\ }

================================= 实验四 逻辑运算判断选取控制

=================================

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 int

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 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\ } putchar ('\\n') ; }

3.---------------------------------------------------- #include 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 main()

{ 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 main() { double e=2,w=0.000001,t; double n=1,s=1; t=1/w; while(s6

6.-------------------------------------------------------- #include 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;jif(a==6)

printf(\"%d\ else

printf(\"+%d\ } s+=a; }

printf(\"=%d\\n\ }

================================= 实验六 数组

=================================

1.--------------------------------------------------- #include main() { int grade[6]; int i,mumber; for(i=1;i<=5;++i) grade[i]=0; printf(\"enter your number\\n\"); for(i=1;i<=20;++i) { scanf(\"%d\ if(!mumber) break;

++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;iprintf(\"%\\nPlease enter another number:\"); scanf(\"%d\ for(i=0;iprintf(\"\\nAfter moving:\\n\");

for(i=0;i7

3.---------------------------------------------------- #include main() { int

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 \ }

printf(\"\\ninput number:\\n\"); scanf(\"%d\ for(i=0;i<10;i++) if(n=i;j--) a[j+1]=a[j]; break; }

a[i]=n;

for(i=0;i<=10;i++) printf(\"%d \ printf(\"\\n\"); }

5.--------------------------------------------------- #include int main() { char a[100], b[100]; int i=0, j=0, n=0; gets(a); gets(b); while(a[j]!=0) { while((a[j+i]==b[i])&&b[i]!=0) i++; if(b[i]==0) n++; i=0; j++; }

8

4.----------------------------------------------------

#include main() {

int i,j,t,p,q,s,n,a[11]={6,3,42,23,35,71,98,67,56,38};

}

printf(\"%d\

strcpy(str2+strlen(str2),str1); printf(\"%s\}

3.----------------------------------------------------

//本代码有些奇怪,在有的vc上可以执行,有的却不行。 #include \"stdio.h\" main() { int i,pos;

char str[]=\"This is a program\"; for(i=0;str[i]!='\\0';i++) { if (str[i]=='a') {pos=i;break;} }

printf(\"The position is %d\\n\}

4.---------------------------------------------------- #include \"stdio.h\" main() { int i,pos;

char str[]=\"This is a program\"; for(i=0;str[i]!='\\0';i++) { if (str[i]=='a') {pos=i;break;} }

printf(\"The position is %d\\n\}

程序运行结果如下图所示:

================================= 实验七 字符处理

=================================

1.---------------------------------------------------

该程序的功能是:输入字符串,删除其中的数字,保留剩下的字符。 #include main() { char a[40],b[40]; int i,j; printf(\" Enter the string: \") ; scanf(\"%s\ i=j=0; while (a[i]!='\\0') { if (! (a[i]>='0'&&a[i]<='9') ) { b[j]=a[i]; j++; } ++i; } b[j]='\\0'; printf (\" %s\}

5.--------------------------------------------------- #include \"stdio.h\" main() {

int i,j,k=0; char s[255];

while((s[k]=getchar())!='@') k++; for(j=0;s[j]!='@';j++)

9

2.---------------------------------------------------- #include \"string.h\" #include \"stdio.h\" main() { char str1[50]=\"every one!\"; char str2[50]=\"hello \";

{ if(s[j]<='Z'&&s[j]>='A') { s[j]+=32; } if(s[j]<='w'&&s[j]>='a') s[j]+=3; else if(s[j]=='x'||s[j]=='y'||s[j]=='z') s[j]-=23; }

printf(\"译码后结果:\"); for(i=0;s[i]!='@';i++) printf(\"%c\printf(\"\\n\"); getchar(); }

===================== 实验八 函数

=====================

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(\"%d\\n\} 8.2

#include int is_prime(int m) { int i; if(m==1) return 0; for(i=2;ivoid main() { int n,c; scanf(\"%d\ c=is_prime(n);

10

6.--------------------------------------------------------

#include \"stdio.h\" main() {

int i=0;

char str1[255],str2[255]; printf(\"\\n Input string 1:\"); scanf(\"%s\

printf(\"\\n Input string 2:\"); scanf(\"%s\

while(1) { if(str1[i]==str2[i]) { i++; continue; } else break; }

printf(\"\\n%d\\n\}

if(c==1) printf(\"prime\\n\"); else printf(\"not prime\\n\");

}

8.3

#include int main() {

int m,n,i,sum=1,t;

printf(\"请输入m的值:\\n\"); scanf(\"%d\

printf(\"请输入n的值:\\n\"); scanf(\"%d\if(mfor(i=m;i>=m-n+1;i--) {

sum*=i; }

for(i=1;i<=n;i++) {

sum/=i; }

printf(\"值为:%d\\n\}

===================== 实验九 指针

=====================

1.

//在每句for (j = 0; j < 12; j++)前都加上p = a;这句//将if (j % 4 == 0) 改为 if ((j + 1) % 4 == 0) //调试后的程序是:

#include

void main( ) {

int j, k, a[12], *p;

p = a;

for (j = 0; j < 12; j++) scanf(\"%d\ p = a;

for (j = 0; j < 12; j++) {

printf(\"%d\ if ((j+1) % 4 == 0) printf(\" \\n\");

11

} }

2.

#include 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\ 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,%d\\n\}

void swap(int *p1,int *p2) { int p; p=*p1;*p1=*p2;*p2=p; }

3. main() { int a[10]; 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]=%d\\n\

}

4.#include

int table[10];

void lookup(int *t,int *a,int n) { int k; *a=t[0]; for(k=1;kt[k]) *a=t[k]; }

void main() { int k,min,*p=&min; for(k=0;k<10;k++) scanf(\"%d\ lookup(table,&min,10); printf(\"min=%d\\n\}

5.#include

void main() { int a[5][5]={0},*p[5],i,j; for(i=0;i<5;i++)p[i]=&a[i][0]; for(i=0;i<5;i++) { *(p[i]+i)=1; *(p[i]+5-(i+1))=1; } for(i=0;i<5;i++) { for(j=0;j<5;j++) printf(\"%2d\

12

}

}

putchar('\\n');

注意:&a[i][0]换成&a[0][i]之后结果不一样: #include void main() {

}

int a[5][5]={0},*p[5],i,j;

for(i=0;i<5;i++)p[i]=&a[0][i]; for(i=0;i<5;i++) { *(p[i]+i)=1; *(p[i]+5-(i+1))=1; }

for(i=0;i<5;i++) { for(j=0;j<5;j++)

printf(\"%2d\

}

putchar('\\n');

13

因篇幅问题不能全部显示,请点此查看更多更全内容