【C++学习】习题记录 第三章 处理数据

第三章 处理数据

第一题:用户输入身高,单位英寸,转换成几英寸几英尺输出。并用下划线提示输入位置,用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;
}

结果显示3.1

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;
}

结果显示2.1

第三题:用户以度分秒输入一个维度,转换成以度显示。转换因子以符号常量表示,并对每个输入量有一个独立常量存储

#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();
}

结果显示3.3

第四题:用户输入秒数,以符号常量表示每天有多少小时,多少分,多少秒。并转换为天,小时,分,秒的形式输出。

#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;
}

结果显示3.4

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;
}

结果显示3.5

第六题:要求用户输入每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;
}

结果显示3.6

ps:注意单位及显示风格差异即可。

第三章结束。.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉木渡香

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值