}
⑵ 输入学生的语文、数学、英语、物理 4门课程的成绩,计算该学生的总成绩和平均成绩并输出。
#include
voidmain()
{
doubleeng,chin,math,phy,sum,aver;
cout<<"pleaseinput4scores:
";
cin>>eng>>chin>>math>>phy; // 输入成绩
sum=eng+chin+math+phy; //计算总成绩
aver=sum/4; // 计算平均分
cout<<"Sum="<}
⑶ 编写程序,从键盘输入一个大写英文字母,输出对应的小写字母。
#include
voidmain()
{
charc1,c2;
cout<<"Pleaseinputanupperletter:
";
cin>>c1;
c2=c1+32;
cout<<"c1="<}
实验三选择结构程序设计
三 实验内容
1.选择题
⑴ C
⑵ B
⑶ D
⑷ C
⑸ C
⑹ C
⑺ D
2.
⑴
-4
⑵
4 5 99
⑶
2,1
⑷
1
3. 编程题
⑴ 由键盘输入三个字符,输出其中的最大者。
【源程序】
#include
voidmain()
{
charx,max;
cout<<"Pleaseinputthreecharacterss:
";
cin>>x;
max=x;
cin>>x;
if(x>max)
max=x;
cin>>x;
if(x>max)
max=x;
cout<<"max="<}
⑵ 输入三角形三边的长,求三角形的面积。
若输入的三个边能构成三角形,则计算其面积并输出;否则输出提示信息。
【源程序】
//参见教材 P44例 3.15
#include
#include
voidmain()
{
doublea,b,c,s,area;
cout<<"Pleaseinputa,b,c=";
cin>>a>>b>>c;
if(a+b<=c||a+c<=b||b+c<=a)
cout<<"cantbeatriangle!
\n";
else
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"area="<
}
}
⑶ 编程求下面符号函数值:
y=
0 (x=0)
1 (x>0)
-1 ( x<0)
【源程序】
#include
voidmain()
{
intx,y;
cout<<"Pleaseinputx=";
cin>>x;
if(x>0)
y=1;
elseif(x==0)
y=0;
else
y=-1;
cout<<"y="<}
⑷ 计算奖金。
设企业利润为 L,当企业利润 L不超过 5000元时,奖金为利润的 1.5%,当 5000< L≤10000元时,超过5000 元部分奖金为 2%( 5000元以下仍按 1.5%);当 10000< L≤20000元,除10000 以下的按上述方法计算外,超过 10000元部分按 2.5%计算奖金;如果 20000< L≤50000元,超过20000 元部分按3%计算奖金;当 50000< L≤元时,超过50000 元部分按3.5%计算奖金;当 L超过 元时,超过 元部分按4%计算奖金。
由键盘输入 L的值,编程计算相应的奖金并输出。
【源程序】
#include
voidmain()
{
doubleL,S;
cout<<"pleaseinputL=";
cin>>L;
if(L<5000)
S=L*0.015;
elseif(L<10000)
S=75+(L-5000)*0.02;
elseif(L<20000)
S=175+(L-10000)*0.025;
elseif(L<50000)
S=175+250+(L-20000)*0.03;
elseif(L<)
S=175+250+900+(L-50000)*0.035;
else
S=175+250+900+1750+(L-)*0.04;
cout<<"S="<
}
⑸ 输入年龄,输出所处人群:
9岁以下为儿童,输出 A; 10~ 19为少年,输出 B; 20~ 29为青年,输出 C; 30~ 49为中年,输出 D; 50以上为老年,输出 E。
【源程序】
#include
voidmain()
{
intage;
cout<<"Pleaseinputage:
";
cin>>age;
switch(age/10)
{
case0:
cout<<"A--儿童\n";break;
case1:
cout<<"B--少年\n";break;
case2:
cout<<"C--青年\n";break;
case3:
case4:
cout<<"D--中年\n";break;
default:
cout<<"E--老年\n";break;
}
}
⑹ 有如下函数:
0< t< 1
1≤t< 2
2≤t< 3
3≤t< 4
由键盘输入t 值,计算S的值。
【源程序】
//方法一
#include
voidmain()
{
doublet,s;
cout<<"pleaseinputt=";
cin>>t;
if(t>0&&t<4)
{
if(t<1)
s=t*t;
elseif(t<2)
s=t*t-1;
elseif(t<3)
s=t*t-2*t+1;
else
s=t*t+4*t-17;
cout<<"s="<
}
else
cout<<"Error!
tcan\’tbelessthan0ormorethan4!
\n";
}
//方法二
#include
voidmain()
{
doublet;
cout<<"pleaseinputt=";
cin>>t;
if(t<=0)
cout