《分支结构例题.doc》由会员分享,可在线阅读,更多相关《分支结构例题.doc(3页珍藏版)》请在金锄头文库上搜索。
1、【源程序1】大小写转换示例。#include main() char c1,c2; scanf(%c,&c1); printf(%c,%dn,c1,c1); c2=c1+32; printf(%c,%dn,c2,c2); getch();【源程序2】求三角形的面积。#include main() float a,b,c,s,area; scanf(%f,%f,%f,&a,&b,&c); s=1.0/2*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c); printf(%fn,area); getch(); 3,4,5修改后#include main() float
2、a,b,c,s,area; scanf(%f%f%f,&a,&b,&c); s=1.0/2*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c); printf(%fn,area); getch(); 3 4 5再修改#include main()float a,b,c,s,area; scanf(%f%f%f,&a,&b,&c); if(a+bc&b+ca&a+cb) s=1.0/2*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c) ; printf(area=%6.2fn,area);else printf(It is not a tri
3、lateral.); getch();【源程序3】逻辑运算符短路求值示例。#includemain() int a=1,b=2,c=3,d=4,m=1,n=1; int x; x=(m=ab)&(n=cd); printf(x=%dn3,x); printf(m=%d,n=%dn,m,n); getch(); 修改后#includemain() int a=1,b=2,c=3,d=4,m=1,n=1; int x; x=(m=ab)&(n=cd); printf(x=%dn,x); printf(m=%d,n=%dn,m,n); getch(); 【源程序4】if语句语法1结构用法示例。#in
4、clude main() int a,b; scanf (%d%d,&a,&b); if(ab) printf(%dn,a); else printf (%dn,b); getch(); 【源程序5】if语句语法2结构用法示例。#includemain() char ch; scanf(%c,&ch); if(ch=a&ch=z) ch-=a-A; printf(%cn,ch); getch(); 【源程序6】if语句嵌套结构用法示例。#include main()char c; scanf(%c,&c); if (c=0&c=A&c=a&c=z) printf (This is a small letter.n); else printf (This is an other charater.n); getch();【源程序7】else和if的匹配问题示例。#includemain( ) float x,y; scanf(%f,&x); if (x0) y=-1; else if (x=0) y=0; else y=1; printf(x=%f,y=%2.0fn,x,y); getch();