C++ Primer Plus第五版 第二章 編程練習答案

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第1題  
Problem : 編寫一個C++程序,它顯示您的姓名和地址。 
*******************************************************************************************************************/  
#include <iostream>  
using namespace std;  
int main()  
{  
	cout << "NAME :David" << endl; 
	cout << "ADRESS :BUPT" << endl;
	system("pause");
	return 0;  
}  


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第2題  
Problem : 編寫一個C++程序,它要求用戶輸入一個以long爲單位的距離, 
然後將它轉換爲碼(一long等於220碼)。 
*******************************************************************************************************************/  
#include <iostream>  
using namespace std;  
const int LTY =220;
int main()  
{  
	cout << "Enter a distence in long: " << endl; 
	int dis;
	cin >> dis; 
	cout << "The distnce is " << dis * LTY << " yards" <<endl;
	system("pause");
	return 0;  
}  

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第3題  
Problem : 編寫一個C++程序,它使用3個用戶定義的函數(包括main()), 
並生成下面的輸出: 
Three blind mice 
Three blind mice 
See how they run 
See how they run 
其中一個函數要調用兩次,該函數生成前兩行;另一個函數也被調用兩次, 
並生成其餘的輸出。  
*******************************************************************************************************************/   
#include <iostream>  
	using namespace std;  
void string1(void);  
void string2(void); 
int main()  
{  
	string1(); 
	string1();  
	string2();  
	string2();  
	system("pause");
	return 0;  
}  
void string1(void)
{  
	cout << "Three blind mice" << endl;  
}  
void string2(void)
{  
	cout << "See how they run" << endl;  
}  


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第4題  
Problem : 編寫一個程序,期中的main()調用一個用戶定義的函數(以 
攝氏溫度值爲參數,並返回相應的華氏溫度值)。改程序按下面的格式 
要求用戶輸入攝氏溫度值,並顯示結果: 
Please enter a Celsius value: 20 
20 degrees Celsius is 68 degrees Fahrenheit. 
下面是轉換公式: 
華氏溫度 = 1.8 * 攝氏溫度 + 32.0  
*******************************************************************************************************************/   
#include <iostream>  
using namespace std;  
double change(double n);
int main()  
{  
	cout << "Please enter a Celsiius value: ";
	double c;
	cin >> c; 
	cout << c << " degrees Celsius is " << change(c) << " degrees Fahrenheit." << endl;
	system("pause");
	return 0;  
}  

double change(double n) 
{  
	return 1.8 * n + 32.0;  
} 

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第5題  
Problem : 編寫一個程序,其main()調用一個用戶定義的函數 (以光年值爲參數,並返回對應天文單位的值)。該程序按下 
面的格式要求用戶輸入光年值,並顯示結果: 
Enter the number of light years: 4.2 
4.2 light years = 265608 astronomical units. 
天文單位是從地球到太陽的平均距離(約150000000公里或 93000000英里),光年是光一年走的距離(約10萬億公里或 
6萬億英里)(除太陽外,最近的恆星大約離地球4.2光年)。 請使用double類型,轉換公式爲: 1光年 = 63240天文單位
*******************************************************************************************************************/   
#include <iostream>  
using namespace std;  
double change(double n);
int main()  
{  
	cout << "Enter the number of light years: ";
	double ly;
	cin >> ly; 
	cout << ly << " light years = " << change(ly) << " astronomical units." << endl;
	system("pause");
	return 0;   
}  
double change(double n)  
{  
	return n * 63240;  
}  

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第二章編程練習 第6題  
Problem :  編寫一個程序,要求用戶輸入小時數和分鐘數。在main()函數 
中,將這兩個值傳遞給一個void函數,後者以下面這樣的格式顯示這兩個值: 
Enter the number of hours: 9 
Enter the number of minutes: 28 
Time: 9:28 
*******************************************************************************************************************/   
#include <iostream>  
using namespace std;  
void show_time(int, int);
int main()  
{  
	int hours,minutes;
	cout << "Enter the number of hours: ";
	cin >> hours;
	cout << "Enter the number of minutes: ";
	cin >> minutes;
	show_time(hours,minutes);
	system("pause");
	return 0;   
}  
void show_time(int hours, int minutes)
{
	cout << "Time: " << hours << ":" << minutes <<endl;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章