CPP_Basic_Code_P3.1-PP3.7.6
// The Notes Created by Z-Tech on 2017/2/17.
// All Codes Boot on 《C++ Primer Plus》V6.0
// OS:MacOS 10.12.4
// Translater:clang/llvm8.0.0 &g++4.2.1
// Editer:iTerm 2&Sublime text 3
// IDE: Xcode8.2.1&Clion2017.1
//P3.1
#include <iostream>
#include <climits>
int main()
{
using namespace std;
int n_int=INT_MAX;
short n_short=SHRT_MAX;//指定最大值
long n_long=LONG_MAX;
long long n_llong=LLONG_MAX;
cout<<"int is "<<sizeof (int)<<" bytes."<<endl;//此处不用n_int而是对类型名int使用,所以必须加()
cout<<"short is "<<sizeof n_short<<" bytes."<<endl;
cout<<"long is "<<sizeof n_long<<" bytes."<<endl;
cout<<"long long is "<<sizeof n_llong<<" bytes."<<endl;
cout<<endl;
cout<<"maximum values:"<<endl;
cout<<"int: "<<n_int<<endl;
cout<<"short: "<<n_short<<endl;
cout<<"long: "<<n_long<<endl;
cout<<"long long: "<<n_llong<<endl<<endl;
cout<<"Minimum int value = "<<INT_MIN<<endl;//最小值
cout<<"Bits per byte = "<<CHAR_BIT<<endl;//字节的位数
return 0;
}
//P3.2
#include <iostream>//有无符号的short溢出后值的变化
#define ZERO 0
#include <climits>
int main()
{
using namespace std;
short sam= SHRT_MAX;
unsigned short sue=sam;
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl//注意此处并未结束
<<"Add $1 to each account."<<endl<<"Now ";
sam++;//32767+1>-32768;整形溢出跳转
sue++;//32767+1=32768
cout<<"Sam has "<<sam<<" dollars and sue has "<<sue;
cout<<" dollars deposited.\nPoor Sam!"<<endl;
sam=sue=ZERO;
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl;
cout<<"Take $1 from each account."<<endl<<"Now ";
sam--;//0-1=-1
sue--;//0-1>65535,整形溢出跳转
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl<<"Luck Sue!"<<endl;
return 0;
}
//P3.3
#include <iostream>
int main()
{
using namespace std;
int chest=42;//十进制
int waist=0x42;//十六进制
int inseam=042;//八进制
cout<<"Monsieur cuts a striking figure!\n";//默认情况下,cout始终以十进制输出
cout<<"chest= "<<chest<<" (42 in decimal)\n";
cout<<"waist= "<<waist<<" (0x42 in hex)\n";
cout<<"inseam= "<<inseam<<" (042 in octal)\n";
return 0;
}
//P3.4
#include <iostream>
using namespace std;
int main()
{
int chest=42;
int waist=42;
int inseam=42;
cout<<"Monsieur cuts a striking figure!"<<endl;
//默认输出即为十进制
cout<<"chest = "<<chest<<" (decimal for 42)"<<endl;
cout<<hex;//十六进制,对后方有效,且控制符位于std中
cout<<"waist = "<<waist<<" (hexadecimal for 42)"<<endl;
cout<<oct;//八进制
cout<<"inseam = "<<inseam<<" (octal for 42)"<<endl;
return 0;
}
//P3.5
#include <iostream>
int main()
{
using namespace std;
char ch;
cout<<"Enter a character: "<<endl;
cin>>ch;
cout<<"Hola! ";
cout<<"Thank you for the "<<ch<<" character."<<endl;
return 0;
}
//P3.6
#include <iostream>
int main()
{
using namespace std;
char ch='M';//如果没有''则将被视为一个未声明的变量
int i=ch;
cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
cout<<"Add one to the character code:"<<endl;
i=++ch;
cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
cout<<"Displaying char ch using cout.put(ch): ";
cout.put(ch);//输出ch所代表的字符
cout.put('!');//直接输出字符需要''
cout<<endl<<"Done!Done!Done!"<<endl;
return 0;
}
//P3.7
#include <iostream>
int main()
{
using namespace std;
cout<<"\aOperation \"HypeHype\" is now activated!\n";// \a为振铃符
cout<<"Enter your agent code:________\b\b\b\b\b\b\b\b";// \b为退格符,可移动光标的位置
long code;
cin>>code;
cout<<"\ayou entered "<< code <<"...\n";
cout<<"\aCode verified! Proceed with Plan Z3!\n";
return 0;
}
//P3.7.extra
#include <iostream>
int main()
{
using namespace std;
int k\u00F6rper;//变量名为Körper
cout<<"Let them eat g\u00E2teau.\n";//gâteau
return 0;
}
//P3.8
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);//强制显示小数点,不显示e表示法
float tub=10.0/3.0;//均被初始化为3.333333
double mint=10.0/3.0;
const float million=1.0e6;//十的六次方
cout<<"tub="<<tub;
cout<<", a million tubs = "<<million*tub;//这里float只精确到小数点后六位,故显示结果不正确
cout<<",\nand ten million tubs = ";
cout<<10*million*tub<<endl;//因为已经超出限制,再乘以十依然不正确
cout<<"mint = "<<mint<<" and a million mint = ";//double精度到15位,所以13位是有效的
cout<<million*mint<<endl;
return 0;
}
//P3.9
#include <iostream>
int main()
{
using namespace std;
float a=2.34E+22f;//2.34*10^22,当22变为6时,结果为1
float b=a+1.0f;//因为float只能表示前6-7位,所以会被系统忽略
cout<<"a = "<<a<<endl;
cout<<"b-a = "<<b-a<<endl;
return 0;
}
//P3.10
#include <iostream>
int main()
{
using namespace std;
float hats,heads;
cout.setf(ios_base::fixed,ios_base::floatfield);//修复小数点后的零,强制显示
cout<<"Enter a number: ";
cin>>hats;
cout<<"Enter another number: ";
cin>>heads;
cout<<"hats = "<<hats<<";heads = "<<heads<<endl;
cout<<"hats + heads = "<<heads+hats<<endl;
cout<<"hats * heads = "<<hats*heads<<endl;
cout<<"hats / heads = "<<hats/heads<<endl;
//cout<<"hats % heads = "<<hats%heads<<endl;该行求模不能使用浮点数
return 0;
}
//P3.11
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Integer division: 9/5 = " << 9 / 5 << endl;
cout << "Floating-Point division: 9.0/5.0 = ";
cout << 9.0 / 5.0 << endl;
cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
cout << "Double constant: 1e7/9.0 = ";
cout << 1.e7 / 9.0 << endl;//1e7和1.e7是等价的。而且不加参数默认double
cout<<"Float constant: 1e7f/9.0f = ";
cout<<1.e7f/9.0f<<endl;//两个数都为float,结果只能为float,6位有效数字
return 0;
}
//P3.12
#include <iostream>
int main()
{
using namespace std;
const int Lbs_per_stn=14;
int lbs;
cout<<"Enter your weight in pound: ";
cin>>lbs;
int stone=lbs/Lbs_per_stn;//计算整的Stone
int pounds=lbs%Lbs_per_stn;//计算余下的英镑数
cout<<lbs<<" Pounds are "<<stone<<" Stone , "<<pounds<<" Pound(s).\n";
return 0;
}
//P3.13
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);
float tree=3;
int guess(3.9832);//括号赋值法
int debt=7.2E12;//每一个编译器均不同
cout<<"Tree = "<<tree<<endl;
cout<<"guess = "<<guess<<endl;
cout<<"Debt = "<<debt<<endl;
return 0;
}
//P3.14
#include <iostream>
int main()
{
using namespace std;
int auks,bats,coots;
auks=19.99+11.99;
bats=(int) 19.99+(int) 11.99;//C 语言老式写法
coots=int (19.99)+ int (11.99);
cout<<"auks= "<<auks<<", bats= "<<bats;
cout<<", coots= "<<coots<<endl;
char ch='Z';
cout<<"The Code for "<<ch<<" is ";
cout<<int(ch)<<endl;
cout<<"Yes,the code is ";
cout<<static_cast<int>(ch)<<endl;
//调用static_cast<>将ch强制类型转换为int
return 0;
}
//PP3.7.1
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter your height by foot:__\b\b";//两个删除符号
int height;
cin>>height;
const int inchV=12;//转换因子inchV
int inch=height/inchV;
int foot=height%inchV;
cout<<"So your height is "<<inch<<" inch and "
<<foot<<" foot.";
return 0;
}
//PP3.7.2
#include <iostream>
int main()
{
using namespace std;
cout<<"Please enter your height by inch and foot(Use Space between them): ";
float inch,foot;//参考身高数据:5 inch&7.9 foot,约1.72m
cin>>inch>>foot;//参考体重数据:120 pound,约54.5kg
cout<<"Please enter your Weight by Pound : ";
float pound;
cin>>pound;
const int hcv=12;
const float wcv=2.2;
float mi=(inch*hcv+foot)*0.0254;
float kg=pound/2.2;
float bmi=kg/(mi*mi);
cout<<"So your height is "<<mi<<" metre and your weight is "<<kg<<" kg."<<endl;
cout<<"Then your BMI is "<<bmi<<endl;
cout<<"Thank you ver much.";
return 0;
}
//PP3.7.3
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter a latitude in degrees,minutes,and seconds: ";
cout<<"First,enter the degrees: ";
float degrees;
cin>>degrees;
cout<<"Next,enter the minutes: ";
float minutes;
cin>>minutes;
cout<<"Finally,enter the seconds: ";
float seconds;
cin>>seconds;
const int sv=60;//const定义需要指明具体的常量类型
cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds "
<<" = "<<degrees+minutes/sv+seconds/sv/sv<<" degrees.";
return 0;
}
//PP3.7.4
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter the numbers of seconds: ";
long allseconds;
cin>>allseconds;
int allhours=allseconds/3600;//全部小时数
short days=allhours/24;//除法计算天数
short hours=allhours%24;//求模算出小时数
long allminutes=allseconds/60;//全部分钟数
short minutes=allminutes%(allhours*60);//求模算出分钟数
short seconds=allseconds%60;//求模算出秒数
cout<<allseconds<<"seconds = "<<days<<" days "<<hours<<" hours "
<<minutes<<" minutes "<<seconds<<" seconds.";
return 0;
}
//PP3.7.5
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout<<"Enter the world's population: ";
long double wpplt;
cin>>wpplt;
cout<<"Enter the population of the U.S: ";
long double upplt;
cin>>upplt;
cout<<"The population of U.S is "<<(upplt/wpplt)*100<<"% of the world population.";//百分比要*100
return 0;
}
//PP3.7.6
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter your drive distance: ";
double distance;
cin>>distance;
cout<<"Next,enter the oil by 升: ";
float oil;
cin>>oil;
cout<<"100km run out of "<<(oil/distance)*100<<"升。";
return 0;
}