1. 程式人生 > >C++ Primer Plus第六版 第五章 程式設計練習答案

C++ Primer Plus第六版 第五章 程式設計練習答案

/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第1題 
Problem : 編寫一個要求使用者輸入兩個整數的程式。該程式將計算並輸出這兩個整數之間(包括這兩個整數)所有整數的和。
這裡假設先輸入較小的整數。例如,如果使用者輸入的是2和9,則程式將指出2-9之間所有整數的和為44.
*******************************************************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	cout << "請輸入兩個整數: ";
	int s , e;
	cin >> s >> e;
	int sum = 0;
	for(;s <= e; s ++)
		sum += s;
	cout << "整數和為" << sum << endl;
	return 0;
}

//第二題因為編譯器不支援array所以沒寫= =

時隔一年來填坑了(逃

 2015/08/05

//用G++4.8.3編譯的,注意G++預設是不開啟c++11的要加上-std=c++11 這個神坑啊!
#include <iostream>
#include <array>
int main()
{
	const int ArSize = 16;
	std::array<long double, ArSize> a = {1};
	for(int i = 1 ; i < a.size() ; ++i)
		a[i] = i * a[i - 1];
	for(int i = 0 ; i < a.size() ; ++i)
		std::cout << i << "! = " << a[i] << std::endl;
	return 0;
}

/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第3題 
Problem : 編寫一個要求使用者輸入數字的程式。每次輸入後,程式都將報告到目前為止,所有輸入的累計和。當用戶輸入0時,
程式結束
*******************************************************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	int data , sum = 0;
	cout << "請輸入一個數字(輸入0終止): ";
	while(cin >> data && data){
		cout << "到目前為止累計和為: " << (sum += data) << endl;
		cout << "請輸入一個數字(輸入0終止): ";
	}
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第4題 
Problem : Daphne以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 d = 100 , c = 100;
	int count = 0;
	while(c <= d){
		count++;
		d += 10;
		c *= 1.05;
	}
	cout << count << endl;
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第5題 
Problem : 假設要銷售《C++ For Fools》一書。請編寫一個程式,輸入全年中每個月的銷售量(圖書數量,而不是銷售額)。
程式通過迴圈,使用初始化為月份字串的char*陣列(或string物件陣列)逐月進行提示,並將輸入的資料儲存在一個int陣列中。
然後,程式計算陣列中各元素的總數,並報告這一年的銷售情況。
*******************************************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str1 = "請輸入第" , str2 = "月的銷售量: ";
	int month = 1 , sell[13] , sum = 0;
	for(;month <= 12; month++){
		cout << str1 << month << str2;
		cin >> sell[month];
		sum += sell[month];
	}
	cout << "總銷售額: " << sum << endl;
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第6題 
Problem : 完成程式設計練習5,但這一次使用一個二維陣列來儲存輸入——3年中每個月的銷售量。程式將報告每年的銷售量以及
三年的總銷售量。
*******************************************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str1 = "請輸入第" , str2 = "年第" , str3 = "月的銷售量: ";
	int year = 1 , month = 1 , sell[4][13] , sum[4] = {0};
	for(; year <= 3 ; year ++){
		for(month = 1; month <= 12 ; month ++){
			cout << str1 << year << str2 << month << str3;
			cin >> sell[year][month];
			sum[year] += sell[year][month];
		}
		cout << "第" << year << "年的銷售量是: " << sum[year] << endl;
	}
	cout << "總銷售額: " << sum[1] + sum[2] + sum[3] << endl;
	return 0;
}

/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第7題 
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>
#include <string>
using namespace std;
struct car{
	string name;
	int year;
};
int main()
{
	cout << "How many cars do you wish to catalog? ";
	int num;
	(cin >> num).get();
	car * c = new car[num];
	for(int i = 1 ; i <= num ; i ++){
		cout << "Car #" << i << ":" <<endl << "Please enter the make: ";
		getline(cin , c[i-1].name);
		cout << "Please enter the year made: ";
		(cin >> c[i-1].year).get();
	}
	cout << "Here is your collection:" << endl;
	for(int i = 0 ; i < num ; i ++)
		cout << c[i].year << " " << c[i].name << endl;
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第8題 
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()
{
	cout << "Enter words (to stop, type the word done):" << endl;
	char ch[20];
	int count = 0;
	while((cin >> ch) && strcmp(ch , "done"))
		count ++;
	cout << count << endl;
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第9題 
Problem : 編寫一個滿足前一個練習中描述的程式,但使用string物件而不是
字元陣列。請在程式中包含標頭檔案string,並使用關係運算符來進行比較測試
*******************************************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout << "Enter words (to stop, type the word done):" << endl;
	string str;
	int count = 0;
	while(cin >> str && str != "done")
		count ++;
	cout << count << endl;
	return 0;
}
	


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/06
From : C++ Primer Plus第六版第五章程式設計練習 第10題 
Problem : 編寫一個使用巢狀迴圈的程式,要求使用者輸入一個值,指出要顯示多少行。然後,程式將顯示相應的行數的星號,其中
第一行包括一個星號,第二行包括兩個星號,依次類推。每一行包含的字元數等於使用者指定的行數,在星號不夠的情況下,在星號
前面加上句點。該程式的執行情況如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
*******************************************************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	cout << "Enter number of rows: ";
	int rows;
	cin >> rows;
	for(int i = 1 ; i <= 5 ; i ++){
		for(int j = 1 ; j <= 5 - i ; j ++)
			cout << ".";
		for(int j = 1 ; j <= i ; j ++)
			cout << "*";
		cout << endl;
	}
	return 0;
}