标签:小数点 sam max enter键 primer visio ber size pen
1 #include<iostream> 2 #include<climits> 3 using namespace std; 4 5 void main() 6 { 7 cout<<"int is "<<sizeof(int)<<" bytes"<<endl; 8 cout<<"short is "<<sizeof(short)<<" bytes"<<endl; 9 cout<<"long is "<<sizeof(long)<<" bytes"<<endl; 10 cout<<"long long is "<<sizeof(long long)<<" bytes"<<endl<<endl; 11 12 cout<<"Maximum values: "<<endl; 13 cout<<"int: "<<INT_MAX<<endl; 14 cout<<"short: "<<SHRT_MAX<<endl; 15 cout<<"long: "<<LONG_MAX<<endl; 16 cout<<"long long: "<<LLONG_MAX<<endl<<endl; 17 18 cout<<"Minimum int value= "<<INT_MIN<<endl; 19 cout<<"bits per Byte: "<<CHAR_BIT<<endl; 20 21 system("pause"); 22 }
1 #include<iostream> 2 #include<climits> 3 using namespace std; 4 5 void main() 6 { 7 short sam=SHRT_MAX; 8 unsigned short sue=sam; 9 cout<<"Sam有"<<sam<<"元,Sue有"<<sue<<"元"<<endl; 10 cout<<"再给他们一元"<<endl; 11 sam++,sue++; 12 cout<<"现在,Sam有"<<sam<<"元,Sue有"<<sue<<"元"<<endl; 13 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; 14 cout<<"回到10年前一无所有的时候"<<endl; 15 sam=sue=0; 16 cout<<"10年前,Sam有"<<sam<<"元,Sue有"<<sue<<"元"<<endl; 17 cout<<"偷吃猪蹄被逮住了,都被罚了一元"<<endl; 18 sam--,sue--; 19 cout<<"现在,Sam有"<<sam<<"元,Sue有"<<sue<<"元"<<endl; 20 21 system("pause"); 22 }
1 #include<iostream> 2 using namespace std; 3 4 void main() 5 { 6 int a=42; 7 int b=0x42;//16进制 8 int c=042;//8进制 9 cout<<"a="<<a<<endl; 10 cout<<"b="<<b<<endl; 11 cout<<"c="<<c<<endl; 12 13 system("pause"); 14 }
cout默认以10进制输出
1 #include<iostream> 2 using namespace std; 3 4 void main() 5 { 6 int a=42,b=42,c=42; 7 cout<<"a="<<a<<endl;//cout默认以10进制输出 8 cout<<hex;//修改cout显示整数的方式,以16进制输出 9 cout<<"b="<<b<<endl; 10 cout<<oct;//以8进制输出 11 cout<<"c="<<c<<endl; 12 13 system("pause"); 14 }
1 #include<iostream> 2 using namespace std; 3 4 void main() 5 { 6 char ch=‘M‘; 7 int i=ch; 8 cout<<ch<<"的ASCLL码为"<<i<<endl; 9 cout<<"给它+1"<<endl; 10 ch++; 11 i=ch; 12 cout<<ch<<"的ASCLL码为"<<i<<endl; 13 cout<<"用cout.put(ch)输出:"; 14 cout.put(ch);//通过类对象cout来使用函数put() 15 cout.put(‘!‘); 16 17 system("pause"); 18 }
1 #include<iostream> 2 using namespace std; 3 4 void main() 5 { 6 cout<<"\a计划\"灭世\"现在正在启动\n";// /a发出一声振铃 7 cout<<"请输入您的密码:______\b\b\b\b\b\b"; 8 long code; 9 cin>>code; 10 cout<<"\a您输入的密码为"<<code<<"...\n"; 11 cout<<"\a密码已通过证实!计划启动!\n"; 12 13 system("pause"); 14 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 //通常cout会删除结尾的0,比如3.25000显示为3.25,调用cout.setf()将覆盖这种行为 6 cout.setf(ios_base::fixed,ios_base::floatfield); 7 float tub=10.0/3.0; 8 double mint=10.0/3.0; 9 const float million=1.0e6; 10 11 cout<<"tub="<<tub<<",a million tubs="<<million*tub<<endl; 12 cout<<"ten million tubs="<<10*million*tub<<endl; 13 cout<<"mint="<<mint<<",a million mints="<<million*mint<<endl; 14 15 cin.get();//按下enter键结束 16 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 float a=2.34E+22f; 6 float b=a+1.0f; 7 8 cout<<"a="<<a<<endl; 9 cout<<"b-a="<<b-a<<endl; 10 11 cin.get();//按下enter键结束 12 }
原因:2.34E+22是一个小数点左边有23位的数字,+1就是在第23位上+1.但是float类型只能表示数字中的前6位(或7位),因此修改第23位对这个值不会有任何影响。
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 float a,b; 6 cout.setf(ios_base::fixed,ios_base::floatfield); 7 cout<<"enter a number:"; 8 cin>>a; 9 cout<<"enter a another number:"; 10 cin>>b; 11 12 cout<<"a="<<a<<",b="<<b<<endl; 13 cout<<"a+b="<<a+b<<endl; 14 cout<<"a-b="<<a-b<<endl; 15 cout<<"a*b="<<a*b<<endl; 16 cout<<"a/b="<<a/b<<endl; 17 18 system("pause"); 19 }
加法结果应为61.42,输出却是61.41998,这是因为float表示有效位数的能力有限。对于float,C++只保证6位有效位,如果需要更高精度,请使用double和long double。亲测,使用double类型之后,计算结果正确
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 cout.setf(ios_base::fixed,ios_base::floatfield); 6 cout<<"Integer division:9/5="<<9/5<<endl; 7 cout<<"Float division:9.0/5.0="<<9.0/5.0<<endl; 8 cout<<"Mixed division:9.0/5="<<9.0/5<<endl; 9 cout<<"double constants:1e7/9.0="<<1e7/9.0<<endl; 10 cout<<"float constants:1e7f/9.0f="<<1e7f/9.0f<<endl; 11 12 system("pause"); 13 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 cout.setf(ios_base::fixed,ios_base::floatfield); 6 float tree(3);//等同于 float tree=3 7 int guess=3.9832; 8 int debt=7.2e12; 9 cout<<"tree="<<tree<<endl; 10 cout<<"guess="<<guess<<endl; 11 cout<<"debt="<<debt<<endl; 12 13 system("pause"); 14 }
float 6位精度,int舍弃小数,int无法存储7.2e12那么大的数。
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 const int code=66; 6 int x=66; 7 char c1={31325}; 8 char c2={66}; 9 char c3={code}; 10 char c4={x}; 11 x=31325; 12 char c5=x; 13 14 cout<<"c1="<<c1<<endl; 15 cout<<"c2="<<c2<<endl; 16 cout<<"c3="<<c3<<endl; 17 cout<<"c4="<<c4<<endl; 18 cout<<"c5="<<c5<<endl; 19 system("pause"); 20 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 cout<<int(‘A‘)<<endl;//c++格式 6 cout<<(int)‘a‘<<endl;//c格式 7 system("pause"); 8 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 int a,b,c; 6 a=19.99+11.99; 7 b=(int)19.99+(int)11.99;//c格式 8 c=int(19.99)+int(11.99);//c++格式 9 10 cout<<"a="<<a<<endl; 11 cout<<"b="<<b<<endl; 12 cout<<"c="<<c<<endl; 13 14 char ch=‘A‘; 15 cout<<"the code for "<<ch<<" is "<<int(ch)<<endl; 16 cout<<"Yes,the code is "<<static_cast<int>(ch)<<endl;//static_cast<typeName>(value)比传统强制类型转换更严格 17 system("pause"); 18 }
[C++ Primer Plus] 2、处理数据(一)程序清单
标签:小数点 sam max enter键 primer visio ber size pen
原文地址:http://www.cnblogs.com/little-monkey/p/7218776.html