1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
What is your first name?Betty Sue
What is your last name?Yewe
What letter grade do you deserve?B
Name:Yewe,Betty Sue
Grade:C
Age:22
#include
#include
using namespace std;
int main()
{
cout << "What is your first name?";
string name1;
getline(cin,name1);
cout << "What is your last name?";
string name2;
getline(cin,name2);
cout << "What letter grade do you desserve?";
char grade;
cin.get(grade);
cout << "What is your age?";
int age;
cin>>age;
cout << "Name:" << name1 << "," << name2 << endl;
cout << "Grade:" <
2.修改程序清单4.4,使用C++string类而不是char数组。
#include
#include
using namespace std;
int main()
{
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin,name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
3.编写一个程序,它要求用户首先输入其名,再输入其姓,然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name:Plip
Enter your last name:Fleming
Here is the information in a single string:Fleming,Flip
#include
#include
using namespace std;
int main()
{
cout << "Enter your first name:";
char name1[10];
cin.getline(name1, 10);
cout << "Enter your last name:";
char name2[10];
cin.getline(name2, 10);
cout << "Here's the information in a string:"<
4.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了卡路里的含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为”Mocha Munch",2.3和350.初始化应在声明snack时进行。最后,程序显示snack变量的内容。
#include
#include
using namespace std;
struct CandyBar
{
string name;
float weight;
int caloris;
};
int main()
{
CandyBar snack={"Mocha Munch",2.3,350};
cout << "糖块的品牌:" << snack.name << endl;
cout << "糖块的重量:" << snack.weight << endl;
cout << "糖块的卡路里:" << snack.caloris << endl;
return 0;
}
5.William Wingate从事比萨饼分析服务。对于每个披萨饼,它都需要记录下列信息:
请设计一个能够储存这些信息的结构,并编写一个使用这种结构变量的程序.程序将请求用户输入上述信息,然后显示这些信息,请使用cin和cout。
#include
#include
using namespace std;
struct Pizza
{
string name;
double dia;
double weight;
};
int main()
{
Pizza pia;
cout << "请输入披萨公司的名称:";
getline(cin,pia.name);
cout << "请输入披萨饼的直径:";
cin >> pia.dia;
cout << "请输入披萨饼的重量:";
cin >> pia.weight;
cout << "披萨公司的名称为:" << pia.name << endl;
cout << "披萨饼的直径为:" << pia.dia<
6.结构CandyBar包含三个成员,如编程练习4所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include
#include
using namespace std;
struct CandyBar
{
string name;
float weight;
int caloris;
};
int main()
{
CandyBar snacks[3] = { {"candy1",2.3,350 },
{"candy2",3.5,500},
{"candy3",3.1,450} };
cout << "the first candy's name:" << snacks[0].name<
7.完成编程练习题6,但使用new来为结构分配内存,而不是声明一个结构变量,另外,让程序现在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include
#include
using namespace std;
struct Pizza
{
double dia;
string name;
double weight;
};
int main()
{
Pizza* pt = new Pizza;
cout << "请输入披萨饼的直径:";
cin>>pt->dia;
cin.get();
cout << "请输入披萨公司的名称:";
cin >> pt->name;
cout << "请输入披萨饼的重量:";
cin >> pt->weight;
cout << "披萨饼的直径为:" << pt->dia << endl;
cout << "披萨公司的名称为:" << pt->name << endl;
cout << "披萨饼的重量为:" << pt->weight << endl;
delete [] pt;
return 0;
}
8.完成编程练习5.但是用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include
#include
#include
using namespace std;
struct CandyBar
{
string name;
float weight;
int caloris;
};
int main()
{
CandyBar *pt=new CandyBar[3];
cout << "Enter the first candy's name:";
cin >>pt[0].name;
cout << "Enter the first candy's weight:";
cin >> pt[0].weight;
cout << "Enter the first candy's caloris:";
cin>>pt[0].caloris;
cout << endl;
cout << "Enter the second candy's name:";
cin >> pt[1].name;
cout << "Enter the second candy's weight:";
cin >> pt[1].weight;
cout << "Enter the second candy's caloris:";
cin >> pt[1].caloris;
cout << endl;
cout << "Enter the third candy's name:";
cin >> pt[2].name;
cout << "Enter the third candy's weight:";
cin >> pt[2].weight;
cout << "Enter the third candy's caloris:";
cin >> pt[2].caloris;
cout << endl;
cout << "the first candy's name:" << pt[0].name << endl;
cout << "the first candy's weight:" << pt[0].weight << endl;
cout << "the first candy's caloris:" << pt[0].caloris << endl;
cout << endl;
cout << "the first candy's name:" <
10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include
#include
using namespace std;
int main()
{
arraygrade;
cout << "Enter the grade:";
cin >> grade[0] >>grade[1] >> grade[2];
int sum = grade[0] + grade[1] + grade[2];
int times = sizeof(grade) / sizeof(int);
cout << "The run times:" << times<