C++ Primer Plus(第6版)---编程作业完成(第二,三章)

第二章

#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
//函数原型
void Print_name(void);
int Long_convert(void);
void Print1(void);
void Print2(void);
void Age(void);
double Temper(double celsius);
double Light_year(double value);
void Time(int hour, int minute);
//主函数
int main()
{
	//Print_name();

	/*int convert;
	convert=Long_convert();
	cout << "转换为码为单位的距离数值为:" <<convert<< endl;*/

	/*Print1();
	Print1();
	Print2();
	Print2();*/

	//Age();

	/*double fahrenheit;
	double celsius;
	std::cout << "Please enter a Celsius value: " << endl;
	std::cin >> celsius;
	fahrenheit = Temper(celsius);
	std::cout <<celsius <<" degrees Celsius is " <<fahrenheit<<" degrees Fahrenheit." << endl;*/

	/*cout << "Enter the number of light years: " << endl;
	double value;
	cin >> value;
	double units=Light_year(value);
	cout <<value<< " light years = "<<units<<"astronomical units"<< endl;*/

	int hour, minute;
	cout << "Enter the number of hours: " ;
	cin >> hour;
	cout << "Enter the number of minutes: " ;
	cin >> minute;
	Time(hour, minute);

	return 0;
}
//------1、显示姓名和地址-------------
void Print_name(void)
{
	cout << "请输入您的姓名: " << endl;
	string name;
	cin >> name;
	cout << "请输入您的地址: " << endl;
	string address;
	cin >> address;
	cout << "姓名:"<<name<<" 地址:"<<address << endl;
	return;
}
//----------2、long转码------------
int Long_convert(void)
{
	int length;
	//long是类型名,不可以使用为变量名
	cout << "请输入以long为单位的距离数值:" << endl;
	cin >>length;
	return length * 220;
}
//----------3、输出函数------------
void Print1(void)
{
	cout << "Three blind mice" << endl;
}
void Print2(void)
{
	cout << "See how they run" << endl;
}

//-------4、计算输出年龄------------
void Age(void)
{
	using std::cout;
	using std::cin;
	cout << "Enter your age:" << endl;
	int age;
	cin >> age;
	int month = 12 * age;
	cout << "Your age in months is " <<month<<endl;
	return;
}

//----------5、温度函数------------
double Temper(double celsius)
{
	return celsius * 1.8 + 32.0;
}

//--------6、光年函数-----------------
double Light_year(double value)
{
	return value * 63240;
}

//--------7、时间函数---------------------
void Time(int hour,int minute)
{
	using namespace std;
	cout << "Time: " << hour << ":" << minute << endl;
}

第三章

#include <iostream>
# include <cmath>
using namespace std;
//函数原型
void height(void);
void convert_BMI(void);
void latitude(void);
void time(void);
void population(void);
void mile(void);
void fuel_consump(void);
//主函数
int main()
{
	/*height();*/

	/*convert_BMI();*/

	/*latitude();*/

	//time();

	//population();

	/*mile();*/

	fuel_consump();
	return 0;
}

//------作业一----------------
//一英尺=12英寸,英尺foot,英寸inch
void height(void)
{
	const int Factor = 12;//const定义的符号常量,用首字母大写来表示
	cout << "Enter a integer present your height: ___\b\b\b";
	int a,b,c;
	cin >> a;
	b = a / Factor;//英尺
	c = a % Factor;//英寸
	cout << "Your height is " << b << " foot " << c<<" inch " << endl;
	return;
}

//---------作业二---------------------------
void convert_BMI(void)
{
	//const符号常量
	const int Foot_inch = 12;
	const double Bonds_kg = 2.2;
	const double Inch_meter = 0.0254;

	int foot_input, inch_input ;
	double bond_input;
	cout << "Enter your weight using foot and inch" << endl;
	cout << "Enter the foot: " << endl;
	cin >> foot_input;
	cout << "Enter the inch: " << endl;
	cin >> inch_input;
	cout << "Enter your height using bond: " << endl;
	cin >> bond_input;

	//身高
	int height_inch ;
	double height_meter;
	height_inch = foot_input * Foot_inch + inch_input;
	height_meter = height_inch * Inch_meter;
	//体重
	double weight_kg;
	weight_kg = bond_input/Bonds_kg;
	//BMI
	double bmi;
	bmi = weight_kg / pow(height_meter,2);

	cout << "You BMI is " <<bmi<< endl;
	return;
}

//------------------作业三------------------------------------------------------
void latitude(void)
{
	//符号常量const
	const int Deg_min = 60;
	const int Min_sec = 60;

	cout << "Enter a latitude in degrees,minutes, and seconds: " << endl;

	int degree_input, minute_input, second_input;
	cout << "First,enter the degrees of  arc: ";
	cin >> degree_input;
	cout << "Next,enter the minutes of arc: ";
	cin >> minute_input;
	cout << "Finally,enter the seconds of arc: ";
	cin>> second_input;

	double degree, minute;
	minute = minute_input + second_input / Min_sec;
	degree = degree_input + minute / Deg_min;

	cout << degree_input << "degrees, "
		<< minute_input << "minutes, "
		<< second_input << "seconds="
		<< degree << "degrees\n";

	return;
}

//----------------作业四-------------------------------------
void time(void)
{
	//符号常量,const
	const int Day_hour = 24;
	const int Hour_min = 60;
	const int Min_sec = 60;

	cout << "Enter the number of seconds: ";
	long long second_input;
	cin >> second_input;

	int day, hour, minute, second;
	day = second_input / (Day_hour*Hour_min*Min_sec);
	cout << "hello" << endl;
	hour = (second_input % (Day_hour*Hour_min * Min_sec))/(60*60);
	cout << "hello" << endl;
	minute = (second_input %(60*60)/60);
	cout << "hello" << endl;
	second = second_input % 60;
	cout << "hello" << endl;

	cout << second_input << " seconds= "
		<< day << " days ," << hour << " hours, "
		<< minute << " minutes, " << second << " seconds. \n";
	return;
}

//-------------作业五--------------------------
void population(void)
{
	long long world, us;
	cout << "Enter the world's population: ";
	cin >> world;
	cout << "Enter the popultaion of US: ";
	cin >> us;
	double  proportion=double(us)/double(world);
	cout << "The population of the US is " << proportion*100 << "%"
		<< " of the word population";
	return;
}

//---------作业六---------------------------------------
//英里-mile,加仑gallon,升liter
void mile(void)
{
	double mile, gullon;
	cout << "Enter the mile: ";
	cin >> mile;
	cout << "Enter the gullon: ";
	cin >> gullon;
	double miles_per_gullon = mile / gullon;
	cout << "miles_per_gullon= " << miles_per_gullon << endl;
	return;
}


//-----------作业7---------------------------------------
void fuel_consump(void)
{
	//100Kilometer=62.14miles
	//1gullon=3.785liter
	//符号常量
	const double Klo_mile = 62.14/100;
	const double Liter_gullon = 1/3.785;

	double europe;
	cout << "Enter the fuel_consumption with the style of Europe: ";
	cin >> europe;
	double europe_kilometer = 100;
	double europe_liter = europe;
	double us_mile = europe_kilometer * Klo_mile;
	double us_gullon = europe_liter * Liter_gullon;

	double us = us_mile / us_gullon;
	cout << us << endl;

	return;
}

如有错误,欢迎指正! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值