码迷,mamicode.com
首页 > 其他好文 > 详细

第二周-四则运算更新

时间:2016-09-15 00:49:16      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

四则运算功能更新:

HTTPS:https://git.coding.net/li_yuhuan/CalculateTest.git

SSH:git@git.coding.net:li_yuhuan/CalculateTest.git

 

代码:

        static void Main(string[] args)
        {
            int length = args.Length;
            
            if (0 == length)
            {
                for (int i = 0; i < 20; i++)
                {
                    ShowTest(false);
                }
                Console.WriteLine("你一共答对" + m_correctNum + "道题,共20道题");
            }
            else if (2 == length)
            {
                if ("-c" == args[0])
                {
                    string str = args[1];

                    if (IsPositiveInt(str))
                    {
                        int count = int.Parse(str);

                        for (int i = 0; i < count; i++)
                        {
                            ShowTest(true);
                        }
                    }
                    else
                    {
                        Console.WriteLine("题目数量必须是 正整数!");
                    }
                }
            }

判断通过控制台传入主函数的参数个数分情况处理;

    1)当参数个数为0时,循环出20道题并判断输入的答案是否正确,最后给出正确的个数;

    2)参数为2个,格式为-c + 正整数时,则打印与正整数相等个数的题目(带答案);当-c后的输入不是正整数,则显示提示;

----------------------------------------------------------------------------------------------------------------------

 

        static private void ShowTest(bool showResult)
        {
            string expression = CreateExpression();  //生成试题

            Queue<string> qexp = SplitExpress(expression); //把表达式存入队列
            List<string> exp = InorderToPostorder(qexp); //表达式由中缀转为后缀
            double result = 0;
            IsResult(exp, out result);//根据后缀表达式计算结果

            if (showResult)
            {//打印试题及结果
                Console.WriteLine(expression + "=                                 " + result);
            }
            else
            {//出题,并读入用户输入的值,判断是否正确
                Console.WriteLine(expression);
                Console.Write("?");

                string input = Console.ReadLine();

                if (IsNumeric(input))
                {
                    double num = double.Parse(input);

                    if (num == result)
                    {
                        Console.WriteLine("答对啦,你真是个天才!");
                        m_correctNum++;
                    }
                    else
                    {
                        Console.WriteLine("再想想吧,答案似乎是" + result + "喔!");
                    }
                }
            }
            
        }

根据传入的bool参数,进行出题或打印试题及答案;

-----------------------------------------------------------------------------------------------------------------

 

static private string CreateExpression()
        {
            string expression = string.Empty;
            int num;
            int index;
            char op;

            List<int> position = new List<int>() {0, 2, 4 };
            List<char> oplist = new List<char>() { +,-, *, /};
            List<string> divlist = new List<string>() { "1","2","4","5","8"};
            List<string> expList = new List<string>();

            num = ran.Next(10);
            expList.Add(num.ToString());

            for (int i = 0; i < 3; i++)
            {
                index = ran.Next(oplist.Count());
                op = oplist[index];

                if (/ == op)
                {
                    oplist.RemoveAt(3);
                    index = ran.Next(divlist.Count());
                    num = int.Parse(divlist[index]);
                }
                else
                {
                    num = ran.Next(10); 
                }

                expList.Add(op.ToString());
                expList.Add(num.ToString());
            }

            int pos;
            index = expList.IndexOf("/");

            if (-1 == index)
            {
                pos = position[ran.Next(3)];
            }
            else
            {
                if (1 == index)
                {
                    position.RemoveAt(1);
                }
                else if (3 == index)
                {
                    position.RemoveAt(2);
                }

                pos = position[ran.Next(position.Count())];
            }

            expList.Insert(pos, "(");
            expList.Insert(pos + 4, ")");

            foreach (string str in expList)
            {
                expression += str;
            }

            return expression;
        }

生成算式:

生成四个随机数,和三个随机符号,如果符号是除号,则下一个随机数从(1,2,4,5,8)中随机,因为我怕会出现无限循环的小数,并且限制算式中最多只有一个除号.生成算式成功后,根据除号的位置来设定括号的位置.(除号后如果有括号且是加减法,可能会除不尽).最后返回算式;

这段算法有待改进,规避了很多情况;具体的完整代码上传到了coding.net

-----------------------------------------------------------------------------------------------------------------

运行样例:

功能1&2:

技术分享

技术分享

---------------------------------------------------------------------------------------------------------------------

功能3:

技术分享

技术分享

 

第二周-四则运算更新

标签:

原文地址:http://www.cnblogs.com/li-yuhuan/p/5873922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!