混合运算改进(代码)

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

namespace 算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义变量
        public static int count = -1;
        public static int right = 0;
        public static int sun;
        public static int shi = 0;
        //产生随机数
        private void suiji() 
        {
            Random ra = new Random();
            int h = int.Parse(textBox4.Text) + 1;
            int j = int.Parse(textBox5.Text);
            int a, b, c;
            a = ra.Next(h, j);
            b = ra.Next(h, a);
            c = ra.Next(a, j);
            label2.Text = a.ToString();
            label4.Text = b.ToString();
            label7.Text = c.ToString();           
            textBox1.Text = "";
            count++;
        }
        //随机产生加减乘除
        private void suanfa()
        {
            string[] oper = new string[] { "+", "-", "*", "/" };
            string f = oper[new Random().Next(0, 4)];
            string h = oper[new Random().Next(0, 4)];
            label3.Text = f;
            label6.Text = h;
 
        }
        //得到正确答案!
        private void zhenque()
        {
           
            if (comboBox1.Text == "前两个加括号")
            {
               
            }
            if (comboBox1.Text == "不加括号")
            {
                kuohao()
            }
            if (comboBox1.Text == "后两个加括号")
            {
                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (shi <= 0)
            {
                timer1.Enabled = false;
                textBox1.Enabled = false;
                MessageBox.Show("时间到!");
                Form2 da = new Form2();
                da.ShowDialog();

            }


            shi = shi - 1;
            textBox2.Text = shi.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            shi = int.Parse(textBox2.Text);
            textBox2.Text = shi.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            suiji();
            suanfa();

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //判定数字是否合法!
                int i = 0;
                string an = textBox1.Text;
                if (int.TryParse(an, out i) == false)
                {
                    MessageBox.Show("你输得的数字不合法!");

                }
                //判定是否正确
                zhenque();

                if (textBox1.Text == sun.ToString())
                {
                    right++;
                }
                suiji();
                suanfa();
                //判断是否达到总题数
                int q = int.Parse(textBox3.Text);
                if (count == q)
                {
                    timer1.Enabled = false;
                    textBox3.Enabled = false;
                    Form2 da = new Form2();
                    da.ShowDialog();
                }

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            textBox3.Enabled = false;
            Form2 da = new Form2();
            da.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a=comboBox1.Text;
            if (a == "前两个加括号")
            {
                av("(",")","" ,"" );
            }
            if (a == "后两个加括号")
            {
                av("", "", "(", ")");
            }
            if (a == "不加括号")
            {
                av("", "", "", "");
            }
         
        }
        private  void kuohao()
        {
            string f = label3.Text;
            String g = label6.Text;
            int x= int.Parse(label2.Text);
            int y=int.Parse(label4.Text);
            int z= int.Parse(label7.Text);
            string fg = f + g;
            switch (fg)
            {
                case "++":
                    sun =x+y+z;
                    break;
                case "+-":
                    sun = x + y - z;
                    break;
                case "+*":
                    sun = x + y * z;
                    break;
                case "+/":
                    sun = x +y / z;
                    break;
                case "-+":
                    sun = x-y +z;
                    break;
                case "--":
                    sun = x- y - z;
                    break;
                case "-*":
                    sun =  x- y * z;
                    break;
                case "-/":
                    sun =  x- y / z;
                    break;
                case "*+":
                    sun =  x* y +z;
                    break;
                case "*-":
                    sun =  x* y-z;
                    break;
                case "**":
                    sun = x*  y* z;
                    break;
                case "*/":
                    sun =  x*  y/ z;
                    break;
                case "/+":
                    sun =  x/ y+ z;
                    break;
                case "/-":
                    sun = x/  y- z;
                    break;
                case "/*":
                    sun =  x/  y*z;
                    break;
                case "//":
                    sun =  x/ y / z;
                    break;

            }

 
        }
        private void av(string va1,string va2,string va3,string va4)
        {
            label1.Text = va1;
            label5.Text = va2;
            label14.Text = va3;
            label15.Text = va4;
 
        }
       
      
    }
}

 总结

这样写感觉很轻松也很整齐,没有的代码的重复性!代码也简洁了不少,就像老师说的一样有一些代码美,虽然还不够完善!不够美!

还有就是上面的代码有一个地方没有实现,就是有无括号写的正确判断!所以也没有引用自定义的方法!

        private void zhenque()
        {
          
            if (comboBox1.Text == "前两个加括号")
            {
              
            }
            if (comboBox1.Text == "不加括号")
            {
               
            }
            if (comboBox1.Text == "后两个加括号")
            {
               
            }
        }
不知道怎么给自定义的方法传括号的参数
   private  void kuohao()
        {
            string f = label3.Text;
            String g = label6.Text;
            int x= int.Parse(label2.Text);
            int y=int.Parse(label4.Text);
            int z= int.Parse(label7.Text);
            string fg = f + g;
            switch (fg)
            {
                case "++":
                    sun =x+y+z;
                    break;
                case "+-":
                    sun = x + y - z;
                    break;
                case "+*":
                    sun = x + y * z;
                    break;
                case "+/":
                    sun = x +y / z;
                    break;
                case "-+":
                    sun = x-y +z;
                    break;
                case "--":
                    sun = x- y - z;
                    break;
                case "-*":
                    sun =  x- y * z;
                    break;
                case "-/":
                    sun =  x- y / z;
                    break;
                case "*+":
                    sun =  x* y +z;
                    break;
                case "*-":
                    sun =  x* y-z;
                    break;
                case "**":
                    sun = x*  y* z;
                    break;
                case "*/":
                    sun =  x*  y/ z;
                    break;
                case "/+":
                    sun =  x/ y+ z;
                    break;
                case "/-":
                    sun = x/  y- z;
                    break;
                case "/*":
                    sun =  x/  y*z;
                    break;
                case "//":
                    sun =  x/ y / z;
                    break;

            }

 

转载于:https://www.cnblogs.com/lizanqirxx/p/4958937.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个可能的实现。需要注意的是,这个实现只是一个简单的示例,可能还有很多需要改进的地方。 ```c++ #include <iostream> #include <fstream> #include <stack> #include <cstdlib> #include <ctime> #include <vector> using namespace std; // 定义运算符的优先级 int priority(char op) { if (op == '+' || op == '-') { return 1; } else if (op == '*' || op == '/') { return 2; } else { return 0; } } // 判断是否为操作符 bool is_operator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } // 将中缀表达式转换为后缀表达式 vector<string> infix_to_postfix(string infix) { vector<string> postfix; stack<char> op_stack; string num = ""; for (int i = 0; i < infix.length(); i++) { char c = infix[i]; if (isdigit(c)) { num += c; } else { if (!num.empty()) { postfix.push_back(num); num = ""; } if (c == '(') { op_stack.push(c); } else if (c == ')') { while (!op_stack.empty() && op_stack.top() != '(') { postfix.push_back(string(1, op_stack.top())); op_stack.pop(); } op_stack.pop(); // 弹出左括号 } else if (is_operator(c)) { while (!op_stack.empty() && priority(op_stack.top()) >= priority(c)) { postfix.push_back(string(1, op_stack.top())); op_stack.pop(); } op_stack.push(c); } } } if (!num.empty()) { postfix.push_back(num); } while (!op_stack.empty()) { postfix.push_back(string(1, op_stack.top())); op_stack.pop(); } return postfix; } // 计算后缀表达式的值 int evaluate_postfix(vector<string> postfix) { stack<int> num_stack; for (int i = 0; i < postfix.size(); i++) { string token = postfix[i]; if (isdigit(token[0])) { num_stack.push(atoi(token.c_str())); } else { int b = num_stack.top(); num_stack.pop(); int a = num_stack.top(); num_stack.pop(); int result; if (token == "+") { result = a + b; } else if (token == "-") { result = a - b; } else if (token == "*") { result = a * b; } else if (token == "/") { result = a / b; } num_stack.push(result); } } return num_stack.top(); } // 随机生成题目 string generate_question() { string question = ""; int num_operands = rand() % 3 + 2; // 随机生成操作数的个数 for (int i = 0; i < num_operands; i++) { int operand = rand() % 100 + 1; // 随机生成操作数 question += to_string(operand); if (i < num_operands - 1) { char op; int op_type = rand() % 4; // 随机生成操作符类型 if (op_type == 0) { op = '+'; } else if (op_type == 1) { op = '-'; } else if (op_type == 2) { op = '*'; } else { op = '/'; } question += op; } } // 随机添加括号 int num_parentheses = rand() % 2; for (int i = 0; i < num_parentheses; i++) { int pos = rand() % (question.length() - 2) + 1; question.insert(pos, 1, '('); int end_pos = rand() % (question.length() - pos - 1) + pos + 1; question.insert(end_pos, 1, ')'); } return question; } // 保存历史分数到文件 void save_score(int score) { ofstream fout("scores.txt", ios::app); if (fout.is_open()) { fout << score << endl; fout.close(); } else { cout << "Error: Failed to save score." << endl; } } // 从文件中读取历史分数 vector<int> load_scores() { vector<int> scores; ifstream fin("scores.txt"); if (fin.is_open()) { int score; while (fin >> score) { scores.push_back(score); } fin.close(); } else { cout << "Error: Failed to load scores." << endl; } return scores; } // 显示历史分数和评价 void show_scores(vector<int> scores, int current_score) { cout << "历史最高分: " << (scores.empty() ? 0 : *max_element(scores.begin(), scores.end())) << endl; cout << "历史平均分: " << (scores.empty() ? 0 : accumulate(scores.begin(), scores.end(), 0) / scores.size()) << endl; cout << "本次得分: " << current_score << endl; if (!scores.empty()) { int num_better = count_if(scores.begin(), scores.end(), [&current_score](int s) { return s > current_score; }); int num_worse = count_if(scores.begin(), scores.end(), [&current_score](int s) { return s < current_score; }); int num_same = scores.size() - num_better - num_worse; cout << "历史分数比较: " << num_better << " 个人比你分数高," << num_same << " 个人和你得分相同," << num_worse << " 个人比你得分低。" << endl; } } int main() { srand(time(NULL)); vector<int> scores = load_scores(); while (true) { string question = generate_question(); cout << "请计算以下表达式的结果: " << question << endl; int answer; cin >> answer; vector<string> postfix = infix_to_postfix(question); int result = evaluate_postfix(postfix); if (answer == result) { int score = question.length(); cout << "回答正确!得分为 " << score << " 分。" << endl; save_score(score); } else { cout << "回答错误!正确答案为 " << result << "。" << endl; } show_scores(scores, score); cout << "是否继续答题?(y/n) "; char choice; cin >> choice; if (choice == 'n') { break; } } return 0; } ``` 上面的代码实现了题目要求的所有功能,包括随机生成表达式、计算表达式的值、保存历史分数、回顾历史分数、比较历史分数等。在实现时,使用了STL中的stack、vector、algorithm等容器和函数,简化了代码的编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值