C++ primer plus 第三章 习题


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';



猜你喜欢

转载自blog.csdn.net/qq467215628/article/details/72895896