c++ Primer Plus 第六版 程式設計練習答案第三章
阿新 • • 發佈:2018-11-17
3.1
#include<stdafx.h> #include <iostream> using namespace std; const int inch_per_feet = 12;//一英尺(inch)等於12英寸(feet) int main() { int height,heigh_inch,heigh_feet; cout << "enter your height in inch=____\b\b\b\b ";//\b表示退格,與下劃線個數相同,用於指示輸入位置 cin >> height; heigh_inch = height / inch_per_feet; heigh_feet = height % inch_per_feet; cout << "your height is " << heigh_inch << " inch " << heigh_feet << " feet\n"; return 0; }
3.2
#include<stdafx.h> #include <iostream> using namespace std; const int inch_per_feet = 12;//一英尺(inch)等於12英寸(feet) const double feet_per_m = 0.0254;//一英寸等於0.0254米,注意由於後面定義的變數為double型別,這裡宣告也要用double const double kg_per_pound = 2.2; int main() { int height_inch, height_feet, weight_pound;//分別表示身高的英尺英寸和體重磅,以整數表示 double height_m,weight_kg,BMI;//身高米與體重kg以及BMI值都可能為小數,用double型別的變數,注意與前宣告型別保持一致 //身高體重的輸入 cout << "enter your heigh in inch:___\b\b"; cin >> height_inch; cout << "enter your heigh in feet:___\b\b"; cin >> height_feet; cout << "enter your weight in pound:___\b\b"; cin >> weight_pound; //身高米、體重kg單位的轉換 height_m = (height_inch*inch_per_feet + height_inch)*feet_per_m; weight_kg = kg_per_pound * weight_pound; //BMI值計算 BMI = weight_kg / (height_m*height_m);//注意這裡的平方不能用^2表示 cout << "your BMI is " << BMI << endl; return 0; }
3.3
#include<stdafx.h> #include <iostream> using namespace std; const int degree_per_minutes = 60; const int degree_per_second = 60*60; int main() { double degree, minute, second; double latitude; cout << "enter a latitude in dgree,minutes,and seconds:\n"; cout << "First,enter the degrees: "; cin >> degree; cout << "Next,enter the minutes of arc: "; cin >> minute; cout << "Finally,enter the second of arc: "; cin >> second; latitude = degree + minute / degree_per_minutes + second / degree_per_second;//轉換因子與變數至少要有一個為浮點型,否則這個計算結果將會捨去小數部分,導致計算不正確 cout << degree << "degrees, " << minute << " minutes," << second << " seconds= " << latitude << " degree\n"; return 0; }
3.4
#include<stdafx.h>
#include <iostream>
using namespace std;
const int day_per_hour = 12;
const int hour_per_minute = 60;
const int minute_per_second = 60;
int main()
{
long second_origi;//用於輸入秒數
int day,se_or_d,hour,se_or_h, minute, second;//均定義為int,用於捨去小數
cout << "enter the number of seconds: ";
cin >> second_origi;
day = second_origi / (minute_per_second*hour_per_minute*day_per_hour);
se_or_d = second_origi - day * (minute_per_second*hour_per_minute*day_per_hour);//存放原始秒數減去天所佔秒數的值,方便計算小時
hour= se_or_d / (minute_per_second*hour_per_minute);
se_or_h = se_or_d - hour * hour_per_minute*minute_per_second;//存放剩餘秒數
minute = se_or_h / minute_per_second;
second = se_or_h - minute * minute_per_second;
cout << second_origi << " seconds is " << day << "days, "<<hour<<" hours, "<<minute<<" minutes, "<<second<<" seconds\n";
return 0;
}
3.5
#include<stdafx.h>
#include <iostream>
using namespace std;
int main()
{
long long A_p, W_p;
double rate;
cout << "enter the world's population: ";
cin >> W_p;
cout << "enter the population of the US: ";
cin >> A_p;
rate = double (A_p) / W_p * 100;//由於用long long 儲存的變數,必須至少將兩個變數中的一個轉化為浮點型資料,否則求出的比率為0,捨去小數
cout << "The population of the US is " << rate << "% of the world population";
return 0;
}
3.6
#include<stdafx.h>
#include <iostream>
using namespace std;
int main()
{
double distance, A_g, oil_con;//汽油量可能為小數,變數定義為double型別
cout << "enter the distance in kilometer: ";
cin >> distance;
cout <<"enter the amount of gasoline in litre: ";
cin >> A_g;
oil_con = A_g/ distance * 100;
cout << "the oil consumption per 100 kilometers is " << oil_con;
return 0;
}
3.7
#include<stdafx.h>
#include <iostream>
using namespace std;
const double kilo_100_mile = 62.14;
const double gallon_per_L = 3.875;
int main()
{
double con_in_Eur,con_in_US;
cout << "enter the oil consumption in Europe method/100km: ";
cin >> con_in_Eur;
con_in_US =kilo_100_mile/con_in_Eur*gallon_per_L;//直接將歐洲風格的耗油量轉換為美國風格的
cout << "the oil consumption in America style is " << con_in_US << "mpg";
return 0;
}