答案仅供参考,实际运行效果取决于运行平台和运行软件
1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe,Betty Sue
Grade: C
Age: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求 A、B 或 C,所以不必担心 D和F之间的空档。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
char grade;
string fname, lname;
cout << "What is your first name? ";
getline(cin, fname);
cout << "What is your last name? ";
getline(cin, lname);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << lname << ", " << fname << endl;
cout << "Grade: " << ++grade << endl;
cout << "Age: " << age << endl;
return 0;
}
2.修改程序清单4.4,使用 C++ tring 类而不是char 数组
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, 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 数组和头文件 cstring 中的两数,下面是该程序运行时的情形:
Enter your fixet name: Fiip
Enter your last mane: Flaming
Here's the information in a single string: Fleming,Flip
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int ArSize = 20;
char fname[ArSize];
char lname[ArSize];
cout << "Enter your first name: ";
cin.getline(fname, ArSize);
cout << "Enter your last name: ";
cin.getline(lname, ArSize);
char *name = new char[strlen(fname) + strlen(lname) + 5];
strcpy(name, lname);
strcat(name, ", ");
strcat(name, fname);
cout << "Here's the information in a single string: ";
cout << name << endl;
delete[] name;
return 0;
}
4.编写一个程序,它要求用户首先输入其名,再输入其姓,然后程序使用一个这号和空格将姓和名组合起来,并存储和显示组合结果。请使用 string 对象和头文件 string 中的数。下面是该程序运行时的情形:
Enter your first name: Fiip
Enter your last nane: Fleming
Here's the information in a gingle string: Pleming,Plip5.结构 CandyBar 包含3个成员。第一个成员存储了糖块的品牌:第二个成员存储糖块的重量(可以有小数):第三个成员存储了糖块的卡路里含量(整数)。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fname, lname;
cout << "Enter your first name: ";
getline(cin, fname);
cout << "Enter your last name: ";
getline(cin, lname);
lname += ", " + fname;
cout << "Here's the information in a single string: ";
cout << lname << endl;
return 0;
}
5.请编写一个程序,声明这个结构,创建一个名为snack 的 CandyBar 变量,并将其成员分别初始化为“Mocha Munch”、23 和 350初始化应在声明 snack时进行。最后,程序显示 smack 变量的内容。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
double candy_weight;
int candy_calorie;
};
int main()
{
CandyBar snack = {"Mocha Munch", 2.3, 350};
cout << "Brand : " << snack.brand << endl;
cout << "Candy_weight: " << snack.candy_weight << endl;
cout << "Candy_calorie: " << snack.candy_calorie << endl;
return 0;
}
6.结构 CandyBar 包含3 个成员,如编程练习 5 所示请编写一个程序,创建一个包含 3 个元素的CandyBar 数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
double candy_weight;
int candy_calorie;
};
int main()
{
CandyBar snack[3] =
{
{"Mocha Munch", 2.3, 350},
{"Mooncake", 3.5, 369},
{"Birthdaycake", 6.8, 460}
};
cout << "The first candy:" << endl;
cout << "Brand: " << snack[0].brand << endl;
cout << "Candy_weight: " << snack[0].candy_weight << endl;
cout << "Candy_calorie: " << snack[0].candy_calorie << endl;
cout << "\nThe second candy:" << endl;
cout << "Brand: " << snack[1].brand << endl;
cout << "Candy_weight: " << snack[1].candy_weight << endl;
cout << "Candy_calorie: " << snack[1].candy_calorie << endl;
cout << "\nThe third candy:" << endl;
cout << "Brand: " << snack[2].brand << endl;
cout << "Candy_weight: " << snack[2].candy_weight << endl;
cout << "Candy_calorie: " << snack[2].candy_calorie << endl;
return 0;
}
7.William Wingate 从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成。
披萨饼的直径
披萨饼的重量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用 cin《或它的方法)和 cout。
#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
string pizza_company;
double pizza_diameter;
double pizza_weight;
};
int main()
{
Pizza snack;
cout << "Please enter the name of the Pizza Company: ";
getline(cin, snack.pizza_company);
cout << "Please enter the diameter of the pizza: ";
cin >> snack.pizza_diameter;
cout << "Please enter the weight of the pizza: ";
cin >> snack.pizza_weight;
cout << "The name of the Pizza Company: " << snack.pizza_company << endl;
cout << "The diameter of the Pizza Company: " << snack.pizza_diameter << endl;
cout << "The weight of the Pizza Company: " << snack.pizza_weight << endl;
return 0;
}
8.完成编程练习 7,但使用 new 来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
string pizza_company;
double pizza_diameter;
double pizza_weight;
};
int main()
{
Pizza *snack = new Pizza;
cout << "Please enter the diameter of the pizza: ";
(cin >> snack->pizza_diameter).get();
cout << "Please enter the name of the Pizza Company: ";
getline(cin, snack->pizza_company);
cout << "Please enter the weight of the pizza: ";
cin >> snack->pizza_weight;
cout << "The name of the Pizza Company: " << snack->pizza_company << endl;
cout << "The diameter of the Pizza Company: " << snack->pizza_diameter << endl;
cout << "The weight of the Pizza Company: " << snack->pizza_weight << endl;
delete snack;
return 0;
}
9.完成编程练习 6,但使用 new 来动态分配数组,而不是声明一个包含 3 个元素的 CandyBar 数组
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
double candy_weight;
int candy_calorie;
};
int main()
{
CandyBar *snack = new CandyBar[3];
snack[0] = {"Mocha Munch", 2.3, 350};
snack[1] = {"Mooncake", 3.5, 369};
snack[2] = {"Birthdaycake", 6.8, 460};
cout << "The first candy:" << endl;
cout << "Brand: " << snack[0].brand << endl;
cout << "Candy_weight: " << snack[0].candy_weight << endl;
cout << "Candy_calorie: " << snack[0].candy_calorie << endl;
cout << "\nThe second candy:" << endl;
cout << "Brand: " << snack[1].brand << endl;
cout << "Candy_weight: " << snack[1].candy_weight << endl;
cout << "Candy_calorie: " << snack[1].candy_calorie << endl;
cout << "\nThe third candy:" << endl;
cout << "Brand: " << snack[2].brand << endl;
cout << "Candy_weight: " << snack[2].candy_weight << endl;
cout << "Candy_calorie: " << snack[2].candy_calorie << endl;
delete[] snack;
return 0;
}
10.编写一个程序,让用户输入三次 40 码跑的成绩(如果您愿意,也可让用户输入 40 米的成绩).并显示次数和平均成绩。请使用一个 ray 对象来存储数据(如果编译器不支持 ray 类,请使用数组)
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<double, 3> scores;
cout << "Please enter your the results of the first 40 meter race: ";
cin >> scores[0];
cout << "Please enter your the results of the second 40 meter race: ";
cin >> scores[1];
cout << "Please enter your the results of the third 40 meter race: ";
cin >> scores[2];
cout << "You ran three times in total." << endl;
cout << "Your average score is ";
cout << (scores[0] + scores[1] + scores[2]) / 3.0;
cout << "." << endl;
return 0;
}