源码来自《C++ Primer Plus》,有微小改动,运行于64位win10
#include <iostream>
#include <climits>
#define ZERO 0
using std::cout;
using std::cin;
using std::endl;
int main() {
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl
<< "Add $1 dollar to each account." << endl << "Now ";
sam = sam + 1;
sue = sue + 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nPoor Sam!" << endl;
sam = ZERO;
sue = ZERO;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl;
cout << "Take $1 dollar from each account." << endl << "Now ";
sam = sam - 1;
sue = sue - 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue ;
cout << " dollars deposited." << endl;
cout << "Lucky Sue!";
return 0;
}
Output:
Sam has 32767 dollars and Sue has 32767 dollars deposited.
Add $1 dollar 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 dollar from each account.
Now Sam has -1 dollars and Sue has 65535 dollars deposited.
Lucky Sue!