C++程序设计教程(第2版)【高等教育出版社】第1-5章答案

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一章

// 修改例 1-1 的 Hello World student,and I like programming!

#include using namespace std; int main()

程序,使其能够在计算机屏幕上显示

“。

"I am a

{

cout<<"I am a student,and I like programming!"<nul"); return 0; }

// 修改例 1-3 的加法计算器程序,编写一个乘法计算器程序 #include using namespace std; int main() {

double a,b,c;

cout<<" 请输入两个数字 "<>a>>b; c=a*b;

cout<nul"); return 0; }

// 修改例 1-4 的生日卡程序,使其能够输入和显示日期

#include using namespace std; int main()

{

char name1[41],name2[41],date[41];

cout<<" 请输入你的朋友的名字: "<>name1;

cout<<" 请输入你的名字: "<>name2;

cout<<" 请输入日期 "<>date;

system("cls");

cout<<"=========================================="<

cout<<"=========================================="<

cout<<""<

system("pause>nul");

return 0;

}

/* 参考例 1-5 ,使用梯形法计算下列定积分的值

(sinx+e^x)dx[ 上限 1,下限 -1]

其中,积分区域等分数取为 200,并比较计算结果和手算结果的。 */ #include

#include //包含标准数学函数库

using namespace std;

int main()

{

double a,b,h,sum;

int n,i;

a=1.00;//积分上限为 1

b=-1.00;//积分下限为 -1

n=200;//积分区间等分为 200 份

h=(a-b)/n;//小区间长度

sum=(exp(a)+exp(b))/2; //exp()

为计算e^x的函数库

for(i=1;i

sum=sum+exp(b+i*h);

sum=sum*h;

cout<<" 结果是 "<

system("pause>nul");

return 0;

}

//仿照例 1-6 ,编写一个计算矩形面积的程序

#include

using namespace std;

double square(double a,double b) //定义

square函数

{

double s;

s=a*b;

return s;

}

int main()

{

double length,wideth,mianji;

cout<<" 请输入长度: "<

cin>>length;

cout<<"请输入宽度:"<

cin>>wideth;

mianji=square(length,wideth);

system("cls");

cout<<" 面积为 :"<

system("pause>nul");

return 0;

}

第二章

//为例 2-2 添加数据检验部分。给出三边长,检验其是否能构成一个三角形。如果检验不合格,输入信息“ Error data! ”。

#include

#include

using namespace std;

int main()

{

double a,b,c,s,area;

system("title 三角形面积计算程序 -by lyz810");

system("color 1e");

cout <<"请分别输入三边长a,b,c的值:";

cin>>a>>b>>c;

if (a + b >c && b + c > a && a + c > b)

{

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

system("cls");

cout<<"面积为:"<

system("pause>nul");

return 0;

}

else

cout<<"Error data!";

system("pause>nul");

return 0;

}

// 输入两个角度值 x,y ,计算式子 [sin(|x|+|y|)]/[ √cos(|x+y|)]

#include

#include

using namespace std;

int main()

{

system("title计算式子的值 -by lyz810");

相关文档
最新文档