1.编写一个程序,如下述输出示例所示的那样请求并显示信息:
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//myPractice401.cpp -- 姓名不是一个单词,用getline()读取,根据用户输入的grade自动向上调一个字母 #include <iostream> using namespace std; int main() { char first_name[20], last_name[20]; char grade; int age; cout << "What is your first name? "; cin.getline(first_name, 20); cout << "What is your last name? "; cin.getline(last_name, 20); cout << "What letter grade do you deserve? "; cin >> grade; cout << "What is your age? "; cin >> age; cout << "Name: " << last_name << ", " << first_name << endl; cout << "Grade: " << char(grade + 1) << endl; cout << "Age: " << age << endl; return 0; }
2.修改《C++学习笔记14:字符串》 中的第三个示例程序,使用C++ string 类而不是 char 数组。
//myPractice402.cpp -- string的输入和输出 #include <iostream> #include <string> using namespace std; int main() { string name, dessert; cout << "Enter your name: "; getline(cin, name); cout << "Enter your favorite dessert: "; getline(cin, dessert); cout << "I have some delicious " << dessert << " for you, " << name << ".\n"; return 0; }
3.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用 char 数组和头文件 cstring 中的函数。下面是该程序运行时的情形:
Enter your first name:Flip
Enter your last name:Fleming
Here's the information in a single string:Fleming,Flip//myPractice403.cpp -- 使用char数组和头文件cstring中的函数处理字符串 #include <iostream> #include <cstring> using namespace std; int main() { const char Connector[] = ", "; const int Size = 20; char first_name[Size], last_name[Size], full_name[Size * 2]; cout << "Enter your first name: "; cin.getline(first_name, Size); cout << "Enter you last name: "; cin.getline(last_name, Size); strcpy(full_name, first_name); strcat(full_name, Connector); strcat(full_name, last_name); cout << "Here is the information in a single string: " << full_name << endl; return 0; }
4.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用 string 对象和头文件 string 中的函数。下面是该程序运行时的情形:
Enter your first name:Flip
Enter your last name:Fleming
Here's the information in a single string:Fleming,Flip//myPractice404.cpp -- 使用string对象和头文件string中的函数处理字符串 #include <iostream> #include <string> using namespace std; int main() { const string Connector = ", "; string first_name, last_name, full_name; cout << "Enter your first name: "; getline(cin, first_name); cout << "Enter you last name: "; getline(cin, last_name); full_name = last_name + Connector + first_name; cout << "Here is the information in a single string: " << full_name << endl; return 0; }
5.结构 CandyBar 包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为 snack 的 CandyBar 变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化应在声明 snack 时进行。最后,程序显示 snack 变量的内容。
//myPractice405.cpp -- 创建结构并声明结构变量 #include <iostream> using namespace std; struct CandyBar { char brand[20]; float weight; unsigned int caloria; }; int main() { CandyBar snack = { "Mocha Munch",2.3,350 }; cout << "Brand: " << snack.brand << endl; cout << "Weight: " << snack.weight << endl; cout << "Caloria: " << snack.caloria << endl; return 0; }
6.结构 CandyBar 包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的 CandyBar 数组,并将它们初始化为所选择的值。然后显示每个结构的内容。
// myPractice406.cpp -- 创建结构数组,并访问结构元素中的成员 #include <iostream> using namespace std; struct CandyBar { char brand[20]; float weight; unsigned int calorie; }; int main() { CandyBar arr[3] = { { "Mocha Munch",2.3,350 }, { "Apple Lemon",2.7,560 }, { "Orange Juice",1.2,2300 } }; cout << "Brand: " << arr[0].brand << ", its weight is: " << arr[0].weight << ", and calorie is: " << arr[0].calorie << endl; cout << "Brand: " << arr[1].brand << ", its weight is: " << arr[1].weight << ", and calorie is: " << arr[1].calorie << endl; cout << "Brand: " << arr[2].brand << ", its weight is: " << arr[2].weight << ", and calorie is: " << arr[2].calorie << endl; return 0; }
7. William Wingate 从事比萨饼分析服务,对于每个比萨饼,他都需要记录下列信息:
- 比萨饼公司的名称。可以有多个单词组成。
- 比萨饼的直径。
- 比萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用 cin(或它的方法)和 cout。
//myPractice407.cpp -- 创建结构,静态存储结构相关数据 #include <iostream> #include <string> using namespace std; struct Pizza_Info { string company; float diameter; float weight; }; int main() { Pizza_Info Papa_John; cout << "Enter the name of the pizza company: "; getline(cin, Papa_John.company); cout << "Enter the diameter of the pizza: "; cin >> Papa_John.diameter; cout << "Enter the weight of the pizza: "; cin >> Papa_John.weight; cout << "Here are the informations of the pizza:\n"; cout << "Company: " << Papa_John.company << endl; cout << "Diameter: " << Papa_John.diameter << endl; cout << "Weight: " << Papa_John.weight << endl; return 0; }
8.完成编程练习7,但使用 new 来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司之前输入比萨饼的直径。
//myPractice408.cpp -- 创建结构,动态分配内存存储结构相关数据 #include <iostream> #include <string> using namespace std; struct Pizza_Info { //char company[40]; string company; float diameter; float weight; }; int main() { Pizza_Info * ptr = new Pizza_Info; cout << "Enter the diameter of the pizza: "; cin >> ptr->diameter; cout << "Enter the name of the pizza company: "; cin.get(); getline(cin, ptr->company); cout << "Enter the weight of the pizza: "; cin >> ptr->weight; cout << "Here are the informations of the pizza:\n"; cout << "Company: " << ptr->company << endl; cout << "Diameter: " << ptr->diameter << endl; cout << "Weight: " << ptr->weight << endl; delete ptr; return 0; }
9.完成编程练习6,但使用 new 来动态分配数组,而不是声明一个包含3个元素的 CandyBar 数组。
//myPractice409.cpp -- 创建结构数组,并利用new来动态分配数组 #include <iostream> #include <string> using namespace std; struct CandyBar { string brand; float weight; unsigned int calorie; }; int main() { CandyBar * ptr = new CandyBar[3]; ptr[0].brand = "Mocha Munch"; ptr[0].weight = 2.3; ptr[0].calorie = 350; (ptr + 1)->brand = "Apple Pie"; (ptr + 1)->weight = 1.5; ptr[1].calorie = 560; (ptr + 2)->brand = "Orange Juice"; ptr[2].weight = 3.3; ptr[2].calorie = 1021; cout << "Here are three sets of information:\n"; cout << "Brand: " << ptr[0].brand << ", Weight: " << ptr[0].weight << ", Calorie: " << ptr[0].calorie << endl; cout << "Brand: " << ptr[1].brand << ", Weight: " << ptr[1].weight << ", Calorie: " << ptr[1].calorie << endl; cout << "Brand: " << ptr[2].brand << ", Weight: " << ptr[2].weight << ", Calorie: " << ptr[2].calorie << endl; delete[] ptr; return 0; }
10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个 array 对象来存储数据(如果编译器不支持 array 类,请使用数组)。
//myPractice410.cpp -- array对象的使用 #include <iostream> #include <array> using namespace std; int main() { const int times = 3; array<float, times> result; cout << "请输入第一次40米跑的成绩(秒):"; cin >> result[0]; cout << "请输入第二次40米跑的成绩(秒):"; cin >> result[1]; cout << "请输入第三次40米跑的成绩(秒):"; cin >> result[2]; float average = (result[0] + result[1] + result[2]) / times; cout << "您三次40米跑的平均成绩是:" << average << "秒。\n"; return 0; }