c++ primer plus 编程答案 上
c++ primer plus 编程答案上
- 第二章
- 第三章
- 第四章
第二章
- 编写一个c++程序,它显示您的姓名和地址
#include<iostream>
int main() {using namespace std;cout << "my name is xxx,\nmy address is xxx" << endl;return 0;
}
- 编写一个c++程序,他要求用户输入一个以long为单位的距离,然后将她转化为为码。
#include <iostream>int main() {using namespace std;int distance = 0, yard;cout << "Please input a distance numebr in the unit of Long: ";cin >> distance;yard = distance * 220;cout << "The distance tranform in yards is: " << yard << endl;return 0;
}
- 编写一个C++程序,它使用 3 个用户定义的函数(包括main()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。
#include <iostream>using namespace std;void blind_mice() {cout << "Three blind mice." << endl;return;
}void how_they_run() {cout << "See how they run" << endl;return;
}int main() {blind_mice();blind_mice();how_they_run();how_they_run();return 0;
}
- 编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age: 29
#include <iostream>int main() {using namespace std;int years, months;cout << "Enter your age: ";cin >> years;months = years * 12;cout << years << " years is " << months << " monthes." << endl; return 0;
}
- 编写一个程序,其中的main( )调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要 求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value: 2020 degrees Celsius is 68 degrees Fahrenheit.
转换公式:华氏温度 = 1.8×摄氏温度 + 32.0
#include <iostream>double celsiu2fahrenit(double celsius) {return 1.8 * celsius + 32.0;
}int main() {using namespace std;double celsius;cout << "Please enter a celsius value: ";cin >> celsius;cout << celsius << " degrees Celsius is " << celsiu2fahrenit(celsius) << " degrees Fahrenheit." << endl;return 0;
}
- 编写一个程序,其main( )调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输 入光年值,并显示结果:
Enter the number of light years: 4.24.2 light years = 265608 astromonical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:1光年=63240天文单位.
#include <iostream>double light_years2astromonical_unit(double light_years) {return light_years * 63240;
}int main() {using namespace std;double light_years;cout << "nter the number of light years: ";cin >> light_years;cout << light_years << " light years = " << light_years2astromonical_unit(light_years)<< " astromonical units." << endl;return 0;
}
7.编写一个程序,要求用户输入小时数和分钟数。在 main() 函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28Time: 9:28
#include <iostream>using namespace std;void display_time(double hours, double minutes) {cout << "Time: " << hours << ":" << minutes << endl;}int main() { double hours, minutes;cout << "Enter the number of hours: ";cin >> hours;cout << "Enter the number of minutes: ";cin >> minutes;display_time(hours, minutes);return 0;
}
第三章
#include <iostream>const int Foot2inch = 12;int main() {using namespace std;int input_height = 0;cout << "Please input you height in inch: __\b\b";cin >> input_height;int height_foot = input_height / Foot2inch;int height_inch = input_height % Foot2inch;cout << "Your height in inch is: " << input_height << "; transforming in foot and inch is: " << height_foot << " foot "<< height_inch << " inch." << endl;return 0;
}
#include <iostream>const int Foot2Inch = 12;
const double Inch2Meter = 0.0254;
const double Kg2Pound = 2.2;double BMI(double weight, double height) {return weight/(height*height);
}int main() {using namespace std;double height_foot = 0;double height_inch = 0;double weight_pound = 0;cout << "Please enter your height in foot and Inch2Meter." << endl;cout << "Enter the foot of height: __\b\b";cin >> height_foot;cout << "Enter the inch of height: __\b\b";cin >> height_inch;cout << "Please enter your weight in pound: __\b\b";cin >> weight_pound;double height_meter = (height_foot * Foot2Inch + height_inch) * Inch2Meter;double weight_kg = weight_pound / Kg2Pound;double bmi = BMI(weight_kg, height_meter);cout << "Your BMI is: " << bmi << endl;return 0;
}
#include <iostream>int main() {using namespace std;double degree, minutes, seconds;cout << "Enter a latitude in degree, minutes and seconds." << endl;cout << "First, enter the degree: ";cin >> degree;cout << "Next, enter the minutes of arc: ";cin >> minutes;cout << "Finally, enter the seconds of arc: ";cin >> seconds;double degree2 = degree + minutes/60 + seconds/3600;cout << degree << " degrees, " << minutes << " minutes, "<< seconds << " seconds = " << degree2 << endl;return 0;
}
#include <iostream>int main() {using namespace std;long total_seconds;cout << "Enter the number of seconds: ";cin >> total_seconds;int days = total_seconds / 86400;int hours = (total_seconds % 86400) / 3600;int minutes = ((total_seconds % 86400) % 3600) / 60;int seconds = ((total_seconds % 86400) % 3600) % 60;cout << total_seconds << "seconds = " << days << " days, " << hours << " hours, "<< minutes << " minutes, "<< seconds << " seconds." << endl;return 0;
}
#include <iostream>int main() {using namespace std;long long population_world, population_China;cout << "Enter the world's population: ";cin >> population_world;cout << "Enter the population of China: ";cin >> population_China;double rate = double(population_China)/population_world;cout << "The population of the China is " << rate * 100<< "% of the world population." << endl;return 0;
}
#include <iostream>int main() {using namespace std;double kilometer, oil_liter;cout << "Enter the distance that you've dirver in kilometer: ";cin >> kilometer;cout << "Enter the comsumption of oil: ";cin >> oil_liter;double kilometer_per_liter = kilometer / oil_liter;cout << "The average fuel comsumption is " << 100 / kilometer_per_liter << " L/100km" << endl;
}
#include <iostream>int main() {using namespace std;const double Km2Mile = 0.6214;const double Gallon2Litre = 3.875;double fuel_comsuption_en = 0.0;cout << "Enter the fuel comsuption in European standard: ";cin >> fuel_comsuption_en;double fuel_comsuption_us = (100 * Km2Mile) / (fuel_comsuption_en/Gallon2Litre);cout << "The fuel comsuption in US standard is " << fuel_comsuption_us << " Miles/Gallon (mpg)." << endl; return 0;
}
第四章
题: 编写一个程序,如下输出实例所示的请求并显示信息:
What is your first name? Betty Sue
Waht is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22Name: Yewe, Betty Sue
Grade: C
Age: 22
程序应接受的名字包含多个单词。另外,程序将向下调整成绩。假设用户请求A、B或C,返回 B、C 或 D。
#include<iostream>
#include<string>int main() {using namespace std;char first_name[40];char last_name[40];char grade_letter;int age;cout << "What is your first name: ";cin.getline(first_name, 40);cout << "What is your last name: ";cin.getline(last_name, 40);cout << "What letter grade do you deserve: ";cin >> grade_letter;cout << "What is your age: ";cin >> age;cout << "Name: " << last_name << ", " << first_name << endl;cout << "Grade: " << char(grade_letter + 1) << endl;cout << "Age:" << age << endl;return 0;
}
- 题: 修改程序清单4.4,使用 C++ string 类而不是 char 数组。
#include<iostream>
#include<string>int main() {using namespace std;string name;string dessert;cout << "Enter your name:" << endl;getline(cin, name);cout << "Enter your favorite dessert" << endl;getline(cin, dessert);cout << "I have delicious " << dessert;cout << " for you, " << name << "." << endl;return 0;
}
- 编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的 情形:
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<string>
#include<cstring>int main() {using namespace std;char first_name[20], last_name[20];char final_name[50];cout << "Enter your last_name:";cin.getline(last_name, 20);cout << "Enter your first_name:";cin.getline(first_name, 20);strcpy(final_name, last_name);strcat(final_name, ",");strcat(final_name, first_name);cout << "Here's the information in a single string: " << final_name << endl;return 0;
}
- 编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming, Flip
#include<iostream>
#include<string>int main() {using namespace std;string first_name, last_name;string final_name;cout << "Enter your first name" << endl;getline(cin, first_name);cout << "Enter your last name" << endl;getline(cin, last_name);final_name = last_name + "," + first_name;cout << "Here's the information in a single string: " << final_name << endl;return 0;
}
结构体 CandyBar 包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。编写一个程序,声明这个结构体,创建一个名为 snack 的 CandyBar 变量,并将其成员分别初始化为 “Mocha Munch”、2.3 和 350。初始化应在声明 snack 时进行。最后,程序显示 snack 变量的内容。
#include <iostream>
#include <string>struct CandyBar
{std::string name;double weight;int calories;
};int main() {using namespace std;CandyBar snack = { "Mocha Munch", 2.3, 350 }; // 初始化结构体cout << "The name of the CandyBar: " << snack.name << endl;cout << "The weight of the candy: " << snack.weight << endl;cout << "The calories information: " << snack.calories << endl;return 0;
}
- 结构体 CandyBar 包含3个成员,如 编程练习5所示。请编写一个程序,创建一个包含 3 个元素的 CandyBar 数组,并将它们初始化为所选择的值,然后显示每个结构体的内容。
#include <iostream>
#include <string>struct CandyBar
{std::string name;double weight;int calories;
};int main() {using namespace std;CandyBar candbar[3] = {{"Mocha Munch", 2.3, 350},{"Big Rabbit", 5, 300},{"Joy Boy", 4.1, 430}};cout << "The name of the CandyBar: " << candbar[0].name << endl;<< "The weight of the candy: " << candbar[0].weight << endl;<< "The calories information: " << candbar[0].calories << endl<<endl;cout << "The name of the CandyBar: " << candbar[1].name << endl;<< "The weight of the candy: " << candbar[1].weight << endl;<< "The calories information: " << candbar[1].calories << endl<<endl;cout << "The name of the CandyBar: " << candbar[2].name << endl;<< "The weight of the candy: " << candbar[2].weight << endl;<< "The calories information: " << candbar[2].calories << endl;return 0;
}
- William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成;
披萨饼的直径;
披萨饼的重量。
请设计一个能够存储这些信息的结构体,并编写一个使用这种结构体变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用 cin(或其它的方法)和cout。
#include <iostream>
#include <string>struct Pizza
{std::string company;double diameter;double weight;};int main() {using namespace std;Pizza pizza;cout << "Enter the pizza company: ";getline(cin, pizza.company);cout << "Enter the diameter of pizza: ";cin >> pizza.diameter;cout << "Enter the weight of pizza: ";cin >> pizza.weight;cout << "\nHere is the pizza information: "<<endl<< "Company: " << pizza.company << endl<< "Diameter: " << pizza.diameter << endl<< "Weight: " << pizza.weight << endl;return 0;
}
- 完成编程练习7,但使用 new 来为结构体分配内存,而不是声明一个结构体变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include <iostream>
#include <string>struct Pizza
{std::string company;double diameter;double weight;
};int main() {using namespace std;Pizza* pizza = new Pizza;cout << "Enter the diameter of pizza: ";cin >> pizza->diameter;cout << "Enter the weight of pizza: ";cin >> pizza->weight;cin.get();cout << "Enter the pizza company: ";getline(cin, pizza->company);cout << endl<<"Here is the pizza information: "<<endl<< "Company: " << pizza->company << endl<< "Diameter: " << pizza->diameter << endl<< "Weight: " << pizza->weight << endl;delete pizza;return 0;
}
- 完成编程练习6,但使用 new 来动态分配数组,而不是声明一个包含 3 个元素的 CandyBar 数组。
#include <iostream>
#include <string>struct CandyBar
{std::string name;double weight;int calories;
};int main() {using namespace std;CandyBar *p_candybar = new CandyBar [3] {{"Mocha Munch", 2.3, 350},{"Big Rabbit", 5, 300},{"Joy Boy", 4.1, 430}};// 输出第一个结构体元素,按照数组的方式输出cout << "The name of the CandyBar: " << p_candybar[0].name << endl<< "The weight of the candy: " << p_candybar[0].weight << endl<< "The calories information: " << p_candybar[0].calories << endl<<endl;// 输出第二个结构体元素,可以按照指针的逻辑输出cout << "The name of the CandyBar: " << (p_candybar+1)->name << endl<< "The weight of the candy: " << (p_candybar+1)->weight << endl<< "The calories information: " << (p_candybar+1)->calories << endl<<endl;// 输出第三个结构体元素,又是数据的方式cout << "The name of the CandyBar: " << p_candybar[2].name << endl<< "The weight of the candy: " << p_candybar[2].weight << endl<< "The calories information: " << p_candybar[2].calories << endl;delete [] p_candybar;return 0;
}
- 编写一个程序,让用户输入三次 40 码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个 array对象来存储数据(如果编译器不支持 array 类,请使用数组)。
#include <iostream>
#include <array>int main() {using namespace std;array<double, 3> result;cout << "Enter threed result of the 40 meters runing time: \n";cin >> result[0];cin >> result[1];cin >> result[2];double ave_result = (result[0] + result[1] + result[2]) / 3;cout << "The all three time results are: " << result[0] << ", "<< result[1] << ", " << result[2] << endl;cout << "The average result: " << ave_result << endl;return 0;
}