您的位置:首页 > 其它

基本数据类型的运算

2014-02-25 22:52 337 查看
#include <iostream >
#include <cstdio>
using namespace std;
void test_int()
{
printf("\ntest int:\n");
int a = 100000;
cout << "100000 * 100000 = " << a * a << endl;
cout << " 14 / 5 = " << 14 / 5 << endl;
cout << " 14 % 5 = " << 14 % 5 << endl;
cout << " -14 / 5 = " << -14 / 5 << endl;
cout << " -14 % 5 = " << -14 % 5 << endl;
cout << " 14 / -5 = " << 14 / -5 << endl;
cout << " 14 % -5 = " << 14 % -5 << endl;
cout << " -14 / -5 = " << -14 / -5 << endl;
cout << " -14 % -5 = " << -14 % -5 << endl;
}
void test_double()
{
printf("\ntest double:\n");
double c, d, z;
c = 1.0; d = 0.0;
cout << " 1.0 / 0.0 = " << c/d << endl;
c = -1.0; d = 0.0;
cout << " -1.0 / 0.0 = " << c/d << endl;
c = 0.0; d = 0.0;
cout << " 0.0 / 0.0 = " << c/d << endl;
z = 1.0;
cout << " z = 1.0; z = " << z << endl;
z /= 12345678912345.0;

cout << " z /= 12345678912345.0; z = " << z << endl;
z += 1.0;
cout << " z += 1.0; z = " << z << endl;
z -= 1.0;
cout << " z -= 1.0; z = " << z << endl;
z *= 12345678912345.0;
cout << " z *= 12345678912345.0; z == " << z << endl;
}
void test_bool()
{
printf("\ntest bool:\n");
cout << "(1 <= 2) = " << (1 <= 2) << endl;
cout << "(3 == 4) = " << (3 == 4) << endl;
cout << "(5 != 6 && -7 <= 10) = " << (5 != 6 && -7 <= 10) << endl;
cout << "(1234 & 5678) = " << (1234 & 5678) << endl;
cout << "(1 << 16) = " << (1 << 16) << endl;
}
void test_order()
{
printf("\ntest order:\n");
int a = 2, b = 3;
cout << "(a++) * (a+2*b) * (b++) = " << (a++) * (a+2*b) * (b++) << endl;
}
void test_char()
{
printf("\ntest char:\n");
cout << "'A ' + 2 = " << 'A' + 2 << endl;
cout << "(char)('A ' + 2) = " << (char)('A' + 2) << endl;
cout << "'z' - 'f' = " << 'z' - 'f' << endl;
}
int main()
{
test_int();
test_double();
test_bool();
test_char();
test_order();

return 0;
}

/*

test int:
100000 * 100000 = 1410065408
14 / 5 = 2
14 % 5 = 4
-14 / 5 = -2
-14 % 5 = -4
14 / -5 = -2
14 % -5 = 4
-14 / -5 = 2
-14 % -5 = -4

test double:
1.0 / 0.0 = inf
-1.0 / 0.0 = -inf
0.0 / 0.0 = nan
z = 1.0; z = 1
z /= 12345678912345.0; z = 8.1e-014
z += 1.0; z = 1
z -= 1.0; z = 8.10463e-014
z *= 12345678912345.0; z == 1.00057

test bool:
(1 <= 2) = 1
(3 == 4) = 0
(5 != 6 && -7 <= 10) = 1
(1234 & 5678) = 1026
(1 << 16) = 65536

test char:
'A ' + 2 = 67
(char)('A ' + 2) = C
'z' - 'f' = 20

test order:
(a++) * (a+2*b) * (b++) = 48

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: