算术运算符 #
一、算术运算符概述 #
C#提供了以下算术运算符:
| 运算符 | 名称 | 示例 |
|---|---|---|
| + | 加法 | a + b |
| - | 减法 | a - b |
| * | 乘法 | a * b |
| / | 除法 | a / b |
| % | 取余 | a % b |
| ++ | 自增 | a++ 或 ++a |
| – | 自减 | a-- 或 --a |
| + | 正号 | +a |
| - | 负号 | -a |
二、基本算术运算 #
2.1 加法运算 #
csharp
int a = 10 + 5;
int b = a + 20;
double c = 3.14 + 2.86;
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2;
2.2 减法运算 #
csharp
int a = 20 - 10;
int b = a - 5;
double c = 5.5 - 2.3;
2.3 乘法运算 #
csharp
int a = 5 * 4;
int b = a * 2;
double c = 3.5 * 2.0;
2.4 除法运算 #
csharp
int a = 20 / 4;
int b = 7 / 2;
double c = 7.0 / 2;
double d = 7 / 2.0;
整数除法注意:
csharp
int result1 = 7 / 2;
double result2 = 7.0 / 2;
double result3 = (double)7 / 2;
2.5 取余运算 #
csharp
int a = 10 % 3;
int b = 17 % 5;
int c = -10 % 3;
bool isEven = number % 2 == 0;
应用场景:
csharp
int minutes = 135;
int hours = minutes / 60;
int remainingMinutes = minutes % 60;
int lastDigit = 12345 % 10;
int page = 10;
int pageSize = 20;
int index = (page - 1) % pageSize;
三、自增自减运算符 #
3.1 前缀与后缀 #
csharp
int a = 5;
int b = ++a;
int c = 5;
int d = c++;
区别:
| 形式 | 说明 | 示例 |
|---|---|---|
| ++a | 先加1,再使用 | a=5; b=++a; → a=6, b=6 |
| a++ | 先使用,再加1 | a=5; b=a++; → a=6, b=5 |
| –a | 先减1,再使用 | a=5; b=–a; → a=4, b=4 |
| a– | 先使用,再减1 | a=5; b=a–; → a=4, b=5 |
3.2 详细示例 #
csharp
int x = 10;
Console.WriteLine($"x = {x}");
Console.WriteLine($"++x = {++x}");
Console.WriteLine($"x = {x}");
int y = 10;
Console.WriteLine($"y = {y}");
Console.WriteLine($"y++ = {y++}");
Console.WriteLine($"y = {y}");
3.3 在表达式中的使用 #
csharp
int a = 5;
int b = 2;
int result = a++ + ++b;
int i = 0;
int[] arr = { 1, 2, 3 };
Console.WriteLine(arr[i++]);
Console.WriteLine(arr[++i]);
四、正负号运算符 #
csharp
int a = 10;
int b = -a;
int c = -b;
int d = +a;
五、运算中的类型提升 #
5.1 自动类型提升 #
csharp
int a = 5;
double b = 2.0;
var result = a + b;
byte b1 = 10;
byte b2 = 20;
var sum = b1 + b2;
5.2 类型提升规则 #
text
byte, short, char → int → long → float → double
csharp
byte b = 10;
short s = 20;
int i = b + s;
long l = 100L;
float f = l + 1.5f;
double d = f + 2.5;
六、溢出处理 #
6.1 溢出示例 #
csharp
int max = int.MaxValue;
int overflow = max + 1;
Console.WriteLine(overflow);
6.2 checked关键字 #
csharp
try
{
checked
{
int max = int.MaxValue;
int result = max + 1;
}
}
catch (OverflowException ex)
{
Console.WriteLine($"溢出异常:{ex.Message}");
}
int safeResult = checked(max + 1);
6.3 unchecked关键字 #
csharp
unchecked
{
int max = int.MaxValue;
int result = max + 1;
}
七、数学函数 #
Math类提供常用数学函数:
csharp
double sqrt = Math.Sqrt(16);
double pow = Math.Pow(2, 10);
double abs = Math.Abs(-10);
double round = Math.Round(3.14159, 2);
double floor = Math.Floor(3.9);
double ceiling = Math.Ceiling(3.1);
double max = Math.Max(10, 20);
double min = Math.Min(10, 20);
int sign = Math.Sign(-5);
八、实战示例 #
8.1 计算器 #
csharp
public static class Calculator
{
public static double Add(double a, double b) => a + b;
public static double Subtract(double a, double b) => a - b;
public static double Multiply(double a, double b) => a * b;
public static double Divide(double a, double b)
{
if (b == 0)
throw new DivideByZeroException("除数不能为零");
return a / b;
}
public static double Modulo(double a, double b) => a % b;
}
8.2 数字处理 #
csharp
public static class NumberHelper
{
public static int GetDigitCount(int number)
{
if (number == 0) return 1;
int count = 0;
while (number != 0)
{
number /= 10;
count++;
}
return count;
}
public static int Reverse(int number)
{
int reversed = 0;
while (number != 0)
{
reversed = reversed * 10 + number % 10;
number /= 10;
}
return reversed;
}
public static int SumOfDigits(int number)
{
int sum = 0;
while (number != 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}
}
8.3 时间计算 #
csharp
public static string FormatSeconds(int totalSeconds)
{
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = totalSeconds % 60;
return $"{hours:D2}:{minutes:D2}:{seconds:D2}";
}
九、注意事项 #
9.1 整数除法 #
csharp
double wrong = 1 / 2;
double correct = 1.0 / 2;
double correct2 = (double)1 / 2;
9.2 浮点精度 #
csharp
double a = 0.1 + 0.2;
Console.WriteLine(a);
Console.WriteLine(a == 0.3);
decimal b = 0.1m + 0.2m;
Console.WriteLine(b);
Console.WriteLine(b == 0.3m);
9.3 自增自减使用 #
csharp
int i = 0;
int result = i++ + i++ + i++;
int j = 0;
int a = j++;
int b = j++;
int c = j++;
int result = a + b + c;
十、总结 #
算术运算符要点:
| 运算符 | 说明 |
|---|---|
| + - * / | 四则运算 |
| % | 取余 |
| ++ – | 自增自减 |
| checked | 溢出检查 |
下一步,让我们学习关系运算符!
最后更新:2026-03-26