/***********************************
2017年10月9日16:18:02
Athor:xiyuan255
Course:C++
Contain:review5.cpp
Reference: C++ Primer plus
說明:C++ Primer plus第五章的練習題(選取部分)
【 P164 】
*************************************/
// review5.cpp -- C++ Primer plus 第五章練習題
#include <iostream>
#include <string> // exsampleNo2 field
#include <array> // exsampleNo2 field
#include <cstring> // exsampleNo8 field
using namespace std;
void exampleNo1(void);
void exampleNo2(void);
void exampleNo3(void);
void exampleNo4(void);
void exampleNo5(void);
void exampleNo6(void);
void exampleNo7(void);
void exampleNo8(void);
void exampleNo9(void);
void exampleNo10(void);
int main(void)
{
//exampleNo1(); // example 1 test
//exampleNo2(); // example 2 test
//exampleNo3(); // example 3 test
//exampleNo4(); // example 4 test
//exampleNo5(); // example 5 test
//exampleNo6(); // example 6 test
//exampleNo7(); // example 7 test
//exampleNo8(); // example 8 test
//exampleNo9(); // example 9 test
exampleNo10(); // example 10 test
return 0;
}
void exampleNo1(void)
{
cout << "請輸入兩個整數(由小到大): \n";
int num1, num2;
cin >> num1 >> num2;
long sum = 0;
for (int i=num1; i<=num2; i++) {
sum += i;
}
cout << "整數" << num1 << "到" << num2 << "之間的整數和是:\n";
cout << "sum = " << sum << endl;
}
/**
輸出結果:
請輸入兩個整數(由小到大):
[[ 2 9 ]]
整數2到9之間的整數和是:
sum = 44
*/
void exampleNo2(void)
{
const int ArSize = 16;
array<long double, ArSize> factorials;
factorials[1] = factorials[0] = 1LL;
for (int i=2; i<ArSize; i++)
factorials[i] = i * factorials[i-1];
for (int i=0; i<ArSize; i++)
cout << i << "! = " << factorials[i] << std::endl;
}
/**
輸出結果:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3.6288e+006
11! = 3.99168e+007
12! = 4.79002e+008
13! = 6.22702e+009
14! = 8.71783e+010
15! = 1.30767e+012
*/
void exampleNo3(void)
{
cout << "請輸入數字,輸入0作爲程序結束:\n";
int count = 0;
char ch = cin.get();
while (ch != '0') {
++count;
if(ch == '\n') {
cout << "count = " << count << endl;
}
ch = cin.get();
}
cout << "Total count = " << count << endl;
}
/**
輸出結果:
請輸入數字,輸入0作爲程序結束:
2623236
count = 8
1531436
count = 16
4510
Total count = 19
*/
void exampleNo4(void)
{
const float Profit_D = 0.1f;
const float Profit_C = 0.05f;
double Daphne = 100.0;
double Cleo = 100.0;
int years = 1;
while (Cleo <= Daphne) {
years++;
Daphne += 100.0 * Profit_D;
Cleo += Cleo * Profit_C;
}
cout << "經過" << years << "年後Cleo的投資價值超過Daphne的投資價值;\n"
<< "此時兩個人的投資價值爲:Cleo = " << Cleo << ", " << "Daphne = "
<< Daphne << endl;
}
/**
輸出結果:
經過28年後Cleo的投資價值超過Daphne的投資價值;
此時兩個人的投資價值爲:Cleo = 373.346, Daphne = 370
*/
void exampleNo5(void)
{
const char * Months[12] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
array<int, 12> sales_volume;
long sum_sales_volume = 0;
for (int i=0; i<12; i++) {
cout << "Please enter " << Months[i] << " of volume: ";
cin >> sales_volume[i];
sum_sales_volume += sales_volume[i];
}
cout << "Yearly sales volume is " << sum_sales_volume << endl;
}
/**
輸出結果:
Please enter January of volume: 1000
Please enter February of volume: 2000
Please enter March of volume: 3000
Please enter April of volume: 4000
Please enter May of volume: 5000
Please enter June of volume: 6000
Please enter July of volume: 7000
Please enter August of volume: 8000
Please enter September of volume: 9000
Please enter October of volume: 10000
Please enter November of volume: 11000
Please enter December of volume: 12000
Yearly sales volume is 78000
*/
void exampleNo6(void)
{
const string Months[12] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
int sales_volume[3][12];
long sum_sales_volume = 0;
for (int i=0; i<3; i++) {
cout << "Please enter the " << i+1 << " year.\n";
for (int j=0; j<12; j++) {
cout << " " << Months[j] << " of volume: ";
cin >> sales_volume[i][j];
sum_sales_volume += sales_volume[i][j];
}
}
cout << "The three years sales volume is " << sum_sales_volume << endl;
}
/**
輸出結果:
Please enter the 1 year.
January of volume: 1000
February of volume: 1000
March of volume: 1000
April of volume: 1000
May of volume: 1000
June of volume: 1000
July of volume: 1000
August of volume: 1000
September of volume: 1000
October of volume: 1000
November of volume: 1000
December of volume: 1000
Please enter the 2 year.
January of volume: 2000
February of volume: 2000
March of volume: 2000
April of volume: 2000
May of volume: 2000
June of volume: 2000
July of volume: 2000
August of volume: 2000
September of volume: 2000
October of volume: 2000
November of volume: 2000
December of volume: 2000
Please enter the 3 year.
January of volume: 3000
February of volume: 3000
March of volume: 3000
April of volume: 3000
May of volume: 3000
June of volume: 3000
July of volume: 3000
August of volume: 3000
September of volume: 3000
October of volume: 3000
November of volume: 3000
December of volume: 3000
The three years sales volume is 72000
*/
void exampleNo7(void)
{
struct car_t {
string company;
int year;
};
cout << "How many cars do you wish to catalog? ";
int count;
cin >> count;
cin.get();
car_t * cars = new car_t[count];
for (int i=0; i<count; i++) {
cout << "Car #" << i + 1 << ": \n";
cout << "Please enter the make: ";
getline(cin, cars[i].company);
cout << "Please enter the year made: ";
cin >> cars[i].year;
cin.get();
}
cout << "Here is your collection: \n";
for (int i=0; i<count; i++) {
cout << cars[i].year << " " << cars[i].company << endl;
}
delete [] cars;
}
/**
輸出結果:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
*/
void exampleNo8(void)
{
char words[20];
cout << "Enter words (to stop, type the word done): \n";
int count = 0;
cin >> words; // 這語句的含義:將輸入流cin與words建立連接,
// 以空白作爲分隔符,遇到回車關閉該此連接;
//cout << "words = " << words << endl;
while (strcmp(words, "done")) {
cin >> words;
//cout << "words = " << words << endl;
count++;
}
cout << "You entered a total of " << count << " words.\n";
}
/**
輸出結果:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
*/
void exampleNo9(void)
{
string words;
cout << "Enter words (to stop, type the word done): \n";
int count = 0;
cin >> words; // 這語句的含義:將輸入流cin與words建立連接,
// 以空白作爲分隔符,遇到回車關閉該此連接;
while (words != "done") {
cin >> words;
count++;
}
cout << "You entered a total of " << count << " words.\n";
}
/**
輸出結果:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
*/
void exampleNo10(void)
{
cout << "Enter number of rows: ";
int rows;
cin >> rows;
for (int i=0; i<rows; i++) {
for (int j=0; j<rows; j++) {
if ((rows-1)-(j+i) > 0)
cout << ". ";
else
cout << "* ";
}
cout << endl;
}
}
/**
輸出結果:
Enter number of rows: 10
. . . . . . . . . *
. . . . . . . . * *
. . . . . . . * * *
. . . . . . * * * *
. . . . . * * * * *
. . . . * * * * * *
. . . * * * * * * *
. . * * * * * * * *
. * * * * * * * * *
* * * * * * * * * *
*/
【第五章】C++ Primer plus 的編程練習題(選取部分)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.