add chapter 3

This commit is contained in:
kuoi 2022-08-23 22:44:15 +01:00
parent 6ce7b718d1
commit 06807d8731
7 changed files with 111 additions and 0 deletions

13
ch3/01.cpp Normal file
View file

@ -0,0 +1,13 @@
# include <iostream>
using namespace std;
const int feet2inch = 12;
int main(){
int height;
cout << "Enter Your Height (feet): ";
cin >> height;
int inch = height / feet2inch;
int feet = height % feet2inch;
cout << "Your Height is " << inch << " inch " << feet << " feet" << endl;
return 0;
}

26
ch3/02.cpp Normal file
View file

@ -0,0 +1,26 @@
# include <iostream>
# include <cmath>
using namespace std;
int ic2ft = 12; // 1 inch = 12 feet
double ft2m = 0.0254; // 1 feet = 0.0254 meter
float kg2p = 2.2;
double bmi (double a, double b);
int main(){
double inch, feet;
double pound;
cout << "Enter Your Height (Inch feet): ";
cin >> inch >> feet;
double meter = (inch * ic2ft + feet) * ft2m;
cout << "Enter Your Weight (pound): ";
cin >> pound;
double kg = pound / kg2p;
double c = bmi (kg, meter);
cout << "The BIM is " << c << endl;
return 0;
}
double bmi (double a, double b){
double c = 1 / pow(b,2) * a;
return c;
}

18
ch3/03.cpp Normal file
View file

@ -0,0 +1,18 @@
# include <iostream>
# include <cmath>
using namespace std;
int conv = 60;
int main(){
int degree, minute, second;
cout << "Enter a latitude in degrees, minutes and second" << endl;
cout << "First, enter the degrees: ";
cin >> degree;
cout << "Next, enter the minutes: ";
cin >> minute;
cout << "Finally, enter the seconds: ";
cin >> second;
float degree_f = degree + minute/float(conv) + second/float(pow(conv,2));
cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << degree_f << " degrees" << endl ;
return 0;
}

17
ch3/04.cpp Normal file
View file

@ -0,0 +1,17 @@
# include <iostream>
# include <cmath>
using namespace std;
long convt = 60;
long convd = 24;
int main(){
long time_all;
cout << "Enter the number of seconds: ";
cin >> time_all;
int day, hour, minute, second;
day = time_all / pow(convt,2) / convd;
hour = (time_all - day * pow(convt,2) * convd)/ pow(convt,2);
minute = (time_all - day * pow(convt,2) * convd - hour * pow(convt,2)) / pow (convt,1);
second = time_all - day * pow(convt,2) * convd - hour * pow(convt,2) - minute * pow (convt,1);
cout << time_all << " secods = " << day << " days, " << hour << " hours, " << minute << " minutes, " << second << " seoncds\n";
return 0;
}

11
ch3/05.cpp Normal file
View file

@ -0,0 +1,11 @@
# include <iostream>
using namespace std;
int main(){
long long wd_pop = 6898758899, us_pop = 310783781;
double rate = double(us_pop) / double(wd_pop);
cout << "There are " << wd_pop << " people in the world" << endl;
cout << "There are " << us_pop << " people in the US" << endl;
cout << "The population of the US is " << rate << "% of the world population" << endl;
return 0;
}

13
ch3/06.cpp Normal file
View file

@ -0,0 +1,13 @@
# include <iostream>
using namespace std;
int main(){
double ga, mi;
cout << "The usage of oil (Gallon): ";
cin >> ga;
cout << "The miles you drive: ";
cin >> mi;
double mi_per_ga = mi / ga;
cout << "Every gallon, the driver can drive " << mi_per_ga << " mile(s)" << endl;
return 0;
}

13
ch3/07.cpp Normal file
View file

@ -0,0 +1,13 @@
# include <iostream>
using namespace std;
double km2mile = 0.6214;
double ga2l = 3.875;
int main(){
double ga, mi, eu_rate;
cout << "The usage of oil (L) per 100 km: ";
cin >> eu_rate;
double us_rate = km2mile * ga2l / eu_rate;
cout << "Every gallon, the driver can drive " << us_rate << " mile(s)" << endl;
return 0;
}