四则运算

 

 

这是我的程序页面

from1代码

 

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;
using System.IO;
namespace sizeyunsuan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string path = "./txt.rtf";        //保存文件路径
        public static int Count = 0;        //题目总数
        public int t = 60;                 //测试时间为60s
        public static int right = 0;       //正确题目数
        public static double result = 0; //结果


        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(path))
            {
                this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                open.Enabled = false;
            }
            save.Enabled = false;
        }
          private void timer1_Tick(object sender, EventArgs e)
        {
            if (t <= 0)
            {
                timer1.Enabled = false;
                jieguo.Enabled = false;
                MessageBox.Show("时间到!");
                jieguo.Enabled = false;
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
            t = t - 1;
            jishi.Text = t.ToString();
        }

         private void jisuan_Click(object sender, EventArgs e)     //计算按钮
        {
            jishi.Text = t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
        }

        
        private void stop_Click(object sender, EventArgs e)          //停止
        {
            timer1.Stop();
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            save.Enabled = true;
            if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
            {
                open.Enabled = true;
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            SaveFileDialog TxTSaveDialog = new SaveFileDialog();
            TxTSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
            if (File.Exists(path))
            {
                this.richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
                MessageBox.Show("保存成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.richTextBox1.Clear();
                save.Enabled = false;

            }
            else
            {
                if (TxTSaveDialog.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox1.SaveFile(TxTSaveDialog.FileName, RichTextBoxStreamType.RichText);
                    MessageBox.Show("保存成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    save.Enabled = false;

                }
            }
        }

        private void open_Click(object sender, EventArgs e)
        {
             OpenFileDialog TxTOpenDialog = new OpenFileDialog();
            TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
            if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
            {
                path = TxTOpenDialog.FileName;
                this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                save.Enabled = false;
                open.Enabled = false;
                MessageBox.Show("读取成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void jieguo_KeyDown(object sender, KeyEventArgs e)
        {

            Fengzhuang fen = new Fengzhuang();
            fen.F= double.Parse(txt1.Text);
            fen.S = double.Parse(txt2.Text);
            fen.fuhao = (comboBox1.SelectedItem).ToString();
            fen.result = result;


            fen.jia();
            fen.jian();
            fen.cheng();
            fen.chu();

            if (e.KeyCode == Keys.Enter)//判断计算情况
            {
                if (jieguo.Text == fen.result.ToString())
                {
                    right++;

                    MessageBox.Show("回答正确!");
                    richTextBox1.Text += txt1.Text + comboBox1.SelectedItem + txt2.Text + label2.Text + jieguo.Text;
                }
                else
                {
                    MessageBox.Show("回答错误!");
                }
                Count++;
                txt1.Clear();
                txt2.Clear();
                jieguo.Clear();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int fuhao = comboBox1.SelectedIndex;
            switch (fuhao)
            {
                case 0:
                    comboBox1.SelectedItem = "+";
                    break;
                case 1:
                    comboBox1.SelectedItem = "-";
                    break;
                case 2:
                    comboBox1.SelectedItem = "*";
                    break;
                case 3:
                    comboBox1.SelectedItem = "/";
                    break;
                default:
                    break;
            }


        }

    }

}

 

 from2代码

 

using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;

namespace sizeyunsuan
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            zongji.Text = Form1.Count.ToString();
            zhengque.Text = Form1.right.ToString();
            zhengquelv.Text = (Form1.Count - Form1.right).ToString();
        }
    }
}

 

 封装代码

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sizeyunsuan
{
    public class Fengzhuang
    {
        private double s;

        public double S
        {
            get { return s; }
            set { s = value; }
        }
        private double f;

        public double F
        {
            get { return f; }
            set { f = value; }
        }
        public double result;
        public string fuhao;
        public void jia()
        {
            if (fuhao=="+")
            {
                result = F + S;
            }
        }
        public void jian()
        {
            if (fuhao == "-")
            {
                result = F - S ;
            }
        }
        public void cheng()
        {
            if (fuhao == "*")
            {
                result = F * S;
            }
        }
        public void chu()
        {
            if (fuhao == "/")
            {
                result = F / S;
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/baojiao/p/5005781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值