《C++ primer plus》第六版课后编程题-第03章
第三章
1
#include <iostream>
using namespace std;
const int number = 12;
int main() {
cout << "请输入你的身高,单位为英寸" << endl;
int inch, feet;
cin >> inch;
feet = inch / number;
inch = inch % number;
cout << "您的身高为" << feet << "英尺" << inch << "英尺" << endl;
}
2
#include<iostream>
using namespace std;
const double meter_inch = 0.0254;
const double pound_kg = 2.2;
const double inch_feet = 12;
double BMI(double inch, double pound) {
double meter = inch * meter_inch;
double kg = pound / pound_kg;
return kg / meter / meter;
}
void main() {
double feet, inch, pound;
cout << "输入您的身高(英尺):" << endl;
cin >> feet;
cout << "输入您的身高(英寸):" << endl;
cin >> inch;
cout << "输入您的体重(磅):" << endl;
cin >> pound;
double inch_all = inch + inch_feet * feet;
double bmi = BMI(inch_all, pound);
cout << "您的BMI为" << bmi << endl;
}
3
#include<iostream>
using namespace std;
void main() {
cout << "Enter a latittude in degrees,minutes,and seconds:" << endl;
cout << "First, enter the degrees:";
double degrees, minutes, seconds;
cin >> degrees;
cout <<endl<< "Next, enter the minutes of arc:";
cin >> minutes;
cout << endl << "Finally, enter the seconds of arc:";
cin >> seconds;
cout << degrees << " degrees," << minutes << " minutes," << seconds << "= ";
double degrees_final = degrees + minutes / 60 + seconds / 60 / 60;
cout << degrees_final << " degrees." << endl;
}
4
#include<iostream>
using namespace std;
void main() {
int seconds_all;
cout << "Enter the number of seconds:";
cin >> seconds_all;
int temp = seconds_all;
int seconds = temp % 60;
temp=temp/ 60;
int minutes = temp % 60;
temp =temp/ 60;
int hours = temp % 24;
temp =temp/ 24;
int days = temp;
cout << seconds_all << " seconds = " << days << "days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << endl;
}
5
#include<iostream>
using namespace std;
void main() {
long long total, current;
cout << "Enter the world's population:";
cin >> total;
cout << "Enter the population of the US:";
cin >> current;
double percent = (double)current / (double)total;
cout << "The population of the US is " << percent << "% of the world population" << endl;
}
6
#include<iostream>
using namespace std;
void main() {
cout << "使用英里/加仑,请输入1" << endl << "使用公里/升,请输入2" << endl;
int input;
cin >> input;
if (input == 1) {
double miles, gallon;
cout << "请输入英里数:";
cin >> miles;
cout << "请输入加仑数:";
cin >> gallon;
cout << "每加仑可行驶" << miles / gallon << "英里" << endl;
}
else {
double km, L;
cout << "请输入公里数:";
cin >> km;
cout << "请输入多少升:";
cin >> L;
cout << "每100公里耗油" << 100*km/L<< "升" << endl;
}
}
7
#include<iostream>
using namespace std;
const double mpg_to_lpkm = 19.0 / 12.4
const double lpkm_to_mpg = 1 / mpg_to_lpkm;
void main() {
cout << "请输入欧洲风格耗油量(每百公里消耗多少升):" << endl;
double n;
cin >> n;
cout << n * mpg_to_lpkm;
}//这个题是想考语法的,但是数学太乱了,我不考虑数学了,数学解法乱写的
原文地址:https://blog.csdn.net/JimDu_dwj/article/details/145286687
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!