zoukankan      html  css  js  c++  java
  • 四则运算

    这是我的程序页面

    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;
                }
            }
        }
    }
    
  • 相关阅读:
    python测试开发django-1.开始hello world!
    python基础--杂项
    Python基础----函数
    python介绍
    公共Webservice
    divmod(a,b)函数
    模块知识
    第三周作业 修改配置文件
    rsync在windows和linux同步数据的配置过程
    docker学习笔记
  • 原文地址:https://www.cnblogs.com/baojiao/p/5005781.html
Copyright © 2011-2022 走看看