在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication7 { class Calculator { static void Main(string[] args) { //string exp = "345*2^(4+2*2-1*3)-5"; for (; ; ) { Console.WriteLine("input a values:"); string exp = Console.ReadLine(); Console.WriteLine("output reslut:"); Console.WriteLine(Calculate(exp)); Console.WriteLine("END"); } // string exp = "130+20"; //Console.ReadKey(); } public static double Calculate(string _exp) { Stack operandStack = new Stack();//操作数 Stack operatorStack = new Stack();//运算符 operatorStack.Push('#'); char[] exp = (_exp + "#").ToCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < exp.Length; i++) { if (IsPerand(exp[i])) { sb.Append(exp[i]); } else { string sbstr = sb.ToString(); if (sbstr!="") { operandStack.Push(sb); } sb = new StringBuilder(); //优先级大于当前栈顶运算符的优先级 if (PriorityBeforeIn(exp[i]) > priorityAferIn((char)operatorStack.Peek())) { operatorStack.Push(exp[i]);//#* } else if (PriorityBeforeIn(exp[i]) < priorityAferIn((char)operatorStack.Peek())) { object sbd =operandStack.Pop(); string str = sbd.ToString(); object sbd1 =operandStack.Pop(); string str1 = sbd1.ToString(); double dou= Convert.ToDouble(str); double dou1= Convert.ToDouble(str1); operandStack.Push(Count((char)operatorStack.Pop(),dou,dou1)); i--; } else if (PriorityBeforeIn(exp[i]) == priorityAferIn((char)operatorStack.Peek())) { if (exp[i] == ')') { while (true) { char c = (char)operatorStack.Peek(); if (c == '(') { operatorStack.Pop(); break; } object sbd = operandStack.Pop(); string str = sbd.ToString(); object sbd1 = operandStack.Pop(); string str1 = sbd1.ToString(); double dou = Convert.ToDouble(str); double dou1 = Convert.ToDouble(str1); operandStack.Push(Count((char)operatorStack.Pop(), dou, dou1)); } } else if (exp[i] == '#') { while (true) { char c = (char)operatorStack.Peek(); if (c == '#') { operatorStack.Pop(); break; } object sbd = operandStack.Pop(); string str = sbd.ToString(); object sbd1 = operandStack.Pop(); string str1 = sbd1.ToString(); double dou = Convert.ToDouble(str); double dou1 = Convert.ToDouble(str1);//出错 operandStack.Push(Count((char)operatorStack.Pop(), dou, dou1)); } } } } } return (double)operandStack.Pop(); } //判断字符是否为操作数 private static bool IsPerand(char c) { bool flag = false;//不执行 switch (c) { case'.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': flag = true; break; } return flag; } //获取操作符入栈前的优先级 private static int PriorityBeforeIn(char _opertor) { int priority = -1; switch (_opertor) { case '#': priority = 0; break; case '(': priority = 8; break; case '^': priority = 6; break; case '*': case '/': case '%': priority = 4; break; case '+': case '-': priority = 2; break; case ')': priority = 1; break; default: break; } return priority; } //获取操作符入站后的优先级 private static int priorityAferIn(char _operator) { int priority = -1; switch (_operator) { case '#': priority = 0; break; case '(': priority = 1; break; case '^': priority = 7; break; case '*': case '/': case '%': priority = 5; break; case '+': case '-': priority = 3; break; case ')': priority = 8; break; default: break; } return priority; } //按照指定操作对两个数字进行相应的计算 private static double Count(char _operator, double a, double b) { double result = 0; switch (_operator) { case '^': result = (double)Math.Pow(b, a); break; case '*': result = b * a; break; case '/': result = b / a; break; case '%': result = b % a; break; case '+': result = b + a; break; case '-': result = b - a; break; default: break; } return result; } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论