第三章程序的选择执行
3.1if语句和关系表达式
例3.1快递业务
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int a,b,n; long long s; cin>>a>>b>>c; s=(( long long )a+b)*n/2; cout<<s<<endl; return 0; } |
3.1.1if语句格式
格式1
if(表达式) 语句;
功能:当条件成立即表达式为真时,执行“语句”,否则执行if语句下方的语句。
例3.2判断负数
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> using namespace std; int main() { int n; cout<< "n=" ; cin>>n; if (n<0) cout<< "注意负数!" <<endl; cout<<n<<endl; return 0; } |
格式2
if(表达式) 语句1;
else 语句;
例3.3判定数的奇偶
1 2 3 4 5 6 7 8 9 10 | #include<iostream> using namespace std; int main() { int n; cin>>n; if (n%2==0) cout<<n<< "是偶数" <<endl; else cout<<n<< "是奇数" <<endl; return 0; } |
例3.4招收社员
方法一
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> using namespace std; int main() { int m; cout<< "m=" ; cin>>m; if (n>=80) cout<< "非常欢迎你参加音乐社" <<endl; else cout<< "欢迎你参加音乐社" <<endl; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> using namespace std; int main() { int m; cout<< "m=" ; cin>>m; if (m>=80) cout<< "非常" ; else cout<< "欢迎你参加音乐社" <<endl; return 0; } |
3.1.2关系表达式
关系运算符
== != > < >= <=
一般形式 表达式 关系运算符 表达式
表达式可以是算术表达式,也可以是关系表达式,逻辑表达式,赋值表达式,字符表达式。
例3.5关系表达式计算
例3.6购买计划
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> using namespace std; int main() { int cup; float a,b; cout<< "cup=" ; cin>>cup; a=cup*3*0.88; b=(cup-cup/9)*3; if (a<b) cout<< "大洋商城" <<endl; else cout<< "百汇商厦" <<endl; return 0; } |
例3.7种树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int money; cout<< "money=" ; cin>>money; if (monkey<100000) cout<< "梨树=" <<money/500<< "棵" <<endl; else { cout<< "梨树=" << floor (money*0.3/500)<< "棵" <<endl; cout<< "桃树=" << floor (money*0.5/600)<< "棵" <<endl; cout<< "苹果树=" << floor (money*0.2/800)<< "棵" <<endl; } return 0; } |
复合语句:用{}括起来,将几个语句括起来的语句组合称为复合语句
3.2逻辑表达式和条件表达式
例3.8评先进
1 2 3 4 5 6 7 8 9 10 | #include<iostream> using namespace std; int main() { int cmark,mmark; cin>>cmark>>mmark; if (cmark>=75&&mmark>=85) cout<< "有资格" <<endl; else cout<< "无资格" <<endl; return 0; } |
3.2.1逻辑运算符
&& || !
逻辑运算符中&&和||低于关系运算符,!高于算术运算符。
将两个关系表达式用逻辑运算符连接起来的表达式,称为逻辑表达式,逻辑表达式的一般可以表示为
表达式 逻辑运算符 表达式
逻辑表达式的值是一个逻辑值。在c++中,整数数据可以出现在逻辑表达式中,在进行逻辑运算时,根据整型数据的值是0或非0,把它作为逻辑值“假”或“真”,然后参加逻辑运算。
逻辑非
逻辑与
逻辑或
例3.9写出条件表达式
例3.10判断闰年
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> using namespace std; int main() { int year; cin>>year; if ((year%4==0&&year%100!=0)||year%400==0) cout<<year<< "是闰年" <<endl; else cout<<year<< "不是闰年" <<endl; return 0; } |
例3.11三角形面积
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { float a,b,c; float p; float s; cin>>a>>b>>c; if (a+b>c&&a+c>b&&b+c>a) { p=(a+b+c)/2; s= sqrt (p*(p-a)*(p-b)*(p-c)); cout<< "三角面积为:" <<s; } else cout<< "不能构成三角形" ; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { float a,b,c; float p; float s; cin>>a>>b>>c; if (a+b<=c||a+c<=b||b+c<=a) cout<< "不能构成三角形" ; else { p=(a+b+c)/2; s= sqrt (p*(p-a)*(p-b)*(p-c)); cout<< "三角面积为:" <<s; } return 0; } |
3.2.2逻辑变量
bool标识符
例3.12阅读程序写程序结果
1 2 3 4 5 6 7 8 9 10 11 12 | #include<iostream> using namespace std; int main() { bool found,flag= false ; found= true ; cout<<flag<< " " <<found<<endl; flag=5; found=0; cout<<flag<< " " <<found<<endl; return 0; } |
3.2.3条件表达式
格式<表达式1>?<表达式2>:<表达式3>
条件表达式要求有3个操作对象,称为三目(元)运算符,它是c++中唯一个三目运算符。
条件表达式的运算规则
(1)计算表达式1的值
(2)若表达式1的值为真(或非0),则只计算表达之2,并将其结果作为整个表达式的值
(3)反之,则只计算表达式3,并将其结果作为整个表达式的值。
例3.13解释下列条件表达的作用
例3.14判断字母大小写,进行相应操作
1 2 3 4 5 6 7 8 9 10 | #include<iostream> using namespace std; int main() { char ch; cin>>ch; ch=(ch>= 'A' &&ch<= 'Z' )?(ch+32):ch; cout<<ch<<endl; return 0; } |
3.3嵌套if语句
例3.15输入年份,输出该年是否是闰年
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> using namespace std; int main() { int year; cin>>year; if (year%400==0) cout<<year<< "是闰年" <<endl; else if (year%4==0) if (year%100!=0) cout<<year<< "是闰年" <<endl; else cout<<year<< "不是闰年" <<endl; else cout<<year<< "不是闰年" <<endl; return 0; } |
嵌套if语句是指在if…else语句分支中还存在if…else语句。
在使用嵌套if语句时,需要特别注意if与else的配对关系,else总是与它上面最近的,且未配对的if配对。
例3.16分析程序的区别
程序1
1 2 3 4 5 6 7 8 9 10 11 12 | #include<iostream> using namespace std; int main() { int n; cin>>n; if (n%3==0) if (n%5==0) cout<<n<< "是15的倍数" <<endl; else cout<<n<< "是3的倍数但不是5的倍数" <<endl; cout<< "结束" <<endl; return 0; } |
程序2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> using namespace std; int main() { int n; cin>>n; if (n%3==0) { if (n%5==0) cout<<n<< "是15的倍数" <<endl; } else cout<<n<< "不是3的倍数" <<endl; cout<< "结束" <<endl; return 0; } |
3.3.2嵌套if语句的应用
例3.17商场优惠活动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { float price,discount,amount; int count; cout<< "输入单价:" <<endl; cin>>price; cout<< "输入购买件数:" <<endl; cin>>count; if (cout<5) discount=1; else if (count<10) discount=0.9; else discount=0.8; amount=price*count*discount; cout<< "单价:" <<price<< " 购买件数:" <<count<< " 折扣:" <<discount<< " 总价:" <<amount<<endl; return 0; } |
例3.18三个数中的最大数
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<iostream> using namespace std; int main() { float a,b,c,maxn; cout<< "输入三个数:" ; cin>>a>>b>>c; if (a>b&&a>c) maxn=a; else if (b>a&&b>c) maxn=b; else maxn=c; cout<< "最大数为:" <<maxn<<endl; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<iostream> using namespace std; int main() { float a,b,c,maxn; cout<< "输入三个数:" ; cin>>a>>b>>c; maxn=a; if (b>maxn) maxn=b; if (c>maxn) maxn=c; cout<< "最大数为:" <<maxn<<endl; return 0; } |
例3.19三个数从大到小输出
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> using namespace std; int main() { float a,b,c; cin>>a>>b>>c; if (a>b) if (b>c) cout<<a<< "," <<b<< "," <<c<<endl; else if (a>c) cout<<a<< "," <<c<< "," <<b<<endl; else cout<<c<< "," <<a<< "," <<b<<endl; else if (b>c) if (a>c) cout<<b<< "," <<a<< "," <<c<<endl; else cout<<b<< "," <<c<< "," <<a<<endl; else cout<<c<< "," <<b<< "," <<a<<endl; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { float a,b,c,temp; cin>>a>>b>>c; if (a<b) { temp=a;a=b;b=temp; } if (a<c){ temp=a;a=c;c=temp; } if (b<c){ temp=b;b=c;c=temp; } cout<<a<< " " <<b<< " " <<c<<endl; return 0; } |
3.4switch语句
例3.20简单的计算器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; int main() { float num1,num2; char op; cin>>num1>>num2>>op; switch (op) { case '+' :cout<<num1<<op<<num2<< "=" <<num1+num2; break ; case '-' :cout<<num1<<op<<num2<< "=" <<num1-num2; break ; case '*' :cout<<num1<<op<<num2<< "=" <<num1*num2; break ; case '/' : if (num2!=0) cout<<num1<<op<<num2<< "=" <<num1+num2; else cout<< "Divided by zero" <<endl; break ; defaut:cout<< "Invalid operator" ; } return 0; } |
3.4.1switch语句格式
switch(表达式)
{
case 常量表达1:语句1;break;
case 常量表达2:语句2;break;
case 常量表达n:语句n;break;
default 语句n+1
}
例3.21阅读下面程序说明程序的区别
程序1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n; cout<< "n=" ; cin>>n; switch (n) { case 1:cout<< "f=n" ; break ; case 2:cout<< "f=n*n" ; break ; case 3:cout<< "f=n*n*n" ; break ; defaut:cout<< "f=0" ; } return 0; } |
程序2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int n; cout<< "n=" ; cin>>n; switch (n) { case 1:cout<< "f=n" ; case 2:cout<< "f=n*n" ; case 3:cout<< "f=n*n*n" ; defaut:cout<< "f=0" ; } return 0; } |
例3.22写程序结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> using namespace std; int main() { char score; cout<< "score=" ; cin>>score; switch (score) { case 'A' : case 'a' : cout<< "excellent" ; break ; case 'B' : case 'b' : cout<< "good" ; break ; defaut:cout<< "general" ; } return 0; } |
例3.23恩格尔系数
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<cstdio> using namespace std; int main() { float n; float x,y; scanf ( "%f %f" ,&x,&y); n=100*x/y; if (n>=60) printf ( "贫穷\n" ); else if (n>=50) printf ( "温饱\n" ); else if (n>=40) printf ( "小康\n" ); else if (n>=30) printf ( "相对富裕\n" ); else if (n>=20) printf ( "富裕\n" ); else printf ( "极其富裕\n" ); return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<cstdio> using namespace std; int main() { int n; float x,y; scanf ( "%f %f" ,&x,&y); n=100*x/y+0.5; switch (n/10) { case 0: case 1: printf ( "极其富裕\n" ); break ; case 2: printf ( "富裕\n" ); break ; case 3: printf ( "相对富裕\n" ); break ; case 4: printf ( "小康\n" ); break ; case 5: printf ( "温饱\n" ); break ; default : printf ( "贫穷\n" ); } return 0; } |
例3.24春节来临
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; int main() { int x,a,b,c,d; cin>>x; a=x/6; switch (x%6) { case 0:b=0,c=0,d=0; break ; case 1:a=a-2,b=1,c=0,d=0; break ; case 2:a=a-3,b=0,c=0,d=1; break ; case 3:a=a-2,b=0,c=1,d=0; break ; case 4:a=a-4,b=1,c=1,d=0; break ; case 5:a=a-5,b=0,c=1,d=1; break ; } cout<< "6元:" <<a<< " 13元:" <<b<< " 15元:" <<c<< " 20元:" <<d<<endl; return 0; } |
3.5分支结构程序设计实例
例3.25劳动课一元两次方程的解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<iostream> using namespace std; int main() { double l,s,x1,x2; double d; cin>>l>>s; d=l*1/4-4*s; if (d<0) cout<< "找不到这样的矩形" <<endl; else { if (d==0) x1=x2=l/4; else { x1=(l/2+ sqrt (d))/2; x2=(l/2- sqrt (d))/2; } cout<< "矩形的长和宽分别为:" <<x1<< "," <<x2<<endl; } return 0; } |
例3.26苹果
方法一
1 2 3 4 5 6 7 8 9 10 11 12 | #include<iostream> using namespace std; int main() { int n,x,y,t,rest; cin>>n>>x>>y; t=cell(( double )y/x); if (t<n) rest=n-t; else rest=0; cout<<rest<<endl; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> using namespace std; int main() { int n,x,y,t,rest; cin>>n>>x>>y; if (y%x==0) rest=y/x>=n?0:n-y; else rest=y/t>=n-1?0:(n-1-y)/x; cout<<rest<<endl; return 0; } |
例3.27大学校园
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { double dis; double t1,t2; cin>>dis; t1=dis/3+27+23; t2=dis/1.2; if (t1>t2) cout<< "walk" <<endl; else if (t1==t2) cout<< "The same" <<endl; else cout<< "Bike" <<endl; return 0; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { double dis; double t1,t2; cin>>dis; t1=dis*2+(27+23)*6; t2=dis*5; if (t1>t2) cout<< "walk" <<endl; else if (t1==t2) cout<< "The same" <<endl; else cout<< "Bike" <<endl; return 0; } |
例3.28分段计费
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库