Programming exercises in Chapter 3 of C++ Primer Plus

Keywords: Programming REST

outline

The programming exercises in this chapter mainly focus on the application of different data types (when to use what data types are better) and type conversion (the definition of data types is different from the type required in actual calculation)

3.1 feet and inches for height

#include<iostream>
using namespace std;
int main(){
	const float changeFactor = 12.0;
	//Input height
	int n_tall;
	float f_tall;
	cout<<"Please enter your height_____\b\b\b\b\b";
	cin>>n_tall;
	cout<<"Your height is "<<n_tall<<"inch"<<endl;
	f_tall = float(n_tall/changeFactor);
	cout<<"In feet, in your height"<<f_tall<<"Foot"<<endl;
	return 0;
}

Note: do not add \ n after \ b \ b \ b, and do not change the line, otherwise the meaning of using \ b to back off will be lost.

3.2 find BMI index

#include<iostream>
using namespace std;

int main()
{
	//Conversion factor
	const int Inch = 12;
	const float Meters = 0.0254;
	const float Pounds = 2.2;
	
	float tallInch;//inch
	int tallFeet;//Foot 
	float meter;//rice
	float weight;//weight
	cout<<"Foot:";
	cin>>tallFeet;
	cout<<"\n inch:";
	cin>>tallInch;
	cout<<"\n Weight (LBS):";
	cin>>weight;
	
	//Convert height to inches
	tallInch = tallFeet*Inch+tallInch; 
	//Convert height to meters
	meter = tallInch*Meters;
	//Convert weight to kg
	weight = weight/Pounds;
	
	//Calculate BMI index 
	float bmi = weight /(meter*meter);
	
	cout<<"Your BMI The index is:"<<bmi<<endl;
	return 0;
}

3.3 display the dimension in degrees

#include<iostream>
using namespace std;

int main(){
	const int Six = 60;
	
	int degrees,minutes,seconds;
	cin>>degrees>>minutes>>seconds;
	cout<<"test float(minutes/Six): " <<float(minutes/Six)<<endl; 
	//float f_degrees = (float)degrees+float(minutes/Six)+float(seconds/(Six*Six));
	//This is wrong, because the initial calculation is in int, so the sub calculation will be discarded, so even after the calculation is completed, it is useless to convert to float 
	float f_degrees = (float)degrees+float(minutes)/float(Six)+float(seconds)/float(Six*Six);
	cout<<"Output in degrees:"<<f_degrees;
	return 0;
}

Note: note the time point of the cast. Because the initial calculation is in int, so the sub calculation will be discarded, so even if the calculation is finished and then converted to float, there is no use. So the two that should divide are cast to float.

3.4 display time in days, hours, minutes and seconds

#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<<"The number of seconds entered is:"<<seconds<<endl;
	cout<<seconds/(DayHour*HourMinu*MinuSeco)<<"day,"<<seconds/(HourMinu*MinuSeco)%DayHour<<"Hour,"<<seconds/MinuSeco%HourMinu<<"Minute,"<<seconds%MinuSeco<<"second\n"; 
	//To use modulus is to find the rest that cannot be integrated with them, and divide it into hours, minutes and seconds 
	return 0;
}

Note: for example, if you want to find out how many hours are left and how many hours are left that can last for 24 hours, you can't count the remaining hours for 24 hours, that is, you can't count the whole day, but you can count the hours, so you can count seconds/(HourMinu*MinuSeco)%DayHour, which is similar to finding out all the hours and then% 24 hours Time does not include minutes and seconds. In seconds%MinuSeco's% MinuSeco is a module that doesn't make up for 60 seconds.

3.5 proportion of national population

#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: it needs to be converted to float type and then to percentage * 100

3.6 indicate fuel consumption per 100 km (L)

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

3.7 calculation of fuel consumption in different styles

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

Summary merger

  1. Note: do not add \ n after \ b \ b \ b, and do not change the line, otherwise the meaning of using \ b to back off will be lost.
  2. Note: note the time point of the cast. Because the initial calculation is in int, so the sub calculation will be discarded, so even if the calculation is finished and then converted to float, there is no use. So the two that should divide are cast to float.
  3. Note: for example, if you want to find out how many hours are left and how many hours are left that can last for 24 hours, you can't count the remaining hours for 24 hours, that is, you can't count the whole day, but you can count the hours, so you can count seconds/(HourMinu*MinuSeco)%DayHour, which is similar to finding out all the hours and then% 24 hours Time does not include minutes and seconds. In seconds%MinuSeco's% MinuSeco is a module that doesn't make up for 60 seconds.
  4. Note: it needs to be converted to float type and then to percentage * 100
Published 6 original articles, won praise 0, visited 1597
Private letter follow

Posted by SilverFoxDesign on Sun, 19 Jan 2020 02:01:04 -0800