当前位置:文档之家› C语言课后习题答案

C语言课后习题答案

#include <stdio.h>
int main() { char c;
int main()
C 语言程序设计 课后习题 参考程序 { double x,y; }
由宋飞、张斌编写 return 0;
(西安邮电学院)
第3页
printf("请输入 x:"); scanf("%lf",&x);
习题 4.5 计算器程序。用户输入运算数和四则运算符,输出运算结 果。
由宋飞、张斌编写
(西安邮电学院)
第2页
printf("请输入一个字母:"); scanf("%c",&c);
printf("转换后:%c\n",c+32);
printf(“请输入 a,b,c,d :”); scanf(“%d,%d,%d,%d”,&a,&b,&c,&d); }
return 0;
average =(a+b+c+d)/4.0;
printf(“请输入运算符(+ - * /):”); scanf(“%c”,&t);
printf(“请输入 a,b:”); scanf(“%d,%d”,&a,&b);
printf("y=%lf\n",y); switch (t) return 0; } { case‘+’ : res=a+b;break; case‘-‘ : res=a-b;break; 习题 4.4 用条件表达式的嵌套来完成此题:学习成绩≥90 分的同 学用 A 表示,70~89 分之间的用 B 表示,60~69 分之间用 C 表示,60 分以下的用 D 表示。 printf(“结果:%f”,res); #include <stdio.h> return 0; int main() { int mark; char res; 习题 4.6 企业发放的奖金根据利润提成。利润(I)低于或等于 10 万元时, 奖金可提成 10%; 利润高于 10 万元, 低于 20 万元时, printf(“请输入成绩:”); scanf(“%d”,&mark); 低于 10 万元的部分按 10%提成,高于 10 万元的部分,可提成 7.5%;20 万到 40 万之间时,高于 20 万的部分,可提成 5%; 40 万到 60 万之间时,高于 40 万的部分,可提成 3%;60 万 if(mark>=90) res=’A’; else if(mark>=70) res=’B’; else if(mark>=60) res=’C’; else res=’D’; #include <stdio.h> printf(“结果 : %c\n”,res); int main() 到 100 万之间时,高于 60 万的部分可提成 1.5%,高于 100 万时,超过 100 万的部分按 1%提成。从键盘输入当月的利润, 求应发放的奖金总数。 } } case‘*’ : res=a*b;break; case‘/’ : res=(float)a/b;break;
/* 多个 if … else 嵌套 if(x>=0&&x<20) y=5*x+11; else if(x<40)
*/ #include <stdio.h>
int main() { int a,b; float res; char t;
y=sin(x)+cos(x); else if(x<60) exp(x)-1; else if(x<80) y=log(x+1); else y=0;
e 1
1 1 1 1! 2! n!
int main() { int n,i,j,s=0;
int main() { int n=1; float e=1,t=1,s=1.0;
printf("请输入 n :\n"); scanf("%d",&n); while(fabs(t)>=1e-6) { for(i=1;i<=n;i++) for(j=1;j<=i;j++) s+=j; e+=t; n++; s*=n; t=1/s; printf("s=%d\n",s); }
}
第五章 循环结构程序设计
习题 5.1 编写一个程序,计算 -32768~+32768 之间的任意整数 (由键盘输入)中个位奇数的平方和。 }
else printf("X 是偶数\n");
return 0;
习题 5.3 #include <stdio.h> 鸡兔问题:鸡兔共 30 只,脚共有 90 个。编写一个程序, 求鸡、兔各有多少只。 int main() { int n,ge,shi,bai,qian,wan,sum=0; int main() printf("请输入一个数:\n"); scanf("%d",&n); { int i,j; #include <stdio.h>
ge=n%10; shi=n/10%10; bai=n/100%10; qian=n/1000%10; wan=n/1000;
/* 分离各位数 */
/* 枚举法(穷举法) */ for(i=0;i<=30;i++) { j=30-i; if(2*i+4*i==90) printf("鸡:%d\n 兔:%d\n",i,j);
其中,S=(a+b+c)/2。
return 0; }
#include <math.h> #include <stdio.h>
习题 1.6 编写一个 C 语言程序, 从键盘输入 x,y,z 三个变量, 并输 出其中的最小值。
int main() { float a,b,c,area,s;
#include <stdio.h>
printf("请输入 a,b,c:"); scanf("%f,%f,%f",&a,&b,&c);
int main() { /* 定义整型变量 */ int x,y,z,min; printf("面积:%f\n",area); /* 输入*/ printf(“请输入 x,y,x:”); scanf(“%d,%d,%d”,&x,&y,&z); } return 0; s =(a+b+c)/2.0; area=sqrt(s*(s-a)*(s-b)*(s-c));
int main() { int a,b,c,d; int t; /* 临时变量 */
int main() { int x,y,z,num;
printf(“请输入 a,b,c,d:”); scanf(“%d,%d,%d,%d”,&a,&b,&c,&d);
if(a>b) {t=a;a=b;b=t;} /*数据交换方法 */ printf("请输入一个数:"); scanf("%d",&num); if(a>c) {t=a;a=c;c=t;} if(a>d) {t=a;a=d;d=t;} if(b>c) {t=b;b=c;c=t;} /* 分离个位,十位,百位的方法 */ x=num%10; y=(num/10)%10; z=(num/100)%10; printf(“结果:%d,%d,%d,%d\n”,a,b,c,d); if(b>d) {t=b;b=d;d=t;} if(c>d) {t=c;c=d;d=t;}
printf("个:%d\n 十:%d\n 百:%d\n",x,y,z); } return 0; }
return 0;
习题 4.3 编程序按下式计算 y 的值,x 的值由键盘输入。
习题 3.7 从键盘上输入一个大写字母,并将其转换成小写字母并输 出。
#include <stdio.h>
(0 x 20) 5 x 11 (20 x 40) sin x cos x x (40 x 60) y e 1 ln ( x 1) (60 x 80) 0 其他值
习题 3.4 编写程序,输入三角形的 3 个边长 a,b,c,求三角形的面 积 area。
int main() { printf(“***************\n”); printf(“How are you !\n”); printf(“***************\n”);
area S ( S a)( S b)( S c)
/* 判断最小值 */ min=x; if(y<min)min=y; if(z<min)min=z;
习题 3.5 编写程序,输入四个数,并求出它们的平均值。
#include <stdio.h>
C 语言程序设计 课后习题 参考程序 int main() { int a,b,c,d; float average;
if(ge%2==1) sum+=ge*ge; if(shi%2==1) sum+=shi*shi; if(bai%2==1) sum+=bai*bai;
}
return 0;
C 语言程序设计 课后习题 参考程序 }
由宋飞、张斌编写 习题 5.7
(西安邮电学院)
第5页
相关主题