基本数据类型的运算

谁践踏了优雅 2022-08-25 08:49 245阅读 0赞
  1. #include <iostream >
  2. #include <cstdio>
  3. using namespace std;
  4. void test_int()
  5. {
  6. printf("\ntest int:\n");
  7. int a = 100000;
  8. cout << "100000 * 100000 = " << a * a << endl;
  9. cout << " 14 / 5 = " << 14 / 5 << endl;
  10. cout << " 14 % 5 = " << 14 % 5 << endl;
  11. cout << " -14 / 5 = " << -14 / 5 << endl;
  12. cout << " -14 % 5 = " << -14 % 5 << endl;
  13. cout << " 14 / -5 = " << 14 / -5 << endl;
  14. cout << " 14 % -5 = " << 14 % -5 << endl;
  15. cout << " -14 / -5 = " << -14 / -5 << endl;
  16. cout << " -14 % -5 = " << -14 % -5 << endl;
  17. }
  18. void test_double()
  19. {
  20. printf("\ntest double:\n");
  21. double c, d, z;
  22. c = 1.0; d = 0.0;
  23. cout << " 1.0 / 0.0 = " << c/d << endl;
  24. c = -1.0; d = 0.0;
  25. cout << " -1.0 / 0.0 = " << c/d << endl;
  26. c = 0.0; d = 0.0;
  27. cout << " 0.0 / 0.0 = " << c/d << endl;
  28. z = 1.0;
  29. cout << " z = 1.0; z = " << z << endl;
  30. z /= 12345678912345.0;
  31. cout << " z /= 12345678912345.0; z = " << z << endl;
  32. z += 1.0;
  33. cout << " z += 1.0; z = " << z << endl;
  34. z -= 1.0;
  35. cout << " z -= 1.0; z = " << z << endl;
  36. z *= 12345678912345.0;
  37. cout << " z *= 12345678912345.0; z == " << z << endl;
  38. }
  39. void test_bool()
  40. {
  41. printf("\ntest bool:\n");
  42. cout << "(1 <= 2) = " << (1 <= 2) << endl;
  43. cout << "(3 == 4) = " << (3 == 4) << endl;
  44. cout << "(5 != 6 && -7 <= 10) = " << (5 != 6 && -7 <= 10) << endl;
  45. cout << "(1234 & 5678) = " << (1234 & 5678) << endl;
  46. cout << "(1 << 16) = " << (1 << 16) << endl;
  47. }
  48. void test_order()
  49. {
  50. printf("\ntest order:\n");
  51. int a = 2, b = 3;
  52. cout << "(a++) * (a+2*b) * (b++) = " << (a++) * (a+2*b) * (b++) << endl;
  53. }
  54. void test_char()
  55. {
  56. printf("\ntest char:\n");
  57. cout << "'A ' + 2 = " << 'A' + 2 << endl;
  58. cout << "(char)('A ' + 2) = " << (char)('A' + 2) << endl;
  59. cout << "'z' - 'f' = " << 'z' - 'f' << endl;
  60. }
  61. int main()
  62. {
  63. test_int();
  64. test_double();
  65. test_bool();
  66. test_char();
  67. test_order();
  68. return 0;
  69. }
  70. /*
  71. test int:
  72. 100000 * 100000 = 1410065408
  73. 14 / 5 = 2
  74. 14 % 5 = 4
  75. -14 / 5 = -2
  76. -14 % 5 = -4
  77. 14 / -5 = -2
  78. 14 % -5 = 4
  79. -14 / -5 = 2
  80. -14 % -5 = -4
  81. test double:
  82. 1.0 / 0.0 = inf
  83. -1.0 / 0.0 = -inf
  84. 0.0 / 0.0 = nan
  85. z = 1.0; z = 1
  86. z /= 12345678912345.0; z = 8.1e-014
  87. z += 1.0; z = 1
  88. z -= 1.0; z = 8.10463e-014
  89. z *= 12345678912345.0; z == 1.00057
  90. test bool:
  91. (1 <= 2) = 1
  92. (3 == 4) = 0
  93. (5 != 6 && -7 <= 10) = 1
  94. (1234 & 5678) = 1026
  95. (1 << 16) = 65536
  96. test char:
  97. 'A ' + 2 = 67
  98. (char)('A ' + 2) = C
  99. 'z' - 'f' = 20
  100. test order:
  101. (a++) * (a+2*b) * (b++) = 48
  102. */

发表评论

表情:
评论列表 (有 0 条评论,245人围观)

还没有评论,来说两句吧...

相关阅读