四则运算程序生成

作业基本要求

 基本要求:

  • 除了整数以外,还能支持真分数的四则运算。
  • 对实现的功能进行描述,并且对实现结果要求截图。

设计思想:首先思考运算数字范围,考虑小学生的运算能力,然后分类运算方法,分为加减乘除混合运算,

然后可以选择是继续做题还是退出程序,可以总结做对或者做错的题数,方便计算正确以及错误的题数。

源代码

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Text.RegularExpressions;  
  
namespace ConsoleApplication1  
{  
    class Class1  
    {  
        private static Dictionary<string, int> _operatorLevel;  
  
        public static void Main(string[] arg)  
        {  
            Console.WriteLine("Type in the source expr");  
            string sourceExpression = Console.ReadLine();  
            
            Console.WriteLine(InsertBlank(sourceExpression));  
            
            string rpnExperssion = ConvertToRPN(InsertBlank(sourceExpression));  
            Console.WriteLine(rpnExperssion);  
            
            Console.WriteLine(GetResult(rpnExperssion));  
            Console.ReadLine();  
        }  
  
        public static double GetValue(double left, double right, char _operator)  
        {  
            switch (_operator)  
            {  
                case '+':  
                    return left+right;  
                case '-':  
                    return left-right;  
                case '*':  
                    return left*right;  
                case '/':  
                    return left/right;  
            }  
            return 0;  
        }  
  
        public static double GetResult(string source)  
        {  
            Stack<string> stack = new Stack<string>();  
            var list = source.Split(' ');  
            for (int i = 0; i < list.Length; i++)  
            {  
                string current = list[i];  
                if (Regex.IsMatch(current, "^([0-9]{1,}){1}"))  
                {  
                    stack.Push(current);  
                }  
                else if (OperatorLevel.ContainsKey(current))  
                {  
                    double right = double.Parse(stack.Pop());  
                    double left = double.Parse(stack.Pop());  
                    stack.Push(GetValue(left, right, current[0]).ToString());  
                }  
            }  
            return double.Parse(stack.Pop());  
        }  
  
        public static string ConvertToRPN(string source)  
        {  
            StringBuilder result = new StringBuilder();  
            Stack<string> stack = new Stack<string>();  
            string[] list = source.Split(' ');  
            for (int i = 0; i < list.Length ; i++)  
            {  
                string current = list[i];  
                if (Regex.IsMatch(current, "^([0-9]{1,}){1}"))  
                {  
                    result.Append(current + " ");  
                }  
                else if (OperatorLevel.ContainsKey(current))  
                {  
                    if (stack.Count > 0)  
                    {  
                        var prev = stack.Peek();  
                        if (prev == "(")  
                        {  
                            stack.Push(current);  
                            continue;  
                        }  
                        if (current == "(")  
                        {  
                            stack.Push(current);  
                            continue;  
                        }  
                        if (current == ")")  
                        {  
                            while (stack.Count > 0 && stack.Peek() != "(")  
                            {  
                                result.Append(stack.Pop() + " ");  
                            }  
                            //Pop the "("   
                            stack.Pop();  
                            continue;  
                        }  
                        if (OperatorLevel[current] < OperatorLevel[prev])  
                        {  
                            while (stack.Count > 0)  
                            {  
                                var top = stack.Pop();  
                                if (top != "(" &&  
                                    top != ")")  
                                {  
                                    result.Append(top + " ");  
                                }  
                                else  
                                {  
                                    break;  
                                }  
                            }  
                            stack.Push(current);  
                        }  
                        else  
                        {  
                            stack.Push(current);  
                        }  
                    }  
                    else  
                    {  
                        stack.Push(current);  
                    }  
                }  
            }  
            if (stack.Count > 0)  
            {  
                while (stack.Count > 0)  
                {  
                    var top = stack.Pop();  
                    if (top != "(" && top != ")")  
                    {  
                        result.Append(top + " ");  
                    }  
                }  
            }  
            return result.ToString();  
        }  
  
        public static string InsertBlank(string source)  
        {  
            StringBuilder sb = new StringBuilder();  
            var list = source.ToCharArray();  
            foreach (var temp in list)  
            {  
                if (OperatorLevel.ContainsKey(temp.ToString()))  
                {  
                    sb.Append(" ");  
                    sb.Append(temp.ToString());  
                    sb.Append(" ");  
                }  
                else  
                {  
                    sb.Append(temp);  
                }  
            }  
            return sb.ToString();  
        }  
  
//运算符字典 方便查询运算符优先级 
            public static Dictionary<string, int> OperatorLevel  
        {  
            get  
            {  
                if(_operatorLevel==null)  
                {  
                    _operatorLevel = new Dictionary<string, int>();  
                    _operatorLevel.Add("+", 0);  
                    _operatorLevel.Add("-", 0);  
                    _operatorLevel.Add("(", 1);  
                    _operatorLevel.Add("*", 1);  
                    _operatorLevel.Add("/", 1);  
                    _operatorLevel.Add(")", 0);  
                }  
                return _operatorLevel;  
            }  
        }  
    }
}
void main()
{
    printf("\n欢迎进入小学四则运算\n\n");
        while(1)
        {    
            int con=0,choose=0;
            printf("请选择:\n");
            printf("加法运算请输入(1)\n");
            printf("减法运算请输入(2)\n");
            printf("乘法运算请输入(3)\n");
            printf("除法运算请输入(4)\n");
            printf("混合运算请输入(5)\n");
            if(con==0)
                scanf("%d",&choose);
            switch(choose)
            {
                case 1:add();break;
                case 2: minu(); break;
                case 3: mul(); break;
                case 4: di(); break;
                case 5: hunhe(); break;
            }
            printf("\n重新选择?请输入(1)\n");
            printf("\n退出运算?请输入(2)\n");
            scanf("%d",&con);
            if(con==1)
                con=1;
            if(con==2)
                break;
        }
        printf("您总共完成%d道题\n正确%d道\n错误%d道\n",right+wrong,right,wrong);
}
 

程序截图

 

 

 

 

 

 

总结:

通过这次作业,我发现自己在编程方面还有许多的不足。自己在C#方面还有许多的东西要学习。这次作业中我通过网络,书籍,还有询问同学,才将作业完成。编程需要平时的积累,在平日我们积少成多,在编程时我们才能更好地编程。以后在上课时,我要更多的跟着老师走,有不懂的多问老师。

posted @ 2016-03-15 16:26  耿健  阅读(249)  评论(0编辑  收藏  举报