C++程序设计教程实验答案.pdf
文本预览下载声明
实验 1.1
#include iostream.h
#include iomanip.h
voidmain()
{
intx;
cinx;
inty;
y x*x;
coutx*x hexyendl;
}
实验 1.2
#include iostream.h
voidmain()
{
floatx 1,y 2,h 3,s;
s h*(x+y)/2.0;
cout上底为1,下底为2,高为3 的梯形面积为:sendl;
}
1
实验 1.3
#include iostream.h
voidmain()
{
floatx,y,z,s;
cout请输入三门成绩x,y,z:endl;
cinxyz;
s (x+y+z)/3;
cout三门课平均成绩为:sendl;
}
2
实验2.1
#include iostream.h
#include iomanip.h
voidmain()
{
float s1,s2,s3;
doubleaver;
cout输入三个数据:endl;
cins1s2s3;
aver (s1+s2+s3)/3;
aver aver*100+0.5;
aver (int)aver;
aver aver/100;
cout平均值 averendl;
}
3
实验2.2
#include iostream.h
#include math.h
voidmain()
{
doublex,y,z;
cout请输入直角三角形的两直角边长:endl;
cinxy;
z sqrt(x*x+y*y);
cout该直角三角形的斜边长为:zendl;
}
实验2.4
#include iostream.h
voidmain()
{
double a,b,c,t,ave;
cout请输入三个数endl;
cinabc;
if (ab)
t a;
else
t b;
if (tc)
t t;
else
t c;
ave (a+b+c)/3;
4
cout三个数的平均值为ave,其中最小的数为:tendl;
}
实验2.3
#include iostream.h
voidmain()
{
doublex,y,r,t;
cout请输入xyendl;
cinxy;
r x*x+y*y;
if (r 16 r 4)
t 12*3.1415926;
else
t 0;
couttendl;
}
实验2.5
#include iostream.h
5
voidmain()
{
charc1 f,c2 l,c3 y;
coutc1c2c3endl;
c1 c1+3;
c2 c2+3;
c3 c3+3;
if(c1z)
c1 c1-26;
if(c2z)
c2 c2-26;
if(c3z)
c3 c3-26;
coutc1c2c3endl;
}
实验3.1
#include iostream.h
voidmain()
{
intx,y;
floatr1,r2;
显示全部