C语言选择结构序设计.docVIP

  • 9
  • 0
  • 约8.37千字
  • 约 6页
  • 2016-10-09 发布于广东
  • 举报
C语言选择结构序设计

3.3 实验 选择结构程序设计 一、实验目的 1.掌握关系运算符与表达式的使用。 2.掌握逻辑运算符与逻辑表达式的使用。 3.理解并掌握if语句和switch语句及它们的嵌套使用。 4.掌握选择结构程序设计中一些常用算法。 二、实验内容 1.输入一个正整数,判断它是奇数还是偶数,并输出判断结果。 #includestdio.h void main() { int a; scanf(%d,a); if(a%2==0) printf(even number); else printf(uneven number); getch(); } 2.输入和的值,按下列公式计算的值。 #includestdio.h void main() { float a,x,y; scanf(%f,%f,a,x); if(x=ax=-a) y=sqrt(a*a-x*x); else if(x==a||x==-a) y=0; else y=x-1; printf(y=%f\n,y); getch(); } 3.输入一个百分制成绩,输出其相应的五级成绩。百分制与五级制成绩之间的换算关系为:90分以上为”A”,80~89分为”B”,70~79分为”C”,60~69分为”D”,60分以下为”E”。 要求:(1)用if语句编程;(2)用switch语句编程 (1) #includestdio.h void main() { int score; printf(\nPlease input the score()0~100:); scanf(%d,score); if(score=90) printf(A\n); else if(score=80) printf(B\n); else if(score=70) printf(C\n); else if(score=60) printf(D\n); else printf(E\n); getch(); } (2) #includestdio.h void main() { int score; printf(\nPlease input the score(0~100):); scanf(%d,score); switch(score/10) { case 10:case 9: printf(A\n);break; case 8: printf(B\n);break; case 7: printf(C\n);break; case 6: printf(D\n);break; default: printf(E\n); getch(); } getch(); } 4.输入3个数,输出其中最大数。 #includestdio.h void main() { int a,b,c,max; printf(input three numbers:); scanf(%d,%d,%d,a,b,c); max=ab? a:b; if(cmax) max=c; printf(max=%d,max); getch(); } 5.输入3个数,按从大到小的顺序输出。 #includestdio.h void main() { int a,b,c,t; printf(\nPlease input three numbers:); scanf(%d,%d,%d,a,b,c); if(ab) {t=a;a=b;b=t;} if(bc) {t=b;b=c;c=t;} if(ab) {t=a;a=b;b=t;} printf(%d,%d,%d,a,b,c); getch(); } 6.输入一个不多于4位的正整数,求它的位数,并按逆序输出各位数字。 #includestdio.h #includeconio.h #includemath.h void main() { int x,n,a,b,c,d; scanf(%d,x); if(x0x10) n=1; else if(x100) n=2; else if(x1000) n=3; else if(x10000) n=4; a=x%10; b=(x/10)%10; c=(x/100)%10; d=(x/1000); switch(n) { case 1:p

文档评论(0)

1亿VIP精品文档

相关文档