C#语言实现计算机的详细代码
时间: 2024-04-01 20:38:39 浏览: 23
好的,下面是一个基于C#语言实现的计算器代码,支持加减乘除和括号运算等基本功能:
```csharp
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个表达式:");
string input = Console.ReadLine();
double result = Calculate(input);
Console.WriteLine("计算结果为:" + result);
Console.ReadKey();
}
static double Calculate(string input)
{
// 去除空格
input = input.Replace(" ", "");
// 括号运算
while (input.Contains("("))
{
int left = input.LastIndexOf("(");
int right = input.IndexOf(")", left);
string subExpr = input.Substring(left + 1, right - left - 1);
double subResult = Calculate(subExpr);
input = input.Substring(0, left) + subResult + input.Substring(right + 1);
}
// 乘除运算
while (input.Contains("*") || input.Contains("/"))
{
int index = input.IndexOf("*");
if (index == -1 || (input.IndexOf("/") != -1 && input.IndexOf("/") < index))
{
index = input.IndexOf("/");
}
string leftExpr = GetLeftExpr(input, index);
string rightExpr = GetRightExpr(input, index);
double leftValue = Convert.ToDouble(leftExpr);
double rightValue = Convert.ToDouble(rightExpr);
double result = 0;
if (input[index] == '*')
{
result = leftValue * rightValue;
}
else
{
result = leftValue / rightValue;
}
input = input.Substring(0, index - leftExpr.Length) + result + input.Substring(index + rightExpr.Length + 1);
}
// 加减运算
double finalResult = 0;
string[] terms = input.Split(new char[] { '+', '-' });
int index = 0;
for (int i = 0; i < terms.Length; i++)
{
if (terms[i] == "")
{
continue;
}
double value = Convert.ToDouble(terms[i]);
if (index == 0)
{
finalResult = value;
}
else
{
if (input[index - 1] == '+')
{
finalResult += value;
}
else
{
finalResult -= value;
}
}
index += terms[i].Length + 1;
}
return finalResult;
}
static string GetLeftExpr(string input, int index)
{
int i = index - 1;
while (i >= 0 && "0123456789.".IndexOf(input[i]) != -1)
{
i--;
}
return input.Substring(i + 1, index - i - 1);
}
static string GetRightExpr(string input, int index)
{
int i = index + 1;
while (i < input.Length && "0123456789.".IndexOf(input[i]) != -1)
{
i++;
}
return input.Substring(index + 1, i - index - 1);
}
}
}
```
这个代码使用了递归来实现括号运算,使用了循环来实现乘除运算和加减运算。注意,在实际开发中,还需要考虑很多边界情况,比如除数为0等异常情况。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)