版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第2章程序控制结构
2.1选择控制
2.2循环控制
2.3判断表达式的使用
2.4转向语句
小结语句是程序的基本语法成分。程序设计语言的语句按功能可以分成三类:
声明语句指示编译器分配内存,或者提供程序连接信息
操作语句描述对数据的处理
控制语句用于控制程序的执行流程。所有程序都只能包含三种控制结构: 顺序结构、选择结构和循环结构第2章程序控制结构对给定的条件进行判断,并根据判断的结果选择不同的操作
2.1选择控制条件运算表达式
根据判断条件,决定表达式的值 不改变程序语句执行流程
比较条件运算1.if语句的形式和执行流程if(表达式)语句;2.1.1if语句语句形式(1)执行流程false(0)true(非0)
表达式
语句2.1.1if语句false(0)true(非0)
表达式
语句1.if语句的形式和执行流程if(表达式)语句;2.1.1if语句语句形式(1)执行流程
表达式
语句true(非0)2.1.1if语句false(0)true(非0)
表达式
语句1.if语句的形式和执行流程if(表达式)语句;2.1.1if语句语句形式(1)执行流程
表达式
false(0)2.1.1if语句2.1.1if语句35abmax例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句35abmax3例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句35abmax3例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句35abmax3例:
:max=a;if(b>a)
max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句35abmax5例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句35abmax5
max=5输出例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句73abmax例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句73abmax7例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句73abmax7例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句73abmax7
max=7输出例:
:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句1.if语句的形式和执行流程语句形式(2)if(表达式)语句1
;
else语句2
;执行流程false(0)true(非0)
表达式
语句1语句22.1.1if语句2.1.1if语句1.if语句的形式和执行流程语句形式(2)if(表达式)语句1
;
else语句2
;执行流程false(0)true(非0)
表达式
语句1语句2true(非0)
表达式
语句12.1.1if语句2.1.1if语句1.if语句的形式和执行流程语句形式(2)if(表达式)语句1
;
else语句2
;执行流程false(0)true(非0)
表达式
语句1语句2false(0)
表达式
语句22.1.1if语句2.1.1if语句例:
:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:35abmax2.1.1if语句2.1.1if语句35abmax例:
:if(b>a)
max=b;elsemax=a;cout<<"max="<<max<<endl;:2.1.1if语句2.1.1if语句例:
:if(b>a)
max=b;
elsemax=a;cout<<"max="<<max<<endl;:35abmax52.1.1if语句2.1.1if语句例:
:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:35abmax5
max=5输出2.1.1if语句2.1.1if语句例:
:if(b>a)
max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax2.1.1if语句2.1.1if语句例:
:if(b>a)max=b;
elsemax=a;cout<<"max="<<max<<endl;:73abmax72.1.1if语句2.1.1if语句例:
:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax7
max=7输出2.1.1if语句2.1.1if语句例:
:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax7
max=7输出试一试用条件表达式修改该程序2.1.1if语句2.1.1if语句2.if语句的嵌套
if语句中的执行语句如果又是另一个if语句,称为嵌套if语句
if与else的配对关系:C++规定,else总是与它接近的if配对使用复合语句,可以改变条件语句的执行流程2.1.1if语句2.1.1if语句例2-1
编写程序计算货物运费。设货物运费每吨单价p(元)与运输距离s(公里)之间有如下关系:p=输入要托运的货物重量为w吨,托运距离s公里,计算总运费t:
t=p*w*s2.1.1if语句2.1.1if语句控制流图truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s2.1.1if语句2.1.1if语句truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s//例2-1计算货物运费#include<iostream>#include<iomanip>usingnamespacestd;intmain(){doublet,p,w,s;
cout<<"Pleaseinputweight(ton):";
cin>>w;
cout<<"Pleaseinputdistsnce(kilometre):";
cin>>s;if(s<100)p=30;elseif(s<200)p=27.5; elseif(s<300)p=25; elseif(s<400)p=22.5; elsep=20;t=p*w*s;
cout<<"Thecostis:"<<setprecision(2)<<t<<'$'<<endl;}想一想:几个else分支语句的次序可以改变吗?2.1.1if语句//例2-1计算货物运费#include<iostream>#include<iomanip>usingnamespacestd;intmain(){doublet,p,w,s;
cout<<"Pleaseinputweight(ton):";
cin>>w;
cout<<"Pleaseinputdistsnce(kilometre):";
cin>>s;if(s<100)p=30;elseif(s<200)p=27.5; elseif(s<300)p=25; elseif(s<400)p=22.5; elsep=20;t=p*w*s;
cout<<"Thecostis:"<<setprecision(2)<<t<<'$'<<endl;}truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s2.1.1if语句
例如:
cin>>s;if(s<300)p=25;elseif(s<100)p=30;elseif(s<400)p=22.5;elseif(s<200)p=27.5;elsep=20;t=p*w*s;
若输入s:150
输出t值等于多少?为什么?2.1.1if语句2.1.1if语句例2-2
输入三个整数,按从小到大顺序输出。解法一
数据排序。先找出最小值,放在a中,然后找次小值,放在b中:
1.对a、b进行比较,把小值放于a;
ifa>bab
//a<b2.
对a、c进行比较,把小值放于a;
ifa>cac
//a<c,a<b3.对
b、c进行比较,把小值放于b;
ifb>cbc
//a<b&&b<c4.输出a,b,c的值。2.1.1if语句2.1.1if语句例2-2
输入三个整数,按从小到大顺序输出。a
7
5
2bca>b:ab
a
5
7
2bca
2
7
5bc
2
7a>c:ac
2b>c:bc
a
2
5
7bca<ba<b,a<ca<b&&b<c2.1.1if语句2.1.1if语句例2-2
输入三个整数,按从小到大顺序输出。#include<iostream>usingnamespacestd;intmain(){inta,b,c,t;
cout<<"Pleaseinputthreeintegernumbers:";
cin>>a>>b>>c;if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}
cout<<a<<""<<b<<""<<c<<endl;}注意语句块结构2.1.1if语句2.1.1if语句例2-2
输入三个整数,按从小到大顺序输出。#include<iostream>usingnamespacestd;intmain(){inta,b,c,t;
cout<<"Pleaseinputthreeintegernumbers:";
cin>>a>>b>>c;if(a>b)
{t=a;a=b;b=t;}if(a>c)
{t=a;a=c;c=t;}if(b>c)
{t=b;b=c;c=t;}
cout<<a<<""<<b<<""<<c<<endl;}注意语句块结构2.1.1if语句2.1.1if语句 if(E1) if(E2)S1
elseS2if(E1)
{if(E2)S1
}elseS2分析S1和S2的执行条件:E1&&E2
执行S1E1&&!E2
执行S2E1&&E2
执行S1
!E1
执行S2注意括号的作用2.if语句的嵌套2.1.1if语句3.应用举例(1)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。//例2-3#include<iostream>usingnamespacestd;intmain(){charch;
cout<<"ch=";
cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;
cout<<ch<<endl;}2.1.1if语句3.应用举例//例2-3
#include<iostream>usingnamespacestd;intmain(){charch;
cout<<"ch=";
cin>>ch;if(ch>='A'&&ch<='Z')
ch+=32;
cout<<ch<<endl;}输入大写字母(1)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。2.1.1if语句3.应用举例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;
cout<<"ch=";
cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;
cout<<ch<<endl;}计算ASCII码偏移值(1)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。2.1.1if语句3.应用举例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;
cout<<"ch=";
cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;
cout<<ch<<endl;}(1)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。2.1.1if语句3.应用举例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;
cout<<"ch=";
cin>>ch;
if(ch>='A'&&ch<='Z')ch+=32;
cout<<ch<<endl;}改写为条件表达式ch=(ch>='A'&&ch<='Z')?ch+32:ch;(1)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。2.1.1if语句3.应用举例
(2)
求一元二次方程ax2+bx+c=0的根。求根公式:①当a=0时,方程不是二次方程②当b2-4ac=0时,有两个相同的实根:③当b2-4ac>0时,有两个不同的实根:④当b2-4ac<0时,有两个共轭复根:2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}数据说明2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}提示并输入系数2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;
if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}不是二次方程2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}a等于0浮点数的误差判断2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;
else
{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}有根2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}求判别式2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;
if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}有两个相同的实根2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}有两个不相同的实根2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}有两个共轭复根2.1.1if语句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;
cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)
cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)
cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);
cout<<"Ithastwocomplexroots:"<<endl;
cout<<rp<<"+"<<ip<<"i"<<endl;
cout<<rp<<"-"<<ip<<"i"<<endl; } }}a,b,c=021Itisnotquadratic.a,b,c=121Ithastwoequalrealroots:-1a,b,c=151Ithastwodistinctrealroots:-0.208712and–4.791288a,b,c=234Ithastwocomplexroots:-0.75+1.198958i-0.75-1.198958i2.1.1if语句根据一个整型表达式的值决定程序分支2.1.2switch语句一般形式:
switch(表达式){case常量表达式1:语句1
case常量表达式2:语句2
…
case常量表达式n:语句ndefault:语句n+1}注:
表达式类型为非浮点型
各常量表达式类型要与之匹配各常量表达式要求各不相等default子句可选。缺省时,没有匹配值switch语句为空语句标号2.1.2switch语句根据一个整型表达式的值决定程序分支2.1.2switch语句表达式语句1语句2语句3语句n语句n+1=常量1=常量2=常量3=常量n=常量n+1执行流程2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}观察不同输入时的输出结果2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;
switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2switch语句例2-5根据考试成绩的等级打印出百分制分数段。#include<iostream>usingnamespacestd;intmain(){chargrade;
cout<<"Inputgradeofscore(a_d):"<<endl;
cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}输入a输出85__10070__8460__6960error2.1.2switch语句2.1.2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人民版九年级生物下册阶段测试试卷含答案
- 2025年华师大新版选修4化学下册阶段测试试卷含答案
- 2025年沪科版第二册生物上册月考试卷
- 2025年人教新课标七年级生物下册阶段测试试卷含答案
- 2025年粤教沪科版八年级科学上册月考试卷含答案
- 2025年沪教版九年级历史上册阶段测试试卷含答案
- 2025年新世纪版七年级物理上册阶段测试试卷含答案
- 2025年华东师大版必修3历史上册月考试卷含答案
- 2025年度网络文字处理专家劳动合同4篇
- 2025年度智能门窗系统销售安装与升级合同4篇
- 2025年度版权授权协议:游戏角色形象设计与授权使用3篇
- 心肺复苏课件2024
- 《城镇燃气领域重大隐患判定指导手册》专题培训
- 湖南财政经济学院专升本管理学真题
- 全国身份证前六位、区号、邮编-编码大全
- 2024-2025学年福建省厦门市第一中学高一(上)适应性训练物理试卷(10月)(含答案)
- 《零售学第二版教学》课件
- 广东省珠海市香洲区2023-2024学年四年级下学期期末数学试卷
- 房地产行业职业生涯规划
- 江苏省建筑与装饰工程计价定额(2014)电子表格版
- MOOC 数字电路与系统-大连理工大学 中国大学慕课答案
评论
0/150
提交评论