Exemple #1
0
 MyNumber OneStepCalculate(MyNumber num1, MyNumber num2, int opera)
 {
     if (opera == 101)
     {
         return(num1 + num2);
     }
     else if (opera == 102)
     {
         return(num1 - num2);
     }
     else if (opera == 103)
     {
         return(num1 * num2);
     }
     else if (opera == 104)
     {
         return(num1 / num2);
     }
     else if (opera == 105)
     {
         return(num1 ^ num2);
     }
     else
     {
         MyNumber error = new MyNumber(-1);
         return(error);
     }
 }
Exemple #2
0
        /*要求用户输入答案并与结果比较*/
        public int GetResult(MyNumber res, string yourans)
        {
            int formflag = CheckYourAnswer(yourans);        //判断答案格式是否正确

            if (formflag == 1)
            {
                //sign代表答案符号,ansnum[0]为分子,[1]为分母
                int[] ansnum = new int[] { 0, 1 };
                int   sign   = TurnToNumber(yourans, ansnum);
                if (ansnum[1] != 0)                         //判断分母是否为0
                {
                    MyNumber yourres = new MyNumber(ansnum[0], ansnum[1], sign);
                    if (yourres == res)
                    {
                        return(1);
                    }
                    //cout << "Right" << endl;
                    else
                    {
                        return(0);
                    }
                    //cout << "Wrong" << endl;
                }
                else
                {
                    return(0);
                }
            }
            else
            {
                return(3);
            }
        }
        public static MyNumber operator +(MyNumber a, MyNumber b)
        {
            MyNumber c = new MyNumber();

            c.numerator   = a.numerator * b.denominator + a.denominator * b.numerator;
            c.denominator = a.denominator * b.denominator;
            return(c);
        }
        public static MyNumber operator ^(MyNumber a, MyNumber b)
        {
            MyNumber c = new MyNumber(1);

            for (int i = 0; i < b.numerator; i++)
            {
                c = c * a;
            }
            return(c);
        }
        public static MyNumber operator /(MyNumber a, MyNumber b)
        {
            MyNumber c = new MyNumber
            {
                numerator   = a.numerator * b.denominator,
                denominator = a.denominator * b.numerator
            };

            return(c);
        }
Exemple #6
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (ansText.Text == "")
            {
                ansText.Focus();
                return;
            }
            Ans      ans              = new Ans();
            MyNumber correctAnswer    = calculate.GetAns(save, build.p);
            int      ansflag          = ans.GetResult(correctAnswer, this.ansText.Text);
            string   correctAnswerStr = correctAnswer.M_ToString();

            if (ansflag == 1)
            {
                timer1.Stop();
                MessageBox.Show("Accepted!\n");
                timer1.Start();
                score++;
                scoreText.Text = "得分:" + score.ToString();
                button1_Click(null, null);
                this.ansText.Text = "";
                totalTime         = 20;
            }
            else if (ansflag == 0)
            {
                timer1.Stop();
                MessageBox.Show("WrongAnswer\n" + "Correct Answer:" + correctAnswerStr);
                timer1.Start();
                chance = chance - 1;
                if (chance <= 0)
                {
                    MessageBox.Show("错误次数过多!");
                    Application.Exit();
                }
                hp.Text = "剩余次数" + chance.ToString();
                button1_Click(null, null);
                this.ansText.Text = "";
                totalTime         = 20;
            }
            else
            {
                timer1.Stop();
                MessageBox.Show("输入格式有误!");
                timer1.Start();
                this.ansText.Text = "";
                this.ansText.Focus();
            }
            record.Text += build.M_ToString() + "=" + correctAnswerStr + "\r\n";//保存历史记录
        }
Exemple #7
0
        public MyNumber GetAns(int[] operation, int length)
        {
            /*将四则运算映射到一串十进制数,0-100为运算数,
             * 101-104分别代表+,-,*,/,105为乘方,106为左括号,107为右括号*/
            Stack <int>      operators = new Stack <int>();
            Stack <MyNumber> operand   = new Stack <MyNumber>();

            for (int i = 0; i < length; i++)
            {
                if (operation[i] >= 0 && operation[i] <= 100)
                {
                    MyNumber temp = new MyNumber(operation[i]);
                    operand.Push(temp);
                }
                else if (operation[i] == 105 || operation[i] == 106)    //左括号与乘方必定入栈
                {
                    operators.Push(operation[i]);
                }
                else if (operation[i] == 103 || operation[i] == 104)    //乘除会弹出乘方与乘除
                {
                    while (operators.Count != 0 && (operators.Peek() == 103 || operators.Peek() == 104 || operators.Peek() == 105))
                    {
                        int opera = operators.Pop();
                        //operators.Pop();
                        MyNumber num1 = operand.Pop();
                        //operand.Pop();
                        MyNumber num2 = operand.Pop();
                        //operand.Pop();
                        operand.Push(OneStepCalculate(num2, num1, opera));
                    }
                    operators.Push(operation[i]);
                }
                else if (operation[i] == 101 || operation[i] == 102)        //加减可能弹出乘除与乘方
                {
                    while (operators.Count != 0 && (operators.Peek() != 106 && operators.Peek() != 107))
                    {
                        int opera = operators.Pop();
                        //operators.pop();
                        MyNumber num1 = operand.Pop();
                        //operand.pop();
                        MyNumber num2 = operand.Pop();
                        //operand.pop();
                        operand.Push(OneStepCalculate(num2, num1, opera));
                    }
                    operators.Push(operation[i]);
                }
                else if (operation[i] == 107)               //右括号会一直弹出直至左括号
                {
                    while (operators.Peek() != 106)
                    {
                        int opera = operators.Pop();
                        //operators.pop();
                        MyNumber num1 = operand.Pop();
                        //operand.Pop();
                        MyNumber num2 = operand.Pop();
                        //operand.pop();
                        operand.Push(OneStepCalculate(num2, num1, opera));
                    }
                    operators.Pop();
                }
            }
            while (operators.Count != 0)
            {
                int opera = operators.Pop();
                //operators.Pop();
                MyNumber num1 = operand.Pop();
                //operand.Pop();
                MyNumber num2 = operand.Pop();
                //operand.pop();
                operand.Push(OneStepCalculate(num2, num1, opera));
            }
            return(operand.Pop());
        }