C++ primer plus 第三章 习题
2017-06-07 07:51
302 查看
limits.cpp
#include <iostream>
#include <climits>//使用limits中各种定义.
using namespace std;
int main(void){
int n_int=INT_MAX;
short n_short=SHRT_MAX;
long n_long=LONG_MAX;
long long n_llong=LLONG_MAX;
//sizeof 是一种操作符,可以对类型使用 sizeof(),对变量使用 sizeof n_int(对变量使用也可以加括号,)
cout << "int is "<<sizeof(int)<<" bytes."<<endl
<<"short is "<<sizeof(short)<<" bytes."<<endl
<<"long is "<< sizeof n_long<<" bytes."<< endl
<<"long long is "<< sizeof n_llong<<" bytes."<<endl<<endl
<<"max value:"<<endl
<<"int : "<<n_int<<endl
<<"short : "<<n_short<<endl
<<"long : "<<n_long<<endl
<<"long long : "<<n_llong
<<endl
<<endl
<< "Minimum int value = "<<INT_MIN<<endl
<<"Bits per bytes = "<<CHAR_BIT<<endl;
return 0;
}
define 和include 一样是预处理编译指令,同样define语句后不需要加分号
例如在climits头文件中有
#define INT_MAX 32767
作用是在编译前的预处理过程中将全文搜索匹配的INT_MAX替换为32767,当然嵌入的单词不会替换,
//exceed.cpp
#include <iostream>
#include <climits>
#define ZERO 0
using namespace std;
int main(void){
short sam=SHRT_MAX;
unsigned short sue=sam;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nAdd $1 to each account \nNow ";
sam+=1;
sue+=1;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nPoor sam!\n";
sam=ZERO;
sue=ZERO;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nTake $1 from eache account.\nNow";
sam-=1;
sue-=1;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nLucy sue!\n";
return 0;
}
//hexoct.cpp
#include <iostream>
using namespace std;
int main(void){
int n=42;
int n1=0x42;
int n2=042;
cout<<"42 in decimal is "<<n<<endl<<"42 in hex is "<<n1<<endl
<<"42 in octal is "<<n2<<endl;
return 0;
}
//hexoct2.cpp
//display values in hex and octal;
#include <iostream>
using namespace std;
int main(void){
int n=42;
cout<< "42 in decimal is "
<<n<<endl
<<hex //manipulator for changing number base
<<"42 in hex is "
<<n<<endl
<<oct
<<"42 in octal is "
<<n<<endl;
return 0;
}
//moreCharType.cpp
//the char type and int type contrasted
#include <iostream>
using namespace std;
int main(void){
char ch='M';
int i=ch;
cout<<"the ascii code of char '"<<ch<<"' is "<< i<<endl;
ch=ch+1;
i++;
cout<<"the ascii code of char '"<<ch<<"' is "<< i<<endl;
//using cout.put()
cout<<"Display ch using cout.put()"<<endl;
cout.put(ch);
//display a char constant
cout.put('!');
cout <<endl<<"done"<<endl;
return 0;
}
//bondini.cpp
//using escape sequences
#include <iostream>
using namespace std;
int main(void){
cout<<"\aOperation \"HyperHyper\" is now activated!:\n"
<<"Enter your agent code:______\b\b\b\b\b\b";
long code;
cin>>code;
cout<<"\aYou entered "<<code<<" ...\n"
<<"\aCode verified!Proceed with Plan Z3\n";
return 0;
}
wchar_t,char16_t,char32_t的识别(L,u,U)
//wchar_t 类型
wchar_t ch=L'P';
wcout<<L"tall"<<endl;
//char16_t 与char32_t
char16_t ch1=u'q';
char32_t ch2=U'\U0000222B';
#include <iostream>
#include <climits>//使用limits中各种定义.
using namespace std;
int main(void){
int n_int=INT_MAX;
short n_short=SHRT_MAX;
long n_long=LONG_MAX;
long long n_llong=LLONG_MAX;
//sizeof 是一种操作符,可以对类型使用 sizeof(),对变量使用 sizeof n_int(对变量使用也可以加括号,)
cout << "int is "<<sizeof(int)<<" bytes."<<endl
<<"short is "<<sizeof(short)<<" bytes."<<endl
<<"long is "<< sizeof n_long<<" bytes."<< endl
<<"long long is "<< sizeof n_llong<<" bytes."<<endl<<endl
<<"max value:"<<endl
<<"int : "<<n_int<<endl
<<"short : "<<n_short<<endl
<<"long : "<<n_long<<endl
<<"long long : "<<n_llong
<<endl
<<endl
<< "Minimum int value = "<<INT_MIN<<endl
<<"Bits per bytes = "<<CHAR_BIT<<endl;
return 0;
}
define 和include 一样是预处理编译指令,同样define语句后不需要加分号
例如在climits头文件中有
#define INT_MAX 32767
作用是在编译前的预处理过程中将全文搜索匹配的INT_MAX替换为32767,当然嵌入的单词不会替换,
//exceed.cpp
#include <iostream>
#include <climits>
#define ZERO 0
using namespace std;
int main(void){
short sam=SHRT_MAX;
unsigned short sue=sam;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nAdd $1 to each account \nNow ";
sam+=1;
sue+=1;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nPoor sam!\n";
sam=ZERO;
sue=ZERO;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nTake $1 from eache account.\nNow";
sam-=1;
sue-=1;
cout << "sam has "<<sam<<" dollars and sue has "<<sue<<" dollars\nLucy sue!\n";
return 0;
}
//hexoct.cpp
#include <iostream>
using namespace std;
int main(void){
int n=42;
int n1=0x42;
int n2=042;
cout<<"42 in decimal is "<<n<<endl<<"42 in hex is "<<n1<<endl
<<"42 in octal is "<<n2<<endl;
return 0;
}
//hexoct2.cpp
//display values in hex and octal;
#include <iostream>
using namespace std;
int main(void){
int n=42;
cout<< "42 in decimal is "
<<n<<endl
<<hex //manipulator for changing number base
<<"42 in hex is "
<<n<<endl
<<oct
<<"42 in octal is "
<<n<<endl;
return 0;
}
//moreCharType.cpp
//the char type and int type contrasted
#include <iostream>
using namespace std;
int main(void){
char ch='M';
int i=ch;
cout<<"the ascii code of char '"<<ch<<"' is "<< i<<endl;
ch=ch+1;
i++;
cout<<"the ascii code of char '"<<ch<<"' is "<< i<<endl;
//using cout.put()
cout<<"Display ch using cout.put()"<<endl;
cout.put(ch);
//display a char constant
cout.put('!');
cout <<endl<<"done"<<endl;
return 0;
}
//bondini.cpp
//using escape sequences
#include <iostream>
using namespace std;
int main(void){
cout<<"\aOperation \"HyperHyper\" is now activated!:\n"
<<"Enter your agent code:______\b\b\b\b\b\b";
long code;
cin>>code;
cout<<"\aYou entered "<<code<<" ...\n"
<<"\aCode verified!Proceed with Plan Z3\n";
return 0;
}
wchar_t,char16_t,char32_t的识别(L,u,U)
//wchar_t 类型
wchar_t ch=L'P';
wcout<<L"tall"<<endl;
//char16_t 与char32_t
char16_t ch1=u'q';
char32_t ch2=U'\U0000222B';
相关文章推荐
- C++Primer Plus笔记——第三章 处理数据及课后习题答案
- c++ primer plus第三章习题答案
- c++ Primer Plus(第六版)第三章习题,写代码之路
- c++ PrimerPlus 第六版中文版编程习题答案
- c++ primer plus第五章习题答案
- C++ primer plus(第六版)学习笔记、习题答案(1)
- C++ primer plus(第六版)学习笔记、习题答案(2)
- C++primer plus 第十四章习题答案
- C++primer plus第三章笔记
- C++ Primer Plus习题笔记(1)
- C++ Primer Plus第三章课后编程答案
- C++ primer plus 第十章课后习题,原创答案。
- c++ primer plus 第六版第三章编程练习答案
- (2011.07.31)自学《C++ Primer Plus》时做过的书上的习题!
- C++Primer Plus 第三章编程练习个人答案
- C++ Primer Plus学习:第三章 处理数据(2)
- C++primer plus 第三 四章的习题(自己写的)
- C++primer plus 第五章习题!
- c++ primer第五版(中文)习题答案 第三章第二节-标准库类型string
- c++ Primer Plus(第六版)第五章习题,写代码之路