中缀表达式转化为后缀表达式的算法流程如下:
(1)为了实现一个空堆栈,将结果字符串变量置空。
(2)从左到右读入一个中缀表达式,每次一个字符。
(3)如果字符是操作数,将它添加到结果字符串。
(4)如果字符是个操作符,弹出操作符,直至遇见开括号、优先级较低的操作符或者同一优先级的右结合符号。把这个操作符压入堆栈。
(5)如果字符是个开括号,把它压入堆栈。
(6)如果字符是个闭括号,在遇见开括号前,弹出所有操作符,然后把它们添加到结果字符串。
(7) 如果到达输入字符串的末尾,弹出所有操作符并添加到结果字符串。
后缀表达式算法流程如下:
(1)初始化一个空堆栈
(2)从左到右读入后缀表达式
(3)如果字符是一个操作数,把它压入堆栈。
(4)如果字符是个操作符,弹出两个操作数,执行恰当操作,然后把结果压入堆栈。如果不能够弹出两个操作数,后缀表达式的语法就不正确。
(5)到后缀表达式末尾,从堆栈中弹出结果。若后缀表达式格式正确,那么堆栈应该为空.
中缀表达式的算法如下:
- 使用两个栈,一个栈用于存储操作数,另一个栈用于存储操作符
- 从左往右扫描,遇到操作数入操作数栈
- 遇到操作符时,如果优先级低于或等于栈顶操作符优先级,则从操作数弹出两个元素进行计算,并压入操作数栈,继续与栈顶操作符的比较优先级。
(4)如果遇到操作符高于栈顶操作符优先级,则直接入操作符栈。
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test6
{
class Program
{
public void caidan()
{
Console.WriteLine("*************************************");
Console.WriteLine("1.中缀表达式到后缀表达式的转换");
Console.WriteLine("2.后缀表达式的计算");
Console.WriteLine("3.中缀表达式的计算");
Console.WriteLine("4.退出");
Console.WriteLine("*************************************");
Console.WriteLine("请选择你想要的功能");
Console.WriteLine("*************************************");
}
static void Main(string[] args)
{
Class1 c= new Class1();
Program program1 = new Program();
Console.WriteLine("请输入中缀表达式");
string s = Console.ReadLine();
program1.caidan();
string i = Console.ReadLine();
switch (i)
{
case "1":
Console.WriteLine("转换的式子是:");
Console.WriteLine(Class1.HouEvaluate(s));
Console.ReadLine();
break;
case "2":
Console.WriteLine("运算的最终结果为:");
Class1.Evaluate(s);
break;
case "3":
Class1.ZhongEvaluate(s);
break;
default:
break;
}
}
}
}
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test6
{
public class Class1
{
public static string HouEvaluate(string expression)
{
Stack<char> s1 = new Stack<char>();
StringBuilder result = new StringBuilder();
for (int i = 0; i < expression.Length; i++)
{
char ch = expression[i];
if (char.IsWhiteSpace(ch)) continue;
switch (ch)
{
case '+':
case '-':
while (s1.Count > 0)
{
char c = s1.Pop();
if (c == '(')
{
s1.Push(c);
break;
}
else
{
result.Append(c);
}
}
s1.Push(ch);
result.Append(" ");
break;
case '*':
case '/':
while (s1.Count> 0)
{
char c = s1.Pop();
if (c == '(')
{
s1.Push(c);
break;
}
else
{
if (c == '+' || c == '-')
{
s1.Push(c);
break;
}
else
{
result.Append(c);
}
}
}
s1.Push(ch);
result.Append(" ");
break;
case '(':
s1.Push(ch);
break;
case ')':
while (s1.Count > 0)
{
char c = s1.Pop();
if (c == '(')
{
break;
}
else
{
result.Append(c);
}
}
break;
default:
result.Append(ch);
break;
}
}
while (s1.Count > 0)
{
result.Append(s1.Pop());
}
return result.ToString();
}
public static double Evaluate(string expression)
{
string shizi = HouEvaluate(expression);
Stack<double> s2 = new Stack<double>();
double x, y;
for (int i = 0; i < shizi.Length; i++)
{
char ch = shizi[i];
switch (ch)
{
case ' ': break;
case '+':
x = s2.Pop();
y = s2.Pop();
s2.Push(x + y);
break;
case '-':
x = s2.Pop();
y = s2.Pop();
s2.Push(x - y);
break;
case '*':
x = s2.Pop();
y = s2.Pop();
s2.Push(x * y);
break;
case '/':
x = s2.Pop();
y = s2.Pop();
s2.Push(x / y);
break;
default:
int pos = i;
StringBuilder operand = new StringBuilder();
do
{
operand.Append(shizi[pos]);
pos++;
} while (char.IsDigit(shizi[pos]) || shizi[pos] == '.');
i = --pos;
s2.Push(double.Parse(operand.ToString()));
break;
}
}
Console.WriteLine(s2.Peek());
Console.ReadLine();
return s2.Peek();
}
public static void ZhongEvaluate(string expression)
{
Stack<double> s1 = new Stack<double>();
Stack<char> s2 = new Stack<char>();
for (int i = 0; i < expression.Length; i++)
{
if (expression[i] != '+' && expression[i] != '-' && expression[i] != '*' && expression[i] != '/')
{
for (int j = i; j < expression.Length; j++)
{
if (expression[j] == '+' || expression[j] == '-' || expression[j] == '*' || expression[j] == '/')
{
s1.Push(double.Parse(expression.Substring(i, j - i)));
i = j - 1;
break;
}
if (j == expression.Length - 1)
{
s1.Push(double.Parse(expression.Substring(i, j + 1 - i)));
i = expression.Length;
}
}
}
else
{
if (s2.Count() == 0)
{
s2.Push(expression[i]);
}
else if ((expression[i] == '+' || expression[i] == '-') && (s2.Peek() == '*' || s2.Peek() == '/'))
{
double a1 = 0, b1 = 0;
a1 = s1.Pop();
b1 = s1.Pop();
char ch = s2.Peek();
switch (ch)
{
case '*':
{
s1.Push(a1 * b1);
break;
}
case '/':
{
s1.Push(a1 / b1);
break;
}
}
s2.Pop();
s2.Push(expression[i]);
}
else
{
s2.Push(expression[i]);
}
}
}
Console.WriteLine("运算的最终结果为:");
while (s2.Count() != 0)
{
char ch1 = s2.Pop();
double a = 0, b = 0;
a = s1.Pop();
b = s1.Pop();
switch (ch1)
{
case '+':
s1.Push(a + b);
break;
case '-':
s1.Push(b - a);
break;
case '*':
s1.Push(a * b);
break;
case '/':
s1.Push(b / a);
break;
}
}
Console.WriteLine(s1.Peek());
Console.ReadLine();
}
}
}