C++第三章例题

#include <iostream>
#include <climits>
#include <cstdlib>
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\n"
<< "short is " << sizeof n_short << " bytes\n"
<< "long is " << sizeof n_long << " bytes\n"
<< "long long is " << sizeof n_llong << " bytes\n" << endl;
cout << "maxium values: " << endl;
cout << "int: " << n_int << endl
<< "short:" << n_short << endl
<< "long: " << n_long << endl
<< "long long: " << n_llong << endl;
cout << "Minimum int value = " << INT_MIN << endl
<< "bits per byte = " << CHAR_BIT << endl;
cin.get();
return 0;

}


#include <iostream>
#define zero 0
#include <climits>
int main()
{
using namespace std;
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl;
cout << "Add $1 to each account." << endl << "Now";
sam++;
sue++;
cout << "Sam has " << sam << " dollars and Sue has " << sue
<< " dollars deposited.\nPoor Sam!" << endl;
sam = zero;
sue = zero;
cout << "Sam has " << sam << "dollars and Sue has " << sue
<< " dollars deposited." << endl;
cout << "Take $1 from each account." << endl << "Now";
sam--;
sue--;
cout << "Sam has " << sam << " dollars and Sue has " << sue
<< " dollars deposited." << endl << "Lucky SUe!\n";
cin.get();
return 0;

}


#include <iostream>
int main()
{
using namespace std;
int a = 42, b = 0x42, c = 042;
cout << "Monsieur cuts a striking figure!\n"
<< "a = " << a << " (42 in decimal)\n"
<< "b = " << b << " (0x42 in hex)\n"
<< "c = " << c << " (042 in octal)\n";
cin.get();
return 0;

}


#include <iostream>
int main()
{
using namespace std;
int a = 42, b = 42, c = 42;
cout << "Monsieur cuts a striking figure!\n";
cout << "a = " << a << " (decimal for 42)\n";
cout << hex;
cout << "b = " << b << " (decimal for 42)\n";
cout << oct;
cout << "c = " << c << " (decimal for 42)\n";
cin.get();
return 0;

}


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
char ch;
cout << "Enter a character:\n";
cin >> ch;
cout << "Think you for the " << ch << " character\n";
system("pause");
return 0;

}


#include <iostream>
int main()
{
using namespace std;
char ch='M';
int x=int(ch);
cout << "The ASCII code for " << ch << " is " << int(ch) << endl;
cout << "Add one to the character code\n";
x++;
cout << "The ASCII code for " << char(x) << " is " << x << endl;
cout << "Displaying char ch using cout.put(ch): ";
cout.put(ch);
cout << endl << "Done\n";
cin.get();
return 0;

}


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
cout << "Operation \"HyperHype\" is now activated!\n"
<< "Enter your agent code:_________\b\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered " << code << "...\n";
cout << "\aCode verified! Proceed with Plan Z3!\n";
system("pause");
return 0;

}


#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float a = 10.0 / 3;
double b = 10.0 / 3;
const float million = 1.0e+6;
cout << "a = " << a << " , a million a = " << million * a;
cout << " ,\nand ten million a = " << 10 * million*a << endl;
cout << "b = " << b << " and a million b = " << million * b;
cout << " ,\nand ten million b = " << 10 * million*b << endl;
cin.get();
return 0;

}


#include <iostream>
int main()
{
using namespace std;
float a = 2.34e+22f, b = a + 1.0f;
cout << "a = " << a << endl;
cout << "b-a = " << b-a << endl;
cin.get();
return 0;

}


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
float a, b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter anthor number: ";
cin >> b;
cout << "a = " << a << "   b = " << b << endl;
cout << "a + b = " << a + b << endl
<< "a - b = " << a - b << endl
<< "a * b = " << a * b << endl
<< "a / b = " << a / b << endl;
system("pause");
return 0;

}


#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Integer division: 9/5 = " << 9 / 5 << endl;
cout << "Floatingf-point division: 9.0/5.0 = " << 9.0 / 5.0 << endl;
cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
cout << "double constants: 1e7/9.0 = " << 1e7 / 9.0 << endl;
cout << "float constants: 1e7f/9.0f = " << 1e7f / 9.0f << endl;
cin.get();
return 0;

}


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int a, b = 14;
cout << "Enter your weight in pounds: ";
cin >> a;
int c = a / b;
int d = a % b;
cout << a << " pounds are " << c << " stone, " << d << " pound(s)." << endl;
system("pause");
return 0;

}


#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float tree = 3;
int guess(3.9832);
int debt = 7.2e12;
cout << "tree = " << tree << endl
<< "guess = " << guess << endl
<< "debt = " << debt << endl;
cin.get();
return 0;

}


#include <iostream>
int main()
{
using namespace std;
int a, b, c;
a = 19.99 + 11.99;
b = (int)19.99 + (int)11.99;
c = int(19.99) + int(11.99);
cout << "a = " << a << "  b = " << b << "  c = " << c << endl;
char ch = 'Z';
cout << "The code for " << ch << " is " << int(ch) << endl;
cout << "Yes, the code is ";
cout << static_cast<int>(ch) << endl;
cin.get();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值