声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。
(具体方式参见第3章模板)
1.编写一个程序,如下述输出示例所示的那样请求显示信息:(注意:该程序应该接受的名字包含多个单词,另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B 或C,所以不用担心D和F之间的空档。)
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_1() { string firstname, lastname; char lettergrade; int age; cout << "What is you first name?"; getline( cin, firstname); cout << "What is your last name?"; getline(cin ,lastname); cout << "What letter grade do you deserve?"; cin >> lettergrade; cout << "What is your age?"; cin >> age; cout << "Name:" << lastname << "," << firstname << endl; cout << "Grade:"<< char (lettergrade + 1) << endl; cout << "Age:" << age << endl; }
2.修改程序清单4.4,使用C++ string类而不是char数组。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_2() { string name; string dessert; cout << "Enter your name:" << endl; getline( cin, name); cout << "Enter your favourite dessert:" << endl; getline(cin ,dessert); cout << "I have some delicious " << dessert << " for you," << name << endl; }
3.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并储存和显示结果。请使用char数组和头文件cstring中的函数。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_3() { char firstname[20]; char lastname[20]; cout << "Enter your first name:"; cin.getline( firstname, 20); cout << "Enter your last name:"; cin.getline(lastname, 20); cout << "Here is the information in a single string:" << strcat(strcat( firstname,","), lastname ) << endl; }
4.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并储存和显示结果。请使用string对象和头文件string中的函数。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_4() { string firstname; string lastname; cout << "Enter your first name:"; getline(cin, firstname); cout << "Enter your last name:"; getline(cin, lastname); cout << "Here is the information in a single string:" <<lastname + "," + firstname << endl; }
5.结构CandyBar包含3个成员。第一个成员储存了糖块的品牌;第二个成员储存糖块的重量(可以有小数);第三个成员储存了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量。并将其成员分别初始化为”Mocha Munch”、2.3和350。初始化在声明snack是进行。最后,程序显示变量snack变量的内容。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_5() { struct CandyBar { char name[20]; double weight; int calary; }; CandyBar snack = { "Mocha Munch", 2.3, 350 }; cout << "Name:" << snack.name << endl; cout << "Weight:" << snack.weight << endl; cout << "calary:" << snack.calary << endl; }
6.结构CandyBar包含3个成员,如编程5所示,请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选择的值,然后显示每个结构的内容。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_6() { struct CandyBar { char name[20]; double weight; int calary; }; CandyBar snack[3] = { {"Mocha Munch", 2.3, 350}, {"Mocha Munch", 2.3, 350}, {"Mocha Munch", 2.3, 350}, }; for (int i = 0; i < 3; i++) { cout << "Name:" << snack[i].name << endl; cout << "Weight:" << snack[i].weight << endl; cout << "calary:" << snack[i].calary << endl; cout << endl; } }
7. William Wingate从事披萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
(1)披萨饼公司的名称,可以有多个单词组成。
(2)披萨饼的直径。
(3)披萨饼的重量。
请设计一个能够储存这些信息的结构,并编写一个使用着这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或其他方法)和cout。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_7() { struct pizzamessage{ string name; double diameter; double weight; }; pizzamessage message; cout << "Please input the message of pizza!"<<endl; cout << "Name:"; getline( cin, message.name); cout <<"Diameter:"; cin >> message.diameter; cout << "Weight:"; cin >> message.weight; cout << "the message of the record is:"<< endl; cout << "Name:" << message.name; cout << "Diameter:" << message.diameter; cout << "Weight:" << message.weight; }
8.完成编程7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨饼公司名称之前输入披萨饼的直径。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_8() { struct pizzamessage{ string name; double diameter; double weight; }; int num; cout << "How many pieces of pizzamessage you want to be record?"; cin >> num; cin.get(); pizzamessage *message = new pizzamessage[num]; for (int i = 0; i < num; i++) { cout << "Please input the pizza message of the " << i + 1 << "th record:"<< endl; cout << "Name:"; getline(cin,message[i].name); cout <<"Diameter:"; cin >> message[i].diameter; cout << "Weight:"; cin >>message[i].weight; cout << endl; cin.get(); } for (int i = 0; i < num; i++) { cout << "the message of the "<< i+1 << "record is:"<< endl; cout << "Name:" << message[i].name << endl; cout << "Diameter:" << message[i].diameter << endl; cout << "Weight:" << message[i].weight << endl; cout << endl; } }
9.完成编程6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_9() { struct CandyBar { char name[20]; double weight; int calary; }; cout << "How many do you want to create?"; int num; cin >> num; cin.get(); CandyBar *candies = new CandyBar[num]; for (int i = 0; i < num; i++ ) { cout << "please input the message:" << endl; cout << "name:"; cin >> candies[i].name; cout << "weight:"; cin >> candies[i].weight; cout << "calary:"; cin >> candies[i].calary; } cout << endl; for (int i = 0; i < num; i++) { cout << "the message:" << endl; cout << "name:" << candies[i].name << endl; cout << "weight:" << candies[i].weight << endl; cout << "calary:" << candies[i].calary << endl; cout << endl; } }
10. 编写一个程序,让用户输入三次40码跑的成绩(也可以让用户输入),并显示次数和平均成绩。请使用一个array对象来储存数据(也可以使用数组)。
#include <iostream> #include <string> #include <cstring> using namespace std; void cprimerplus_exercise4_10() { cout << "How many times do you want to record?"<< endl; int num; cin >> num; double * times = new double[num]; for (int i = 0; i < num; i++) { cout << i+1 << " th input:" << endl; cin >> times[i]; } double sum = 0; for (int i = 0; i < num; i++) { sum +=times[i]; } cout << "you have " << num << "records" << endl; cout << " your average score is " << sum / num << endl; }
c++primerplus(第六版)编程题——第4章(复合类型)