# Simple C++ Maths

• C++中的操作
• 例子
• 运算符的优先级
• 例子

C++中的数学非常简单。请记住，C++数学运算遵循高中数学的运算法则。例如，乘法和除法的优先级高于加法和减法，可以用括号更改这些操作的求值顺序。

C++中数学运算符操作如下：

### 例子

``````#include <iostream>
using namespace std;

int main() {
int myInt = 100;

myInt = myInt / 10; //myInt 现在为 10
cout <<"Value of myInt after division by 10 is: " <<myInt << endl;
myInt = myInt * 10; //myInt 又变为 100
cout <<"Value of myInt after multiplication by 10 is: " <<myInt << endl;
myInt = myInt + 50; //myInt 增加为 150
cout <<"Value of myInt after addition of 50 is: " <<myInt << endl;
myInt = myInt - 50; //myInt 变回一开始的 100
cout <<"Value of myInt after subtraction of 50 is: " <<myInt << endl;

myInt = myInt + 100 * 2; // myInt 现在是 300
cout << "Value of myInt after adding 100 and multiplying by 2 is: "<<myInt<<endl;
myInt = (myInt + 100) * 2; // myInt 现在是 800
cout << "Value of myInt after doing the same operations using paranthesis is: "<<myInt<<endl;

myInt -= 10; // myInt 现在 790 b
cout << "Value of myInt after -=10 operation is: "<<myInt<<endl;
myInt = myInt % 100; // myInt 现在是 90
cout << "Value of myInt after taking modulus with 100 is: "<<myInt<<endl;

cout << "Value of myInt after all operations is: " <<myInt << endl;

return 0;
}``````

### 举例

20 + 30 * 2 / 10 – 3

``````#include <iostream>
using namespace std;

int main() {
// 20 + 30 * 2 / 10 - 3
cout << "Answer is " <<  20 + 30 * 2 / 10 - 3 << endl;
return 0;
}``````