Logo
Section 1 / 5

简单的 C++ 数学运算

了解 C++ 中使用的算术运算符。

检查点

简单的 C++ 数学运算

了解 C++ 中使用的算术运算符。

符号算术运算
+加法
-减法
/除法
*乘法
%取余(或模运算)
1#include <iostream> 2using namespace std; 3 4int main() { 5 cout << 15 + 5 << endl; // 加法 6 cout << 15 - 5 << endl; // 减法 7 cout << 15 * 5 << endl; // 乘法 8 cout << 15 / 5 << endl; // 除法 9 cout << 15 % 5 << endl; // 取余 10 return 0; 11}
1#include <iostream> 2using namespace std; 3 4int main() { 5 int num1, num2, res; 6 num1 = 15; 7 num2 = 5; 8 res = 0; 9 res = num1 + num2; // 加法 10 cout << res << endl; 11 res = num1 - num2; // 减法 12 cout << res << endl; 13 res = num1 * num2; // 乘法 14 cout << res << endl; 15 res = num1 / num2; // 除法 16 cout << res << endl; 17 res = num1 % num2; // 取余 18 cout << res << endl; 19 return 0; 20}
1#include <iostream> 2using namespace std; 3 4int main() { 5 cout << 15 / 4 << endl; 6 cout << 15.0 / 4.0 << endl; 7 cout << 15.0 / 4 << endl; 8 cout << 15 / 4.0 << endl; 9 return 0; 10}
1#include <iostream> 2using namespace std; 3 4int main() { 5 // 抵消误差(Cancellation Error) 6 cout << 1100.0 + 0.00000000000008 << endl; 7 // 算术溢出(Arithmetic Overflow) 8 // cout << 9999999999999999999999 + 8989898989898989898 << endl; 9 return 0; 10}

代码示例

1x + y - z
1.

What is the result of the following operation?

112 / 3 * 2
0/500
1#include <iostream> 2using namespace std; 3 4int main() { 5 // 20 + 30 * 2 / 10 - 3 6 cout << "答案是 " << 20 + 30 * 2 / 10 - 3 << endl; 7 return 0; 8}

代码示例

1a = a + b;

代码示例

1a += b;

表格

符号运算
+=加法赋值
-=减法赋值
*=乘法赋值
/=除法赋值
%=取余赋值
1#include <iostream> 2using namespace std; 3 4int main() { 5 int a, b; 6 a = 9; 7 b = 3; 8 cout << "a:" << a <<endl; // a = 9 9 cout << "b:" << b << endl; // b = 3 10 a += b; 11 cout << "a += b" << endl; 12 cout << "a:" << a <<endl; // a = 12 13 cout << "b:" << b << endl; // b = 3 14 a -= b; 15 cout << "a -= b" << endl; 16 cout << "a:" << a <<endl; // a = 9 17 cout << "b:" << b << endl; // b = 3 18 a *= b; 19 cout << "a *= b" << endl; 20 cout << "a:" << a <<endl; // a = 27 21 cout << "b:" << b << endl; // b = 3 22 a /= b; 23 cout << "a /= b" << endl; 24 cout << "a:" << a <<endl; // a = 9 25 cout << "b:" << b << endl; // b = 3 26 a %= b; 27 cout << "a %= b" << endl; 28 cout << "a:" << a <<endl; // a = 0 29 cout << "b:" << b << endl; // b = 3 30 return 0; 31}
1#include <iostream> 2using namespace std; 3 4int main() { 5 int val = 0; 6 7 // 后置递增 8 cout << val << endl; 9 cout << val++ << endl; 10 cout << val++ << endl; 11 12 // 后置递减 13 cout << val << endl; 14 cout << val-- << endl; 15 cout << val-- << endl; 16 17 // 最终值 18 cout << val << endl; 19 20 return 0; 21}
1#include <iostream> 2using namespace std; 3 4int main() { 5 int val = 0; 6 7 // 前置递减 8 cout << val << endl; 9 cout << --val << endl; 10 cout << val << endl; 11 12 // 前置递增 13 cout << val << endl; 14 cout << ++val << endl; 15 cout << val << endl; 16 17 return 0; 18}
1.

What is the output of the following code?

1int x = 5; 2cout << x++ << " "; 3cout << ++x << endl;
0/500

Quiz

单选题

以下表达式的输出结果是什么?

12 + 15 * ( 7 + (2 * 0) )
1 / 2

指针与数组

类型转换