C++ Primer Plus 第六版 第五章练习答案
-
for
循环; -
while
循环; -
do while
循环; -
基于范围的
for
循环;
#include <iostream>
using namespace std;
//practice 5.1
void p5_1(void) {
int number1, number2, sum = 0;
cout << "Please enter the first number: ";
cin >> number1;
cin.get();
cout << "Enter the second number: ";
cin >> number2;
cin.get();
for (int i = number1; i <= number2; i++)
sum += i;
cout << sum << endl;
return;
}
// practice 5.2
const int ArSize = 100;
#include<array>
void p5_2(void){
array<long double, ArSize+1> factorials;
factorials[0] = factorials[1] = 1.0;
for (int i = 2; i <= ArSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i <= ArSize; i++)
cout << i << "! = " << factorials[i] << endl;
return;
}
// practice 5.3
void p5_3(void){
int num = 0, sum = 0;
while (1){
cout << "Enter a number(to stop, type the number 0): ";
cin >> num;
cin.get();
if (num == 0)
break;
sum += num;
cout << "Till now, the sum of the number you inputed is " << sum << endl;
}
return;
}
// practice 5.4
void p5_4(void){
double Daphne = 100, Cleo = 100;
int years = 0;
while (Daphne >= Cleo){
years++;
Cleo += Cleo * 0.05;
Daphne += 10;
}
cout << "After " << years << " years, " << "Cleo account is " << Cleo << " Daphne account is " << Daphne << "." << endl;
}
// practie 5.5
#include<string>
const int Size = 12;
void p5_5(void){
string month[Size] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" };
int sell[Size];
int total_sales = 0;
cout << "Enter the sales of book <C++ for Fools> each month: " << endl;
for (int i = 0; i < Size; i++){
cout << month[i] << ": ";
cin >> sell[i];
cin.get();
total_sales += sell[i];
}
cout << "The total sales is " << total_sales << endl;
return;
}
// practice 5.6
void p5_6(void){
string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" };
int sales[3][12];
int total_sales[3];
for (int i = 0; i < 3; i++){
cout << "Enter " << i + 1 << " year(s) sales of book <C++ for Fools> each month: " << endl;
for (int j = 0; j < 12; j++){
cout << month[j] << ": ";
cin >> sales[i][j];
cin.get();
total_sales[i] += sales[i][j];
}
}
for (int i = 0; i < 3; i++)
cout << i + 1 << " year(s) total sales is " << total_sales[i] << endl;
cout << "Three years total sales is " << total_sales[0] + total_sales[1] + total_sales[2] << endl;
}
//practice5.7
struct car{
string company;
int year;
};
void p5_7(void){
int size = 0;
cout << "How many cars do you wish to catalog? ";
cin >> size;
cin.get();
car *pcar = new car[size];
for (int i = 0; i < size; i++){
cout << "Car #" << i + 1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, pcar[i].company);
cout << "Please enter the year make: ";
cin >> pcar[i].year;
cin.get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < size; i++)
cout << pcar[i].year << " " << pcar[i].company << endl;
}
// practice 5.8
#include<cstring>
void p5_8(void){
int words = 0;
char input[100];
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> input)
if (strcmp(input, "done"))
words++;
else
break;
cout << "You entered a total of " << words << " words." << endl;
}
// practice 5.9
void p5_9(void) {
int words = 0;
string input;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> input)
if (input != "done")
words++;
else
break;
cout << "You entered a total of " << words << " words." << endl;
}
// practice 5.10
void p5_10(void){
int row = 0;
cout << "Enter number of rows: ";
cin >> row;
cin.get();
for (int i = 1; i <= row; i++){
for (int j = 1; j <= row - i; j++)
cout << ".";
for (int j = 0; j < i; j++)
cout << "*";
cout << endl;
}
}
int main(int argc, char **argv) {
cout << "=====================================\n"
<< "============ Chapter5: ============\n"
<< "=====================================\n\n";
cout << "==>> Practice 5_1:\n";
p5_1();
cout << endl;
cout << "==>> Practice 5_2:\n";
p5_2();
cout << endl;
cout << "==>> Practice 5_3:\n";
p5_3();
cout << endl;
cout << "==>> Practice 5_4:\n";
p5_4();
cout << endl;
cout << "==>> Practice 5_5:\n";
p5_5();
cout << endl;
cout << "==>> Practice 5_6:\n";
p5_6();
cout << endl;
cout << "==>> Practice 5_7:\n";
p5_7();
cout << endl;
cout << "==>> Practice 5_8:\n";
p5_8();
cout << endl;
cout << "==>> Practice 5_9:\n";
p5_9();
cout << endl;
cout << "==>> Practice 5_10:\n";
p5_10();
cout << endl;
}