第三章 处理数据
第一题:用户输入身高,单位英寸,转换成几英寸几英尺输出。并用下划线提示输入位置,用const符号常量表示转换因子。
#include<iostream>
using namespace std;
int main(void)
{
const double a = 0.0833;
cout << "Enter your height in inch:___" << "\b\b\b";
int height_inch;
cin >> height_inch;
int feet;
double inch;
feet = int(height_inch*a);
inch = height_inch - feet/a;
cout << "\nYour height is : " << feet << " feet ," << inch << " inch. ";
getchar();
cin.get();
return 0;
}
ps:题目要求用几英寸几英寸表示,不是分别表示英尺的整数和小数部分。
第二题:用户以几英尺几英寸形式输入身高,并以磅数输入其体重,使用三个变量来存储,最后计算BMI:体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include<iostream>
using namespace std;
int main(void)
{
cout << "Your height in form of n foot and m inch." << endl;
double height_feet, height_inch;
cout << "Please enter the foot value :n = ";
cin >> height_feet;
cout << "Please enter the inch value :m= ";
cin >> height_inch;
cout << "Your weight is ___ pound." << "\b\b\b\b\b\b\b\b\b\b";
double pound;
cin >> pound;
const double a = 12;
const double b = 0.0254;
const double c = 1 / 2.2;
double height_meter,tizhong;
cout << "You are : " << height_feet*a + height_inch << " inch. \n";
height_meter = (height_feet*a + height_inch)*b;
tizhong = pound*c;
cout << "Your BMI value is :" << tizhong /height_meter /height_meter<<endl;
getchar();
cin.get();
return 0;
}
第三题:用户以度分秒输入一个维度,转换成以度显示。转换因子以符号常量表示,并对每个输入量有一个独立常量存储
#include<iostream>
using namespace std;
int main(void)
{
cout << "Enter a latitude in degrees, minutes,and seconds "<<endl ;
cout << "First,enter the degrees: ";
double degrees;
cin >> degrees;
cout << "Next,enter the minutes of arc: ";
double minutes;
cin >> minutes;
cout << "Finally,enter the seconds of arc: ";
double seconds;
cin >> seconds;
const double trans = 60;
cout << degrees << "degrees , " << minutes << " minutes , ";
cout<< seconds << " seconds = " << degrees + minutes / trans + seconds / trans / trans <<" degrees."<<endl ;
getchar();
cin.get();
}
第四题:用户输入秒数,以符号常量表示每天有多少小时,多少分,多少秒。并转换为天,小时,分,秒的形式输出。
#include<iostream>
using namespace std;
int main(void)
{
cout << "Enter the number of seconds: ";
long seconds;
cin>>seconds;
const int minutes = seconds / 60;
const int hours = minutes / 60;
const int days = hours / 24;
cout << seconds << " seconds = " << days << " days ";
cout << hours%24<<" hours " ;
cout << minutes%60 << " minutes ";
cout << seconds%60 << " seconds. \n ";
getchar();
cin.get();
return 0;
}
ps:注意余数的用法
第五题:要求用户输入驾驶里程(英里),和耗油量(加仑),计算1加仑能形式的里程。
#include<iostream>
using namespace std;
int main(void)
{
cout << "Enter your car miles: " ;
double miles;
cin >> miles;
cout << "Enter your car gas (gallen): ";
double gas;
cin >> gas;
cout << "A gallen ofgas can run " << double(miles)/gas << " miles." << endl;
getchar();
getchar();
return 0;
}
第六题:要求用户输入每100公里的耗油量(升),转换为每加仑能行驶多少英里。1加仑为3.875升,100公里为62.14英里。
#include<iostream>
using namespace std;
int main(void)
{
cout << "Please enter your car cost with Europe. \n";
cout << "100 km cost gas __ L.\b\b\b\b\b";
double gas_L;
cin >> gas_L;
cout << "Your car's cost is " << 62.14/(gas_L/3.875) << " mpg" << endl;
getchar();
getchar();
return 0;
}
ps:注意单位及显示风格差异即可。
第三章结束。.