书籍:C++ Primer Plus (第六版)(中文版)
工具:Dev-C++ 5.11
电脑信息:Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 Windows10 专业版
第3章 数据处理
3.1 简单变量
实例3.1 limits.cpp
#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;
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 << "最大值: " << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl;
cout << "最小int值 = " << INT_MIN << endl;
cout << "每字节位数 = " << CHAR_BIT << endl;
return 0;
}
编译运行结果:
int is 4 bytes.
short is 2 bytes.
long is 4 bytes.
long long is 8 bytes.
最大值:
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807
最小int值 = -2147483648
每字节位数 = 8
--------------------------------
进程在0.3248秒后退出,返回值为0
请按任意键继续. . .
实例3.2 exceed.cpp
#include <iostream>
#define ZERO 0
#include <climits>
int main()
{
using namespace std;
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam有 " << sam << " 美元,而Sue有 " << sue;
cout << " 存款." << endl
<< "给每个账户加1美元." << endl << "现在";
sam = sam + 1;
sue = sue + 1;
cout << "Sam有 " << sam << " 美元,而Sue有 " << sue;
cout << " 存款.\n可怜的Sam!" << endl;
sam = ZERO;
sue = ZERO;
cout << "Sam有 " << sam << " 美元,而Sue有 " << sue;
cout << " 存款." << endl;
}
cout<<"Take $1 from each account."<<endl<<"Now ";
sam=sam-1;
sue=sue-1;
cout<<"Sam has "<<sam<<" dollars and sue has "<<sue;
cout<<" dollars deposited."<<endl<<"Lucky Sue!"<<endl;
return 0;
}
编译运行结果:
Sam has 32767 dollars and sue has 32767 dollars deposited.
Add 1$ to each account.
Now Sam has -32768 dollars and sue has 32768 dollars deposited.
Poor Sam!
Sam has 0 dollars and sue has 0 dollars deposited.
Take $1 from each account.
Now Sam has -1 dollars and sue has 65535 dollars deposited.
Lucky Sue!
--------------------------------
Process exited after 0.3318 seconds with return value 0
请按任意键继续. . .
实例3.3?hexoct.cpp
#include <iostream>
int main()
{
using namespace std;
int chest=42;
int waist=0x42;
int inseam=042;
cout<<"Monsieur cuts a striking figure!\n";
cout<<"chest= "<<chest<<" (42 in decimal) \n";
cout<<"waist= "<<waist<<" (0x42 in hexadecimal) \n";
cout<<"inseam= "<<inseam<<" (042 in octal) \n";
return 0;
}
编译运行结果:
Monsieur cuts a striking figure!
chest= 42 (42 in decimal)
waist= 66 (0x42 in hexadecimal)
inseam= 34 (042 in octal)
--------------------------------
Process exited after 0.3296 seconds with return value 0
请按任意键继续. . .
实例3.4?hexoct2.cpp
#include <iostream>
using namespace std;
int main()
{
using namespace std;
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;
cout<<"waist= "<<waist<<" (hexadecimal for 42) "<<endl;
cout<<oct;
cout<<"inseam= "<<inseam<<" (octal for 42) "<<endl;
return 0;
}
编译运行结果:
Monsieur cuts a striking figure!
chest= 42 (decimal for 42)
waist= 2a (hexadecimal for 42)
inseam= 52 (octal for 42)
--------------------------------
Process exited after 0.3168 seconds with return value 0
请按任意键继续. . .
实例3.5?chartype.cpp
#include <iostream>
int main()
{
using namespace std;
char ch;
cout<<"input a symbol: "<<endl;
cin>>ch;
cout<<"Hola! ";
cout<<"appreciate the "<<ch<<" symbol. "<<endl;
return 0;
}
编译运行效果:
input a symbol:
w
Hola! appreciate the w symbol.
--------------------------------
进程结束后,耗时 6.362 秒,返回值为 0
请按任意键继续. . .
实例3.6?morechar.cpp
#include <iostream>
int main()
{
using namespace std;
char ch='M';
int i=ch;
cout<<"the ASCII code for "<<ch<<" is "<<i<<endl;
cout<<"increment the character code by one: "<<endl;
ch=ch+1;
i=ch;
cout<<"the ASCII code for "<<ch<<" is "<<i<<endl;
cout<<"show char ch using cout.put(ch): ";
cout.put(ch);
cout.put('!');
return 0;
}
编译运行效果:
the ASCII code for M is 77
increment the character code by one:
the ASCII code for N is 78
show char ch using cout.put(ch): N!
--------------------------------
进程结束后,耗时 0.3277 秒,返回值为 0
请按任意键继续. . .
实例3.7?bondini.cpp
#include <iostream>
int main()
{
using namespace std;
cout<<"\a启动 \"HyperHype\" 操作!\n ";
cout<<"输入你的特工编号:________\b\b\b\b\b\b\b\b ";
long code;
cin>>code;
cout<<"\a你输入了 "<<code<<" ...\n";
cout<<"\a特工编号验证成功!继续执行Z3计划!\n";
return 0;
}
编译运行效果:
启动 "HyperHype" 操作!
输入你的特工编号: 123456_
你输入了 123456 ...
特工编号验证成功!继续执行Z3计划!
--------------------------------
进程结束后,耗时 8.318 秒,返回值为 0
请按任意键继续. . .


雷达卡


京公网安备 11010802022788号







