C++ Primer Plus(第6版)Chapter 5 编程题答案
第1题:
#include <iostream>
#include <string>
#include <array>
#include <cstring>
using namespace std;
// task 1
int main()
{
int i, j;
int sum = 0;
cout << "Enter two int: ";
cin >> i;
cin >> j;
for (int x = i; x <= j; x++)
{
sum += x;
}
cout << "The sum of range(" << i << ", " << j << ") is " << sum << endl;
cin.get();
cin.get();
return 0;
}
第2题:
// task 2
int main()
{
const int size = 101;
array<long double, size> lda = {0};
lda[1] = lda[0] = 1;
for (int i = 2; i < size; i++)
{
lda[i] = i*lda[i - 1];
}
cout << "100! = " << lda[100] << endl;
cin.get();
cin.get();
return 0;
}
第3题:
// task 3
int main()
{
int num;
int sum = 0;
cout << "Enter the numbers: ";
cin >> num;
while (num)
{
sum += num;
cout << "Now sum is " << sum << " continue: "; //每次输入都显示目前为止所有输入的累积和
cin >> num;
}
cin.get();
cin.get();
return 0;
}
第4题:
// task 4
int main()
{
double d, c;
d = c = 100;
int years = 0;
while (c <= d)
{
d += 10;
c = c + c*0.05;
years++;
}
cout << years << " years later, Now d is " << d << " and c is " << c << endl;
cin.get();
cin.get();
return 0;
}
第5题:
// task 5
int main()
{
int sales[12];
int sum = 0;
string months[12] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
cout << "Enter the sale of 12 months: ";
for (int i = 0; i < 12; i++)
{
cin >> sales[i];
cout << "The sale of month " << months[i] << " is " << sales[i] << endl;
sum += sales[i];
}
cout << "The total sales of the year is " << sum << endl;
cin.get();
cin.get();
return 0;
}
第6题:
// task 6
int main()
{
int sales[3][12];
int i, j, sum1, sum2;
sum1 = sum2 = 0;
cout << "Enter the sale of months of 3 years: ";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 12; j++)
{
cin >> sales[i][j];
sum2 += sales[i][j];
}
sum1 += sum2;
cout << "The " << i + 1 << "st year sales is " << sum2 << endl;
sum2 = 0;
}
cout << "The sale of 3 years is " << sum1 << endl;
cin.get();
cin.get();
return 0;
}
第7题:
// task 7
struct car
{
char maker[20];
int year;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog? ";
cin >> num;
car* pt = new car[num];
for (int i = 1; i <= num; i++)
{
cin.get(); //吃掉之前那个换行
cout << "Car #" << i << endl;
cout << "Please enter the make: ";
cin.getline((pt + (i - 1))->maker, 20);
cout << "Please enter the year made: ";
cin >> (pt + (i - 1))->year;
}
cout << "Here is your collection:\n";
for (int i = 0; i < num; i++)
{
cout << (pt + i)->year << "\t" << (pt + i)->maker << endl;
}
delete[] pt; //别忘了释放分配的堆内存
cin.get();
cin.get();
return 0;
}
第8题:
// task 8
int main()
{
const int size = 10;
char word[size];
int count = 0;
cout << "Enter words (to stop, type the word done):\n";
cin >> word;
while (strcmp(word, "done"))
{
count++;
cin >> word;
}
cout << "You entered a total of " << count << " words" << endl;
cin.get();
cin.get();
return 0;
}
第9题:
// task 9
int main()
{
string word;
int count = 0;
cout << "Enter words (to stop, type the word done):\n";
cin >> word;
while (word != "done")
{
count++;
cin >> word;
}
cout << "You entered a total of " << count << " words" << endl;
cin.get();
cin.get();
return 0;
}
第10题:
// task 10
int main()
{
int lines;
cout << "Enter the num of the lines: ";
cin >> lines;
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= lines - i; j++)
{
cout << ".";
}
for (int m = 1; m <= i; m++)
{
cout << "*";
}
cout << endl;
}
cin.get();
cin.get();
return 0;
}