复习题
1
一种整型不能满足编程的需求
2
short shit=80;
unsigned shit=42110;
auto shit=3000000000;
3
没提供措施,自己小心
4
33L是long类型,33是int类型
5
等价
6
(1) int shit=88;std::cout<<(char)shit;
(2) char shit=88;std::cout<<shit;
7
如果浮点型中用于存储小数的位数>=整型的位数,就不会有舍入误差。否则,就会有。
查limits.h和float.h,自己解决。
8
74 4 0 4.5 3
9
(1) int shit=(int)x1+(int)x2;
(2) int shit=x1+x2;
10
int float char char32_t double
编程练习
3.1
#include<iostream>
int main(void)
{
using namespace std;
cout<<"输入你的身高,以英寸为单位\n";
int inch;
cin>>inch;
cout<<"你身高"<<inch/12<<"英尺"<<inch%12<<"英寸\n";
return 0;
}
3.2
#include<iostream>
int main(void)
{
using namespace std;
const int inch_per_foot=12;
const double meter_per_inch=0.0254;
const double pound_per_kg=2.2;
cout<<"输入你的身高,以英尺和英寸的方式\n你身高的英尺数:";
int foot;
cin>>foot;
cout<<"你身高的英寸数:";
int inch;
cin>>inch;
cout<<"输入你的体重,以磅为单位\n你的体重是:";
int pound;
cin>>pound;
inch=foot*12+inch;
double meter=inch*meter_per_inch;
double kg=pound/pound_per_kg;
double BMI=kg/meter/meter;
cout<<"你的身高 "<<meter<<"米\n你的体重 "<<kg<<"公斤\n你的BMI "<<BMI<<endl;
return 0;
}
3.3
#include<iostream>
int main(void)
{
using namespace std;
const int minutes_per_degrees=60;
const int seconds_per_minutes=60;
cout<<"Enter a latitude in degrees,minutes,and seconds:\n";
cout<<"First enter the degrees:";
int degrees;
cin>>degrees;
cout<<"Next,enter the minutes of arc:";
int minutes;
cin>>minutes;
cout<<"Finally,enter the seconds of arc:";
int seconds;
cin>>seconds;
cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "
<<degrees+(double)minutes/minutes_per_degrees+(double)seconds/minutes_per_degrees/seconds_per_minutes
<<"degrees";
return 0;
}
3.4
#include<iostream>
int main(void)
{
using namespace std;
const int hpd=24;
const int mph=60;
const int spm=60;
const int spd=hpd*mph*spm;
const int sph=mph*spm;
cout<<"Enter the number of seconds:";
long seconds;
cin>>seconds;
cout<<seconds<<" seconds = "<<seconds/spd<<" days, "<<seconds%spd/sph<<" hours, "
<<seconds%spd%sph/spm<<" minutes, "<<seconds%spd%sph%spm<<" seconds\n";
return 0;
}
3.5
#include<iostream>
int main(void)
{
using namespace std;
cout<<"Enter the world's population:";
long long world;
cin>>world;
cout<<"Enter the population of the US:";
long long US;
cin>>US;
cout<<"The population of the US is "<<(double)US/world*100<<"% of the world population.\n";
return 0;
}
3.6
#include<iostream>
int main(void)
{
using namespace std;
const double mpk=0.6214;
const double lpg=3.785;
cout<<"输入驱车里程(英里) ";
double mile;
cin>>mile;
cout<<"输入耗油量(加仑) ";
double gallon;
cin>>gallon;
cout<<"汽车耗油量为"<<mile/gallon<<"英里每加仑\n";
double km=mile/mpk;
double liter=gallon*lpg;
cout<<"每百公里耗油量为"<<liter/km*100<<"升\n";
return 0;
}
3.7
#include<iostream>
int main(void)
{
using namespace std;
const double mpk=0.6214;
const double lpg=3.785;
cout<<"输入每百公里耗油量(升) ";
double liter;
cin>>liter;
double gallon;
gallon=liter/lpg;
double mile;
mile=mpk*100;
cout<<"每百公里耗油量为"<<liter<<"升\n";
cout<<"汽车耗油量为"<<mile/gallon<<"英里每加仑\n";
return 0;
}