c#表达式求值问题

中缀表达式转化为后缀表达式的算法流程如下:

(1)为了实现一个空堆栈,将结果字符串变量置空。

(2)从左到右读入一个中缀表达式,每次一个字符。

(3)如果字符是操作数,将它添加到结果字符串。

(4)如果字符是个操作符,弹出操作符,直至遇见开括号、优先级较低的操作符或者同一优先级的右结合符号。把这个操作符压入堆栈。

(5)如果字符是个开括号,把它压入堆栈。

(6)如果字符是个闭括号,在遇见开括号前,弹出所有操作符,然后把它们添加到结果字符串。

(7) 如果到达输入字符串的末尾,弹出所有操作符并添加到结果字符串。

后缀表达式算法流程如下:

    (1)初始化一个空堆栈

(2)从左到右读入后缀表达式

(3)如果字符是一个操作数,把它压入堆栈。

(4)如果字符是个操作符,弹出两个操作数,执行恰当操作,然后把结果压入堆栈。如果不能够弹出两个操作数,后缀表达式的语法就不正确。

(5)到后缀表达式末尾,从堆栈中弹出结果。若后缀表达式格式正确,那么堆栈应该为空.

中缀表达式的算法如下:

  1. 使用两个栈,一个栈用于存储操作数,另一个栈用于存储操作符
  2. 从左往右扫描,遇到操作数入操作数
  3. 遇到操作符时,如果优先级低于或等于栈顶操作符优先级,则从操作数弹出两个元素进行计算,并压入操作数栈,继续与栈顶操作符的比较优先级

(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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值