《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx

上传人:b****6 文档编号:5381978 上传时间:2022-12-15 格式:DOCX 页数:12 大小:17.26KB
下载 相关 举报
《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx_第1页
第1页 / 共12页
《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx_第2页
第2页 / 共12页
《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx_第3页
第3页 / 共12页
《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx_第4页
第4页 / 共12页
《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx

《《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx》由会员分享,可在线阅读,更多相关《《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx(12页珍藏版)》请在冰豆网上搜索。

《C++ Primer Plus》第四章编程题自答答案VS运行通过.docx

《C++PrimerPlus》第四章编程题自答答案VS运行通过

#include"stdafx.h"

#include

 

intmain()

{

usingnamespacestd;

/*第一题

编写一个程序,如下运行

Whatisyourfirstname?

BettySue

Whatisyourlastname?

Yewe

Whatlettergradedoyoudeserve?

B

Whatisyourage?

22

Name:

Yewe,BettySue

Grade:

C

Age:

22

注意:

该程序应该接受的名字包含多个单词。

另外,程序将向下调整成绩,即向上调一个字母。

假设用户请求A、B或C,所以不必担心D和F之间的空挡。

*/

charfirstName[20];

charlastName[20];

chargrade;

intage;

cout<<"Whatisyourfirstname?

";

cin.getline(firstName,20);

cout<<"Whatisyourlastname?

";

cin.getline(lastName,20);

cout<<"Whatlettergradedoyoudeserve?

";

cin>>grade;

cout<<"Whatisyourage?

";

cin>>age;

cout<<"\nName:

"<

cout<<"\nGrade:

"<

cout<<"\nAge:

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

intmain()

{

usingnamespacestd;

/*第二题

修改程序清单4.4,使用C++string类而不是char数组。

程序4.4

#include

intmain()

{

usingnamespacestd;

constintArSize=20;

charname[ArSize];

chardessert[ArSize];

cout<<"Enteryourname:

\n";

cin.getline(name,ArSize);

cout<<"Enteryourfavoriterdessert:

\n";

cin.getline(dessert,ArSize);

cout<<"Ihavesomedelicious"<

cout<<"foryou,"<

cin.get();

}

*/

stringname;

stringdessert;

cout<<"Enteryourname:

\n";

getline(cin,name);

cout<<"Enteryourfavoriterdessert:

\n";

getline(cin,dessert);

cout<<"Ihavesomedelicious"<

cout<<"foryou,"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第三题

要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,

并存储和显示组合结果。

请使用char数组和头文件cstring中的函数。

下面是该程序运行时的情形:

Enteryourfirstname:

Flip

Enteryourlastname:

Fleming

Here'stheinformationinasinglestring:

Fleming,Flip

*/

charfirstName[20],lastName[20];

stringsingleString;

cout<<"Enteryourfirstname:

";

cin.getline(firstName,20);

cout<<"Enteryourlastname:

";

cin.getline(lastName,20);

singleString=strcat(strcat(lastName,","),firstName);

//singleString+=firstName;

cout<<"Here'stheinformationinasinglestring:

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第四题

要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,

并存储和显示组合结果。

请使用string对象和头文件string中的函数。

下面是该程序运行时的情形:

Enteryourfirstname:

Flip

Enteryourlastname:

Fleming

Here'stheinformationinasinglestring:

Fleming,Flip

*/

stringsingleString,firstName,lastName;

cout<<"Enteryourfirstname:

";

getline(cin,firstName);

cout<<"Enteryourlastname:

";

getline(cin,lastName);

singleString=lastName+","+firstName;

cout<<"Here'stheinformationinasinglestring:

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第五题

结构CandyBar包含3个成员。

第一个成员储存了糖块的品牌;第二个成员储存了糖块的重量(可以有小数);第三个成员储存了糖块的卡路里含量(整数)。

请编写一个程序,声明这个结构,创建一个名为snack和CandyBar变量,并将其成员分别初始化为“MochaMunch”、2.3和350。

初始化应在声明snack时进行。

最后,程序显示snack变量的内容。

*/

structCandyBar{

charbrand[20];

floatgram;

intcalorie;

}snack{"MochaMunch",2.3,350};

cout<<"品牌名:

"<

"<

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第六题

结构CandyBar包含3个成员,如编程练习5所示。

请编写一个程序,创建一个包含3个元素的CandyBar数组,

并将它们初始化为选择的值,然后显示每个结构的内容

*/

structCandyBar{

charbrand[20];

floatgram;

intcalorie;

}snack[3]{

{"MochaMunch",2.3,350},

{"巧克力",3.4,460},

{"牛奶糖",4.5,570},

};

cout<<"品牌名:

"<

"<

"<

cout<<"\n品牌名:

"<

"<

"<

cout<<"\n品牌名:

"<

"<

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第七题

WilliamWingate从事披萨饼分析服务。

对于每个披萨饼,他都需要记录下列信息:

●披萨饼公司的名称,可以有多个单词组成。

●披萨饼的直径。

●披萨饼的重量。

请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。

程序将请求用户输入上述信息,然后显示这些信息,请使用cin(或它的方法)和cout。

*/

structpizza{

stringbrand;

intdiameter;

floatkilogram;

}pz;

cout<<"请输入生产公司:

";

getline(cin,pz.brand);

cout<<"请输入直径:

";

cin>>pz.diameter;

cout<<"请输入重量:

";

cin>>pz.kilogram;

cout<<"该披萨产自:

"<

"<

"<

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第八题

完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。

另外,让程序在请求输入披萨饼公司名称之前输入披萨饼的直径。

*/

structpizza{

stringbrand;

intdiameter;

floatkilogram;

};

pizza*pz=newpizza;

cout<<"请输入直径:

";

cin>>pz->diameter;

cin.get();

cout<<"请输入生产公司:

";

getline(cin,pz->brand);

cout<<"请输入重量:

";

cin>>pz->kilogram;

cout<<"该披萨产自:

"<brand<<"公司,直径:

"<diameter<<"寸,重:

"<kilogram<<"千克";

deletepz;

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第九题

完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

*/

structCandyBar{

stringbrand;

floatgram;

intcalorie;

};

CandyBar*snack=newCandyBar[3]{

{"MochaMunch",2.3f,350},

{"巧克力",3.4f,460},

{"牛奶糖",4.5f,570}

};

//strcpy_s(snack[0].brand,"MochaMunch");

//snack[0].gram=2.3f;

//snack[0].calorie=350;

//

//strcpy_s(snack[1].brand,"巧克力");

//snack[1].gram=3.4f;

//snack[1].calorie=460;

//

//strcpy_s(snack[2].brand,"牛奶糖");

//snack[2].gram=4.5f;

//snack[2].calorie=570;

cout<<"品牌名:

"<

"<

"<

cout<<"\n品牌名:

"<

"<

"<

cout<<"\n品牌名:

"<

"<

"<

delete[]snack;

cin.get();

cin.get();

return0;

}

#include"stdafx.h"

#include

#include

#include

#include

 

intmain()

{

usingnamespacestd;

/*第十题

让用户输入三次40码跑的成绩(如果你愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。

请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。

*/

arraytime;

cout<<"请输入第一名40米跑成绩:

";

cin>>time[0];

cout<<"请输入第二名40米跑成绩:

";

cin>>time[1];

cout<<"请输入第三名40米跑成绩:

";

cin>>time[2];

cout<<"三人平均成绩为"<<(time[0]+time[1]+time[2])/3<<"秒";

cin.get();

cin.get();

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 辩护词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1