大学C语言课本课后习题相应答案及详细解答

更新时间:2024-01-20 00:22:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

5-1

#include\

void main()

{

char c1,c2;

printf(\ c1=getchar();

if(c1>='a'&&c1<='z') c2=c1-32;

else if(c1>='A'&&c1<='Z') c2=c1+32; else c2=c1;

printf(\

}

5-2

#include\void main() { char c; printf(\ c=getchar(); if(c>='A'&&c<='Z') if(c=='A') printf(\没有前面的字母!\ else if(c=='Z') printf(\没有后面的字母!!\ else printf(\前面的字母=%c,后面的字母=%c\ printf(\}

5-3 #include\void main() { int s; char g; printf(\ scanf(\ if(s>=0&&s<=100){ switch(s/10){ case 9: case 10: g='A';break; case 8: g='B';break; case 7: g='C';break; case 6: g='D';break; default: g='E'; } printf(\ } else printf(\成绩不在百分制范围内!\\n\}

1

5-4

#include\void main() { int y,m,d,dok; int yy,mm,dd; printf(\ scanf(\ switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(d>0&&d<=31) dok=4; else dok=0; break; case 4: case 6: case 9: case 11: if(d>0&&d<=30) dok=3; else dok=0; break; case 2: if(y%4==0&&y0!=0||y@0==0) if(d>0&&d<=29) dok=2; else dok=0; else if(d>0&&d<=28) dok=1; else dok=0; break; default: dok=0; }

if(dok==0) printf(\月份或日期不对!\\n\ else{ switch(dok){ case 1: if(d==28){yy=y;dd=1;mm=m+1;} else{yy=y;dd=d+1;mm=m;} break; case

2:if(d==29){yy=y;dd=1;mm=m+1;} else{yy=y;dd=d+1;mm=m;}

break; case

3:if(d==30){yy=y;dd=1;mm=m+1;} else{yy=y;dd=d+1;mm=m;} break; case 4:if(d==31)

if(m==12){yy=y+1;dd=1;mm=1;} else {yy=y;dd=1;mm=m+1;} else{yy=y;dd=d+1;mm=m;} break; }

printf(\年%d月%d日\\n\ } }

5-5

#include\void main() { int a,b,c,t; printf(\ scanf(\ if(a+b>c&&b+c>a&&a+c>b){ if(a>b){t=a;a=b;b=t;} if(a>c){t=a;a=c;c=t;} if(b>c){t=b;b=c;c=t;} if(a==b&&b==c) printf(\等边三角形.\ else if(a==b||b==c||a==c) printf(\等腰角形.\ else if(c*c==a*a+b*b) printf(\直角三角形.\ else printf(\任意三角形.\ printf(\ } else printf(\不能构成三角形!\\n\}

2

6-1

#include\void main() { int a,b,m,n,k; printf(\ scanf(\ a=m; b=n; k=a%b; while(k){ a=b; b=k; k=a%b; } printf(\}

6-2

#include\void main() { int n; int i,j,s; printf(\ scanf(\ for(i=1;i<=n;i++){ s=0;

for(j=1;j

6-3

#include\#include\void main() { double x,y,zd,zx,x1,x2; zd=zx=50; for(x=0;x<=2;x=x+0.2){

y=2*pow(x,3)-3*pow(x,4)+6*pow(x,5)-4*x+50; if(y>zd){ zd=y;x1=x;} if(y

6-4

#include\void main() { double x,sum=0,a,b,c; int i; printf(\ scanf(\ a=x; b=1.0; c=1.0; for(i=1;i<=10;i++){ sum=sum+a/(b*c); a=a*x*x; b=b+2; c=c*i; } printf(\}

3

7-1

/*选择排序*/ #include\void main() { int i,j,n,k,temp; int a[10]; printf(\ scanf(\ printf(\ for(i=0;i

for(j=i+1;j

7-2

#include\void main() { int a[3][3]; int i,j,s=1; printf(\ for(i=0;i<3;i++) for(j=0;j<3;j++){ scanf(\ if(i==j) s=s*a[i][j]; } printf(\ }

7-3

/*杨辉三角*/ #include\void main() { int x[7][7]; int i,j; for(i=0;i<7;i++) { x[i][0]=1; x[i][i]=1; } for(i=2;i<7;i++) for(j=1;j

7-4

#include #include void main() { char str[21]; int i,j; printf(\ gets(str); for(i=0,j=strlen(str)-1;i<=j;i++,j--) if(str[i]!=str[j]) break; if(i>j) printf(\是回文\\n\ else printf(\不是回文\\n\}

4

7-5

/*输入一维数组的10个元素,并将最小值与第1个数交换,最大值与最后一个数交换,然后输出交换后的结果*/ #include void main() { int a[10],i,zx,zd; printf(\ zx=zd=0; for(i=0;i<10;i++){ scanf(\ if(a[i]a[zd]) zd=i; } if(zx!=0){ int t; t=a[0]; a[0]=a[zx]; a[zx]=t; } if(zd!=9){ int t; t=a[9]; a[9]=a[zd]; a[zd]=t; } for(i=0;i<10;i++) printf(\ printf(\}

8-2

#include\

double xexp(double x,int n) { double c; if(n==0) c=1.0; else c=x*xexp(x,n-1); return c; }

void main() { int n; double x; printf(\ scanf(\ printf(\ scanf(\ printf(\}

8-3

#include\

int find(int x[],int n,int y) { int i,pos=-1; for(i=0;i

void main() { int a[10]={11,1,13,15,18,7,19,27,3,8}; int i,y,pos;

for(i=0;i<10;i++) printf(\ printf(\ scanf(\ pos=find(a,10,y); if(pos==-1) printf(\ else printf(\}

5

8-1

#include\

#include\ //getch()

#include\ //srand(),rand(),system(\#include\ //time() void main() { void init(int a[][5],int m,int n); void input(int a[][5],int m,int n); void output(int a[][5],int m,int n); int min(int b[][5],int m,int n); int a[5][5],ch='0'; while(1){

system(\ //清屏 printf(\初始化 2.键盘输入 0.结束程序:\ ch=getch(); //头文件conio.h printf(\ if(ch=='0') break; else if(ch=='1'){ init(a,5,5); output(a,5,5); } else if(ch=='2'){ input(a,5,5); output(a,5,5); } else printf(\

printf(\ printf(\按任意键继续!\\n\ getch(); } }

void init(int a[][5],int m,int n) { int i,j; srand(time(0)); //time(0)表示以当前的时间做种子,增加每次运行的随机性 for(i=0;i<5;i++) for(j=0;j<5;j++) a[i][j]=rand()0; //随机数范围:0~32767,将它控制在0~99的范围

}

void input(int a[][5],int m,int n) { int i,j; printf(\ for(i=0;i

void output(int a[][5],int m,int n) { int i,j; printf(\ for(i=0;i<5;i++){ for(j=0;j<5;j++) printf(\ printf(\ } }

int min(int b[][5],int m,int n) { int i,j,zx; zx=b[0][0]; for(i=0;i

6

8-4

#include\float pave,nave;

void saver(float a[],int n) { int i,z,f; float psum,nsum; psum=nsum=0.0; z=f=0; for(i=0;i0) {psum=psum+a[i];f++;} else continue; pave=(z!=0?psum/z:0.0); nave=(f!=0?nsum/f:0.0); }

void main() { float a[10]={1.0,11.0,3.0,-1.5,-5.5,-2}; saver(a,10); printf(\}

8-5

#include\#include\void p1(int a,int b) {

printf(\two equal roots:%8.4f\\n\}

void p2(int a,int b, int disc) { float x1,x2; x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf(\distant real roots:%8.4f and %8.4f\\n\}

void p3(int a,int b, int disc) { float real,image; real=-b/(2*a); image=sqrt(-disc)/(2*a); printf(\ printf(\ printf(\}

void main()

{ int a,b,c,disc; printf(\ scanf(\ disc=b*b-4*a*c; if(fabs(disc)<=1e-6) p1(a,b); else if(disc>1e-6) p2(a,b,disc); else p3(a,b,disc); }

7

8-6

#include\#include\#include\#include\void main() { void printaverage(int score[][5],int m,int n); void printname(int score[][5],int m,int n); int score[10][5]; int i,j; srand(time(0)); for(i=0;i<10;i++) for(j=0;j<5;j++) score[i][j]=50+rand()P+1; printf(\ printf(\ 1 2 3 4 5\\n\ printf(\ for(i=0;i<10;i++){ printf(\ for(j=0;j<5;j++) printf(\ printf(\ } printaverage(score,10,5); printname(score,10,5); }

void printaverage(int score[][5],int m,int n) { int i,j,sum; printf(\ for(i=0;i

void printname(int score[][5],int m,int n)

{ int i,j,max0,row0; for(j=0;j<5;j++){ max0=score[0][j]; for(i=0;i<10;i++) if(score[i][j]>max0){ max0=score[i][j]; row0=i; } printf(\%d,max score=%d,student:No.%d\\n\ } }

8

本文来源:https://www.bwwdw.com/article/zvwo.html

Top