实验二 选择结构程序设计
(1)
#include <stdio.h> int main() { char a,b,c,d,e; a = getchar(); b = getchar(); c = getchar(); d = getchar(); e = getchar(); putchar(a); putchar(b); putchar(c); putchar(d); putchar(e); putchar("\n"); return 0; }
(2)
#include<stdio.h> int main() { int year,month,days; month = 2; printf("input the year:"); scanf("%d",&year); if((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0 && year % 100 == 0) { days=29; printf("year:"); printf("%d\n",year); printf("month:"); printf("%d\n",month); printf("days:"); printf("%d\n",days); } else{ days=28; printf("year:"); printf("%d\n",year); printf("month:"); printf("%d\n",month); printf("days:"); printf("%d\n",days); } return 0; }
(3)
#include<stdio.h> #include<math.h> int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); if((a+b)>c && (a+c)>b && (b+c)>a) { printf("能构成三角形\n"); if((pow(a,2)+pow(b,2)==pow(c,2)) || (pow(a,2)+pow(c,2)==pow(b,2)) || (pow(b,2)+pow(c,2)==pow(a,2))) { printf("直角三角形\n"); } if((a==b)||(a==c)||(b==c)) { if((a==b)&&(a==c)&&(b==c)) { printf("等边三角形\n"); } else { printf("等腰三角形\n"); } } } else { printf("不能构成三角形\n"); } return 0; }
(4)
#include<stdio.h> int main() { double a,rate,tax,profit; printf("input a:"); scanf("%lf",&a); if(a<500) { rate = 0.00; tax = a * rate; profit = a - tax; } else if(a>=500 && a<1000) { rate = 0.05; tax = a * rate; profit = a - tax; } else if(a>=1000 && a<2000) { rate = 0.08; tax = a * rate; profit = a - tax; } else if(a>=2000 && a<5000) { rate = 0.10; tax = a * rate; profit = a - tax; } else if(a>5000) { rate = 0.15; tax = a * rate; profit = a - tax; } printf("%lf\n%lf\n%lf\n",rate,tax,profit); return 0; }
#include<stdio.h> int main() { int c; double a,rate,tax,profit; printf("please enter a:"); scanf("%lf",&a); if(a>=5000) c=10; else c=a/500; switch(c) { case 0: rate=0.00;break; case 1: rate=0.05;break; case 2: case 3: rate=0.08;break; case 4: case 5: case 6: case 7: case 8: case 9: rate=0.10;break; case 10: rate=0.15;break; } tax = a * rate; profit = a - tax; printf("%lf\n%lf\n%lf\n",rate,tax,profit); return 0; }