C ++ Primer Plus program of the third chapter exercises

Overview

Programming exercises in this chapter mainly on the use of different types of data (at what time what kind of data type is better) and type conversion (type-defined data types required with the actual calculation time is not the same)

3.1 feet and inches tall

#include<iostream>
using namespace std;
int main(){
	const float changeFactor = 12.0;
	//输入身高
	int n_tall;
	float f_tall;
	cout<<"请输入你的身高_____\b\b\b\b\b";
	cin>>n_tall;
	cout<<"你的身高为 "<<n_tall<<"英寸"<<endl;
	f_tall = float(n_tall/changeFactor);
	cout<<"用英尺表示,你的身高表示"<<f_tall<<"英尺"<<endl;
	return 0;
}

Note: Do not \ b \ b \ b \ b back plus \ n, not to change jobs, or use \ b fallback, there is no meaning.

3.2 seeking BMI index

#include<iostream>
using namespace std;

int main()
{
	//转换因子
	const int Inch = 12;
	const float Meters = 0.0254;
	const float Pounds = 2.2;
	
	float tallInch;//英寸
	int tallFeet;//英尺 
	float meter;//米
	float weight;//体重
	cout<<"英尺:";
	cin>>tallFeet;
	cout<<"\n英寸:";
	cin>>tallInch;
	cout<<"\n体重(磅):";
	cin>>weight;
	
	//将身高转换为英寸
	tallInch = tallFeet*Inch+tallInch; 
	//将身高转换为米
	meter = tallInch*Meters;
	//将体重转换为千克
	weight = weight/Pounds;
	
	//计算BMI指数 
	float bmi = weight /(meter*meter);
	
	cout<<"你的BMI指数为:"<<bmi<<endl;
	return 0;
}

3.3 degrees to display the dimension

#include<iostream>
using namespace std;

int main(){
	const int Six = 60;
	
	int degrees,minutes,seconds;
	cin>>degrees>>minutes>>seconds;
	cout<<"测试float(minutes/Six):" <<float(minutes/Six)<<endl; 
	//float f_degrees = (float)degrees+float(minutes/Six)+float(seconds/(Six*Six));
	//这种是错的,因为最开始是用int进行计算,这样子计算会舍掉 ,这样子就算计算完再转换为float也没有什么用了 
	float f_degrees = (float)degrees+float(minutes)/float(Six)+float(seconds)/float(Six*Six);
	cout<<"以度为单位输出:"<<f_degrees;
	return 0;
}

Note: Note that point in time forced conversion. Since the beginning of the calculation is an int, the calculation would give it like this, like this even complete calculation then converted to float there is no use. So it should be divided by two is cast to type float up.

3.4 in days, hours, minutes and seconds the display time

#include<iostream>
using namespace std;

int main(){
	const int DayHour = 24;
	const int HourMinu = 60;
	const int MinuSeco = 60;
	
	long long seconds;
	cin>>seconds;
	cout<<"输入的秒数为:"<<seconds<<endl;
	cout<<seconds/(DayHour*HourMinu*MinuSeco)<<"天,"<<seconds/(HourMinu*MinuSeco)%DayHour<<"小时,"<<seconds/MinuSeco%HourMinu<<"分钟,"<<seconds%MinuSeco<<"秒\n"; 
	//用求模就是为了求出不能跟他们为一个整体的剩余的,划分到小时分钟和秒数里去 
	return 0;
}

Note: The modulus can not be the equivalent of a whole and the rest scattered, such as seeking how many hours can be lobbied the property 24 hours a day gone, the remaining hours can not scrape together a full 24 hours, That can not scrape together a full day, but to meet the number of hours, so you can seconds / (HourMinu * MinuSeco)% DayHour, similar to Determine all hours and% 24 hours, these hours to minutes and seconds do not include it . The number of seconds in seconds% MinuSeco% MinuSeco is of 60 seconds has not got enough to die out.

National proportion of people seeking 3.5

#include<iostream>
using namespace std;
int main(){
	long long china;
	long long global;
	cin>>china>>global;
	cout<<float(china)/float(global)*100<<"%"<<endl;
	return 0;
} 

Note: You need converted to float, and then into the percentage required * 100

3.6 indicates that for every 100 km fuel consumption (liters)

#include<iostream>
using namespace std;
int main(){
	double distance;
	double oilCost;
	cin>>distance;
	cin>>oilCost;
	cout<<distance/oilCost<<endl;
	cout<<float(distance)/float(oilCost);
	return 0;
} 

Fuel consumption of 3.7 different styles of computing

#include<iostream>
using namespace std;
int main(){
	float oilCost;
	cin>>oilCost;
	cout<<62.14/(oilCost/3.875); 
	return 0;
}

The combined summary

  1. Note: Do not \ b \ b \ b \ b back plus \ n, not to change jobs, or use \ b fallback, there is no meaning.
  2. Note: Note that point in time forced conversion. Since the beginning of the calculation is an int, the calculation would give it like this, like this even complete calculation then converted to float there is no use. So it should be divided by two is cast to type float up.
  3. Note: The modulus can not be the equivalent of a whole and the rest scattered, such as seeking how many hours can be lobbied the property 24 hours a day gone, the remaining hours can not scrape together a full 24 hours, That can not scrape together a full day, but to meet the number of hours, so you can seconds / (HourMinu * MinuSeco)% DayHour, similar to Determine all hours and% 24 hours, these hours to minutes and seconds do not include it . The number of seconds in seconds% MinuSeco% MinuSeco is of 60 seconds has not got enough to die out.
  4. Note: You need converted to float, and then into the percentage required * 100
Released nine original articles · won praise 0 · Views 1802

Guess you like

Origin blog.csdn.net/weixin_46021869/article/details/104040579