永发信息网

关于用C#语言和顺序栈做的简易计算器

答案:1  悬赏:20  手机版
解决时间 2021-08-12 04:39

我写的顺序栈表达式求值的代码,但运行的时候不能正确运算,有哪位大大看看哪里不对,一下是代码:

 //用顺序栈实现算法
        public bool MatchBracket(char[] charlist)
        {
            SeqStack<char> s = new SeqStack<char>(50);
            int len = charlist.Length;
            for (int i = 0; i < len; ++i)
            {
                if (s.IsEmpty())
                {
                    s.Push(charlist[i]);
                }
                else if (((s.GetTop() == '(') && (charlist[i] == ')')) || (s.GetTop() == '[' && charlist[i] == ']'))
                {
                    s.Pop();
                }
                else
                {
                    s.Push(charlist[i]);
                }
            }

            if (s.IsEmpty())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int EvaluateExpression(String calc)
        {
            SeqStack<char> optr = new SeqStack<char>(20);
            SeqStack<int> opnd = new SeqStack<int>(20);
            calc = calc + "#";
            optr.Push('#');
            Char c = '0';
            char theta = '0';
            int a = 0;
            int b = 0;
            Char[] ca = calc.ToCharArray();
            int index = 0;
            while (c != '#')
            {
               
                c = ca[index++];

                if ((c != '+') && (c != '-') && (c != '*') && (c != '/') && (c != '(') && (c != ')')&&(c!='#'))
                {
                    int h = int.Parse(c.ToString());
                    opnd.Push(h);
                }
                else
                {
                    switch (Precede(optr.GetTop(), c))
                    {
                        //如果栈顶操作符优先级高,返回>
                        //如果栈顶操作符优先级低,返回<
                        case '<':
                            optr.Push(c);
                            //c = Convert.ToChar(Console.Read());
                            break;
                        case '=':
                            optr.Pop();
                            //c = Convert.ToChar(Console.Read());
                            break;
                        case '>':
                            //将操作符从optr栈中取出


                            theta = optr.GetTop();
                            //取出两个操作数
                            b = opnd.Pop();
                            a = opnd.Pop();
                            //将计算结果存入open栈
                            opnd.Push(Operate(a, theta, b));
                            break;
                        case 'e':
                            Console.WriteLine("无效的算术表达式");
                            break;
                    } //end of switch
                }//end of else
            }//end of while
            return opnd.GetTop();

        }

        public static char Precede(char a, char b)
        {
            if (a == '+' || a == '-')
            {
                if (b == '+' || b == '-' || b == ')' || b == '#')

                    return '>';

                else

                    return '<';

            }
            if (a == '*' || a == '/')
            {
                if (b == '(')
                    return '<';
                else
                    return '>';
            }
            if (a == '(')
            {
                if (b == ')')
                    return '=';
                if (b == '#')
                    return 'e';
                else
                    return '<';

            }
            if (a == ')')
            {
                if (b == '(')
                    return 'e';
                else
                    return '>';
            }
           else //(a == '#')
            {
                if (b == ')')
                    return 'e';
                if (b == '#')
                    return '=';
                else
                    return '<';
            }

        }

        public static int Operate(int a, char b, int c)
        {
            int d = 0;
            switch (b)
            {
                case '*':
                    d = a * c;
                    break;
                case '+':
                    d = a + c;
                    break;
                case '-':
                    d = a - c;
                    break;
                case '/':
                    d = a / c;
                    break;
            }
            return d;
        }

最佳答案
public Form1()
{
InitializeComponent();
}

//小数点
private void butpiont_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + btn.Text;


}

private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button2_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button3_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button4_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button5_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button6_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button7_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button8_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}

private void button9_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += b
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
是不是是长的帅参军后就可以当大帅?
你最好不要放弃法语的英语意思
我的手机为什么下载的网易账号通拿不回老卡或
在线阅读跳舞之猎国
列表的英语怎么写?
工商管理专业介绍
谁帮忙找个GIF压缩软件
今年如何给宝宝起个好名字(无论男孩女孩)
渑池县三门峡源海国际这个地址在什么地方,我
金豆有事么用啊?
你是我的温暖?
有啥好歌听啊
侠盗猎车手圣安地列斯说使用最新版虚拟光驱载
艾薇儿有没结婚?
考不上大学怎么办??
推荐资讯
8月10号 今天DNF维护 更新文件有毒、请给一个
会唱歌唱但写不出歌谱来?
CF怎么上房啊?
做梦被石头砸是好是坏
怎么排遣心中的压抑
梧州露天影院点击视频它会提示首次点播请下载
有这样的母亲吗?竟然叫自己的儿子去死!
我想寄一棵白杨树苗到美国,还想在包裹里放只
怎样才能让牛仔裤不褪色,牛仔裤掉色后怎么处
我的手机蓝牙用不了了
农夫山泉多少钱一瓶,农夫山泉矿泉水全国统一
不粘锅的油渍怎么清洗,怎么用牙膏清洗油渍
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?