0 / 8 页已完成

计算和组合Calculations & Combining Variables

4.1 四则运算4.1 Basic Arithmetic

程序最擅长的事情之一就是算数。C++ 支持你熟悉的四种运算:One of the things programs do best is math. C++ supports the four operations you already know:

运算Operation 符号Symbol 例子Example 结果Result
Add + 3 + 4 7
Subtract - 10 - 3 7
Multiply * 5 * 6 30
Divide / 10 / 3 3(不是 3.33!)3 (not 3.33!)

4.2 整数除法陷阱4.2 The Integer Division Trap

注意上面表格里除法的结果:10 / 3 等于 3,不是 3.33。Notice the division result above: 10 / 3 equals 3, not 3.33.

这是因为 103 都是整数(int),整数除整数,结果还是整数,小数部分直接扔掉。This is because 10 and 3 are both integers (int). When you divide an integer by an integer, the result is also an integer — the decimal part is simply thrown away.

C++
int a = 7 / 2;       // a = 3(不是 3.5!小数部分被扔掉了)
double b = 7.0 / 2;  // b = 3.5(只要有一个是小数,结果就是小数)
避坑秘诀Pro Tip

如果你需要精确的除法结果,至少把一个数写成小数形式(加 .0)。比如写 7.0 / 2 而不是 7 / 2If you need an accurate division result, make at least one number a decimal (add .0). For example, write 7.0 / 2 instead of 7 / 2.

检查点:预测输出Checkpoint: Predict the Output
这段代码中 result 的值是多少?What is the value of result in this code?
C++
int result = 9 / 2;

4.3 取余运算4.3 Modulo (Remainder)

% 叫取余(也叫模运算),它算的是除法的余数% is called modulo (also known as the remainder operator). It gives you the remainder of a division.

C++
int r = 7 % 3;    // r = 1(7 除以 3 等于 2 余 1)
int s = 10 % 5;   // s = 0(10 除以 5 刚好整除,余数为 0)
int t = 8 % 2;    // t = 0(8 是偶数,除以 2 余 0)
实用技巧:判断奇偶Useful Trick: Check Odd or Even

一个数 x % 2 的结果如果是 0,说明 x 是偶数;如果是 1,说明 x 是奇数。If x % 2 equals 0, then x is even; if it equals 1, then x is odd.

4.4 组合变量做计算4.4 Combining Variables in Calculations

变量不只是用来存数字的,还可以用来做计算,把结果存到新变量里:Variables aren't just for storing numbers — you can use them in calculations and store the results in new variables:

C++
int a = 10;
int b = 20;
int total = a + b;    // total = 30
int diff = b - a;     // diff = 10

4.5 机器人实例:轮子走了多远?4.5 Robot Example: How Far Did the Wheel Travel?

来一个真实的例子。你的机器人轮子转了几圈,你想算它走了多远。Here's a real example. Your robot's wheel has spun a few rotations and you want to calculate how far it traveled.

公式很简单:距离 = 圈数 x 周长,而周长 = 2 x 3.14159 x 半径。The formula is simple: distance = rotations x circumference, where circumference = 2 x 3.14159 x radius.

C++ (VEXcode)
int main() {
    double radius = 5.0;       // 轮子半径 5cm
    double pi = 3.14159;
    double circumference = 2 * pi * radius;  // 周长
    double rotations = 3.0;    // 转了 3 圈
    double distance = rotations * circumference;

    Brain.Screen.print("Distance: %.1f cm", distance);
    return 0;
}
C++ (VS Code)
int main() {
    double radius = 5.0;       // 轮子半径 5cm
    double pi = 3.14159;
    double circumference = 2 * pi * radius;  // 周长
    double rotations = 3.0;    // 转了 3 圈
    double distance = rotations * circumference;

    Brain.Screen.print("Distance: %.1f cm", distance);
    return 0;
}
C++ (PROS)
void initialize() {
    pros::lcd::initialize();

    double radius = 5.0;       // 轮子半径 5cm
    double pi = 3.14159;
    double circumference = 2 * pi * radius;  // 周长
    double rotations = 3.0;    // 转了 3 圈
    double distance = rotations * circumference;

    pros::lcd::print(1, "Distance: %.1f cm", distance);
}

运行结果:屏幕显示 Distance: 94.2 cmResult: The screen shows Distance: 94.2 cm

这就是编程的魅力 -- 你只要告诉程序公式和数据,它帮你算出结果。以后轮子换了大小,只需要改 radius 的值就行。This is the power of programming — you just give the program a formula and data, and it calculates the result for you. If you change the wheel size later, you only need to change the radius value.

检查点Checkpoint
13 % 5 的结果是多少?What is the result of 13 % 5?

4.6 写代码练习4.6 Coding Exercise

挑战Challenge

写代码计算:半径 5cm 的轮子转了 3 圈,走了多少 cm?Write code to calculate: How many cm does a wheel with radius 5cm travel after 3 rotations?

提示:Hints:

正确答案应该约等于 94.2 cmThe correct answer should be approximately 94.2 cm.