个人作业——计应191(西)第五组周伟春

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace 四则运算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Stack<string> stack_operator = new Stack<string>();//运算符栈
        Stack<string> stack_number = new Stack<string>();//数字栈
        string str = " ";
        string substr = "  ";


        private bool judgenumber(string text)//判断是否为数字
        {
            try
            {
                int var1 = Convert.ToInt32(text);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private bool judgeoperator(string text)//判断是否为运算符
        {
            if (text == "(" || text == ")" || text == "+" || text == "-" || text == "*" || text == "/")
            {
                return true;
            }
            else
                return false;
        }
        public object addition(object a, object b)//加法
        {
            Decimal d1 = Decimal.Parse(a.ToString());
            Decimal d2 = Decimal.Parse(b.ToString());
            return d2 + d1;
        }
        public object subduction(object a, object b)//减法
        {
            Decimal d1 = Decimal.Parse(a.ToString());
            Decimal d2 = Decimal.Parse(b.ToString());
            return d2 - d1;
        }
        public object multiplication(object a, object b)//乘法
        {
            Decimal d1 = Decimal.Parse(a.ToString());
            Decimal d2 = Decimal.Parse(b.ToString());
            return d2 * d1;
        }
        public object division(object a, object b)//除法
        {
            Decimal d1 = Decimal.Parse(a.ToString());
            Decimal d2 = Decimal.Parse(b.ToString());
            return d2 / d1;
        }
        int judgelevel(string text)//判断优先级
        {
            if (text.Equals("("))
            {
                return 1;
            }
            else if (text.Equals(")") || text.Equals("(") || text.Equals(")"))
            {
                return 1;
            }
            else if (text.Equals("+") || text.Equals("-"))
            {
                return 2;
            }
            else if (text.Equals("*") || text.Equals("/"))
            {
                return 3;
            }
            else
                return 10;


        }
        int operator_dected(string types, string a, string b)//根据运算符的类型返回对应的值
        {
            if (types == "+")
            {
                return Convert.ToInt32(addition(a, b));
            }
            else if (types == "-")
            {
                return Convert.ToInt32(subduction(a, b));
            }
            else if (types == "*")
            {
                return Convert.ToInt32(multiplication(a, b));
            }
            else if (types == "/")
            {
                return Convert.ToInt32(division(a, b));
            }
            else
                return 999;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            stack_number.Clear();//清空栈
            stack_operator.Clear();
            str = textBox1.Text + "!";//!为结束运算符
            int temp_count = 0;
            try
            {
                for (int i = 0; i < str.Length; i++)
                {
                    substr = str.Substring(i, 1);
                    if (judgenumber(substr))//如果是数字
                    {
                        if (temp_count == 0)
                        {
                            stack_number.Push(substr);
                        }
                        else
                            temp_count--;
                        if (judgenumber(str.Substring(i + 1, 1)))
                        {
                            string link1 = stack_number.Pop();
                            link1 += str.Substring(i + 1, 1);
                            stack_number.Push(link1);
                            temp_count++;
                        }
                    }
                    else if (judgeoperator(substr))
                    {
                        if (stack_operator.Count >= 1)
                        {
                            int new1 = judgelevel(substr);
                            int old1 = judgelevel(stack_operator.Peek());
                            if (old1 < new1 || substr == "(")//判断优先级
                            {
                                stack_operator.Push(substr); //将运算符插入栈中
                            }
                            else
                            {
                                if (substr == ")")
                                {
                                    for (; stack_operator.Count > 0; stack_operator.Pop())
                                    {
                                        if (stack_operator.Contains("(") && stack_operator.Peek() == "(")
                                        {
                                            stack_operator.Pop();
                                            break;
                                        }
                                        else
                                        {
                                            int temp1 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                                            int temp2 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                                            stack_number.Push(operator_dected(stack_operator.Peek(), temp1.ToString(), temp2.ToString()).ToString());
                                        }
                                    }
                                }
                                else
                                {
                                    for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop())
                                    {
                                        string temp_a = substr;
                                        int new2 = judgelevel(temp_a);
                                        int old2 = judgelevel(stack_operator.Peek());
                                        if (old2 < new2 || substr == "(")//判断优先级
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                                            int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                                            stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString());
                                        }
                                    }
                                    stack_operator.Push(substr);
                                }
                            }
                        }
                        else
                        {
                            stack_operator.Push(substr);


                        }
                    }
                    else if (substr == "!")
                    {
                        for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop())
                        {
                            int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                            int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
                            stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString());
                        }
                        label1.Text = "结果:" + stack_number.Peek();
                    }
                    else
                    {
                        MessageBox.Show("输入有非法内容,注意不能有中文括号!");
                        break;
                    }
                }
            }
            catch
            {


            }
        }
    }
}
————————————————
版权声明:本文为CSDN博主「ytyur6r」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ytyur6r/article/details/79894976

任务内容计划共完成需要的时间(h)实际完成需要的时间(h)
计划 12.5 13
开发 10 11
需求分析 (包括学习新技术)  0.5  1
· 生成设计文档  1  2
· 设计复审 (和同事审核设计文档)  1  2
代码规范 (为目前的开发制定合适的规范)  1  1.5
具体设计  1  1
具体编码 2 4
· 代码复审 1 1.5
· 测试(自我测试,修改代码,提交修改) 1 1.5
报告  2 3
· 测试报告 1  2
计算工作量 1 2
· 事后总结 ,并提出过程改进计划  1  1

个人总结:

此次项目,加深了我对C#语言知识的充分运用以及理解,使自己的编程水平得到了一点点的提高。从上面的计划与实现表格也能看出来,实际上我的开发时间是预估时间的两倍之久。并且投入在设计上的时间非常之长(需求分析+生成设计文档+设计复审+具体设计)。本次个人项目中学到最多的不是关于C#的语法说明,更不是一些算法的应用技巧,我觉得收获最多的是关于想与做的协调的认识。我觉得自己在处理问题方面的能力也提高了,比如在编程中有时会忘记一些小步骤,未给变量赋初值就直接使用,导致程序无法运行。经历了计组实验,发现了提前设计的必要性与反思的收获。再之后就是在面向对象课程的训练之下,对于细节的注意和关于出错小心翼翼的处理。这时我会认真思考问题,调试代码,查找错误,理顺这其中的逻辑关系,然后再对程序进行修改,从而克服困难。

posted @ 2021-04-11 20:31  计应191西五组  阅读(55)  评论(0)    收藏  举报