C++ primer plus Chapter 3 Exercises


limits.cpp
#include <iostream>
#include <climits>//Use various definitions in 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 is an operator, you can use sizeof() for types and sizeof n_int for variables (you can also use parentheses for variables,)
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 is the same as include and is a preprocessing compilation directive, and there is no need to add a semicolon after the define statement.

For example in the climits header file there is

#define INT_MAX 32767

The function is to replace the INT_MAX matched by the full-text search with 32767 during the preprocessing process before compilation. Of course, the embedded words will not be replaced.

//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;
}

Identification of wchar_t, char16_t, char32_t (L, u, U)

//wchar_t type

wchar_t ch=L'P';

wcout<<L"tall"<<endl;

//char16_t and char32_t

char16_t ch1=u'q';
char32_t ch2=U'\U0000222B';



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325611442&siteId=291194637