/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第1題
Problem : 編寫一個要求用戶輸入兩個整數的程序。該程序將計算並輸出這兩個整數之間(包括這兩個整數)所有整數的和。
這裏假設先輸入較小的整數。例如,如果用戶輸入的是2和9,則程序將指出2-9之間所有整數的和爲44.
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter two int number: \n";
int num[2];
for (int i=0; i<2;i++)
{
cin >> num[i];
}
if (num[0]>num[1])
{
int temp=0;
temp=num[0];
num[0]=num[1];
num[1]=temp;
}
int sum=0;
for (int i=num[0];i<=num[1];i++)
sum+=i;
cout << "The sum of the int number during " << num[0] << " and " << num[1] << " is " << sum <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第2題
Problem : 編寫一個要求用戶輸入數字的程序。每次輸入後,程序都將報告到目前爲止,所有輸入的累計和。當用戶輸入0時,
程序結束
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter double number (0 represents the end): \n";
double sum=0;
double data;
cin >> data;
while(data != 0)
{
sum+=data;
cout << "The sum of the data until now is " << sum <<endl;
cin >> data;
}
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第3題
Problem : 以10%的單利投資了100美元。也就是說,每一年的利潤都是投資額的10%,即每年10美元:
利息 = 0.10 * 原始存款
而Cleo以5%的複利投資了100美元。也就是說,利息是當前存款(包括獲得的利息)的5%:
利息 = 0.05 * 當前存款
Cleo在第一年投資100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,依次類推。請編寫一個
程序,計算多少年後,Cleo的投資價值才能超過Daphne的投資價值,並顯示此時兩個人的投資價值。
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
double saving_of_Daphne=100;
double saving_of_Cleo=100;
int year=0;
while(saving_of_Daphne >= saving_of_Cleo)
{
year++;
saving_of_Daphne=saving_of_Daphne+100*0.1;
saving_of_Cleo=saving_of_Cleo*(1+0.05);
}
cout << "After " << year << ", saving of Cleo is more than Daphne" <<endl;
cout << "Saving of Cleo is " << saving_of_Cleo << " and " << "saving of Daphne is " << saving_of_Daphne << endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第4題
Problem : 假設要銷售《C++ For Fools》一書。請編寫一個程序,輸入全年中每個月的銷售量(圖書數量,而不是銷售額)。
程序通過循環,使用初始化爲月份字符串的char*數組(或string對象數組)逐月進行提示,並將輸入的數據儲存在一個int數組中。
然後,程序計算數組中各元素的總數,並報告這一年的銷售情況。
*******************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
const int MONTH =12;
int main()
{
// 方法一:
char * month[MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};
// 方法二:string數組
string month1 [MONTH]={"January","February","March","April","May","June","July","August","September","October","November","December"};
// 方法三:char 數組
char month2[20][MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};
int sales[MONTH];
int sum=0;
for (int i=0;i<MONTH;i++)
{
cout << "Please enter the sales of " << month[i] << "\t";
cin >> sales[i];
sum+=sales[i];
}
for (int i=0;i<MONTH;i++)
cout << "The sales of " << month[i] << " is :\t" << sales[i] <<endl;
cout << "The sum of the whole year is:\t " << sum <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第5題
Problem : 完成編程練習4,但這一次使用一個二維數組來存儲輸入——3年中每個月的銷售量。程序將報告每年的銷售量以及
三年的總銷售量。
*******************************************************************************************************************/
#include<iostream>
using namespace std;
const int MONTH =12;
const int YEAR =3;
int main()
{
char * month[MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};
int sales[YEAR][MONTH];
int sum_per_year[3]={0};
int sum=0;
for (int j=0;j<YEAR;j++)
{
for (int i=0;i<MONTH;i++)
{
cout << "Please enter the sales of " << j+1 << "th year, " <<month[i] << "\t";
cin >> sales[j][i];
sum_per_year[j]+=sales[j][i];
}
sum+=sum_per_year[j];
}
for (int i=0;i<YEAR;i++)
cout << "The sum of the " << i+1 << " th year is:\t " << sum_per_year[i]<<endl;
cout << "The sum of the "<< YEAR <<" years is:\t " << sum <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第6題
Problem : 設計一個名爲car的結構,用它存儲下述有關汽車的信息: 生產商(存儲在字符數組或string對象中的字符串)、
生產年份(整數)。編寫一個程序,向用戶詢問有多少輛汽車。隨後,程序使用new來創建一個由相應數量的car結構組成的動態數組。
接下來,程序提示用戶輸入每輛車的生產商(可能由多個單詞組成)和年份信息。請注意,這需要特別小心,因爲它將交替讀取
數值和字符串(參見第4章)。最後,程序將顯示每個結構的內容。該程序的運行情況如下:
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
*******************************************************************************************************************/
#include<iostream>
using namespace std;
struct car
{
char maker[20];
int year;
};
int main()
{
cout << "How many cars do you wish to catalog? ";
int num;
(cin >> num).get();
car *p =new car[num];
for (int i=0;i<num;i++)
{
cout << "Car #" << i+1 << ":\n";
cout << "Please enter the make: ";
cin.getline(p[i].maker,20);
cout << "Please enter the year made: ";
(cin >> p[i].year).get();
}
cout << "Here is your collection: \n";
for (int i=0;i<num;i++)
cout << p[i].year << "\t" << p[i].maker << endl;
system("pause");
delete p;
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第7題
Problem : 編寫一個程序,它使用一個char數組和循環來每次讀取一個單詞,直到用戶輸入done爲止。隨後,該程序指出用戶輸入
了多少個單詞(不包括done在內)。下面是該程序的運行情況:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a toal of 7 words.
您應該在程序中包含頭文件cstring,並使用函數strcmp()來進行比較測試
*******************************************************************************************************************/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[20];
cout << "Enter words (to stop, type the word done): \n";
int num=0;
while(cin >> str && strcmp(str,"done"))
num++;
cout << "You entered a toal of " << num <<" words. \n";
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第8題
Problem : 編寫一個滿足前一個練習中描述的程序,但使用string對象而不是
字符數組。請在程序中包含頭文件string,並使用關係運算符來進行比較測試
*******************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cout << "Enter words (to stop, type the word done): \n";
int num=0;
while(cin >> str && (str!="done"))
num++;
cout << "You entered a toal of " << num <<" words. \n";
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章編程練習 第9題
Problem : 編寫一個使用嵌套循環的程序,要求用戶輸入一個值,指出要顯示多少行。然後,程序將顯示相應的行數的星號,其中
第一行包括一個星號,第二行包括兩個星號,依次類推。每一行包含的字符數等於用戶指定的行數,在星號不夠的情況下,在星號
前面加上句點。該程序的運行情況如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Enter number of rows: \n";
int row;
cin >> row;
for (int i=0; i<row ;i++)
{
for (int j=0; j<row-i-1 ;j++)
{
cout << ".";
}
for (int j=row-i-1; j<row ;j++)
{
cout << "*";
}
cout << endl;
}
system("pause");
return 0;
}