【发布时间】:2023-03-03 11:58:02
【问题描述】:
我的计算器在没有括号的情况下几乎可以完美运行(它有一些错误)。这个计算器可以进行多种运算(例如 12+345*2-100 //602)。 我希望将括号和其中的字符串替换为结果,然后继续计算。 如何修复括号的方法以使其按预期工作?
计算步骤:
- 找到一个符号
- 查找符号周围的数字
- 进行计算并用字符串中的结果替换数字和符号
- 检查任何符号,如果有则重新开始
-
给出数学问题的结果
//Finds numbers around an symbol static void FindNumbers(string equation, int start, char symbol) { number1 = 0; number2 = 0; number1String = string.Empty; number2String = string.Empty; if (equation[start] == symbol) { for (int j = start - 1; j >= 0; j--)//Finds left number around the symbol { if (char.IsDigit(equation[j])) { number1String = equation[j] + number1String; } else { break; } } for (int j = start + 1; j < equation.Length; j++)//Finds right number around the symbol { if (char.IsDigit(equation[j])) { number2String += equation[j]; } else { break; } } number1 = int.Parse(number1String); number2 = int.Parse(number2String); } return; } //Devision and Multiplication static void Priority1(string equation, int start) { for (int i = start; i < equation.Length; i++)//Multiplication { if (equation[i] == '*') { symbol = equation[i]; FindNumbers(equation, i, symbol); currentresult = number1 * number2; equation = equation.Replace(number1.ToString() + symbol + number2.ToString(), currentresult.ToString()); } } for (int i = 0; i < equation.Length; i++)//Devision { if (equation[i] == '/') { symbol = equation[i]; FindNumbers(equation, i, symbol); currentresult = number1 / number2; equation = equation.Replace(number1.ToString() + symbol + number2.ToString(), currentresult.ToString()); } } Priority2(equation, 0); } //Addition and Devision static void Priority2(string equation, int start) { for (int i = start; i < equation.Length; i++)//Addition { if (equation[i] == '+') { symbol = equation[i]; FindNumbers(equation, i, symbol); currentresult = number1 + number2; equation = equation.Replace(number1.ToString() + symbol + number2.ToString(), currentresult.ToString()); } } for (int i = 0; i < equation.Length; i++)//Devision { if (equation[i] == '-') { symbol = equation[i]; FindNumbers(equation, i, symbol); currentresult = number1 - number2; equation = equation.Replace(number1.ToString() + symbol + number2.ToString(), currentresult.ToString()); } } for (int i = 0; i < equation.Length; i++)//Checks if there are more symbols in the string { if (char.IsSymbol(equation[i])) { Priority1(equation, 0); } } tempresult = equation; Console.WriteLine("Result : " + equation); } //Brackets static void Brackets(string equation, int index) { for (int i = index; index < equation.Length; index++) { if (equation[index] == '(') { index += 1; Brackets(equation, index); for (int j = index; j < equation.Length; j++) { if (equation[j] == ')') { tempresult = temp; Console.WriteLine("..." + tempresult); break; } temp += equation[j]; } Priority1(tempresult, index); equation = equation.Replace('(' + temp.ToString() + ')', tempresult.ToString()); Console.WriteLine("." + equation); } } Priority1(equation, 0); } static void Main(string[] args) { equation = Console.ReadLine(); Brackets(equation, 0); Console.ReadKey(); }
【问题讨论】:
-
第 0 步:定义表达式语法
-
这闻起来像作业!还有我会尝试作为 TDD Kata 的东西
-
其实这不是作业,但我在书中找到了一些东西,然后我想到了,所以我决定试着去做。
标签: c# string calculator