四则运算的封装

封装:

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

namespace 软件工程作业
{
    class Enclosure
    {
        private double x;//第一个数

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        private double y;//第二个数

        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public double result;//计算结果
        public string opera = "";//计算符号
        public void Add()//加法
        {
            if (opera=="+")
            {
                result = X + Y;

            }
        }
        public void Sub()
        {
            if (opera == "-")
            {
                result = X - Y;

            }
        }
        public void Mul()
        {
            if (opera == "*")
            {
                result = X * Y;

            }

        }
        public void Div()
        {
            if (opera == "/")
            {
                result = X / Y;

            }
        }
    }
}

Form1:

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 软件工程作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string path = "./test1.rtf";//保存文件路径
        public static int Count = 0;//题目总数
        public int a = 60;//测试时间为60s
        public static int right = 0;//正确题目数
        public static double result = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = a.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                timer1.Enabled = false;
                textBox3.Enabled = false;
                MessageBox.Show("时间到!");
                textBox3.Enabled = false;
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
            a = a - 1;
            label4.Text = a.ToString();
        }

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

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            Enclosure enc = new Enclosure();//实例化一个对象
            enc.X = double.Parse(textBox1.Text);//第一个数
            enc.Y = double.Parse(textBox2.Text);//第二个数
            enc.opera = label3.Text;//运算符号
            enc.result = result;//结果
            enc.Add();
            enc.Sub();
            enc.Mul();
            enc.Div();

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

                    MessageBox.Show("回答正确!");
                    richTextBox1.Text += textBox1.Text + label3.Text + textBox2.Text + label5.Text + textBox3.Text;
                }
                else
                {
                    MessageBox.Show("回答错误!");
                }
                Count++;
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            label3.Text = "+";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label3.Text = "-";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label3.Text = "*";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label3.Text = "/";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(path))
            {
                this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                Open.Enabled = false;
            }
            Save.Enabled = false;

        }
        //保存,打开, richTextBox1
        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 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 richTextBox1_TextChanged(object sender, EventArgs e)
        {
            Save.Enabled = true;
            if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
            {
                Open.Enabled = true;
            }
        }
    }

}

Form2:

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

namespace 软件工程作业
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.Count.ToString();
            textBox2.Text = Form1.right.ToString();
            textBox3.Text = (Form1.Count - Form1.right).ToString();
        }
    }
}

测试结果:

时间: 2024-08-06 07:57:57

四则运算的封装的相关文章

作业五 关于封装与测试 拿我小学生四则运算为例

我的封装: 1 class yy 2 { 3 private int max=4; 4 private int min=1; 5 private double w2,m2; 6 private String ff; 7 Random random = new Random(); 8 9 //System.out.println(s); 10 public yy() 11 { 12 int s = random.nextInt(max)%(max-min+1) + min; 13 14 doubl

作业5 四则运算 测试与封装 5.2

作业5 四则运算 测试与封装  5.2 开发环境:   Eclipse 开发人员:   欧其锋(201306114305) 余汉城(201306114317)(http://www.cnblogs.com/yuhancheng/) 分工:   欧其锋:异常处理 余汉城:重构 源代码: 1 package GongNengpk; 2 3 import GongNengpkTest.ChuShuLingException; 4 import GongNengpkTest.JCException; 5

四则运算测试与封装5.1 结对

开发环境:eclipse 结对同伴 :罗凯杰 同伴博客:http://www.cnblogs.com/lkjie/ 未封装的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Size { public static void main(String[] args) { // TODO Auto-generated method stub myframe f=new myframe(

20150421 作业5 测试与封装 四则运算

大家写了不少四则运算的练习,这些代码都各有特色,大家写的 “软件” 也有一定的用处. 如果我们要把这个功能放到不同的环境中去 (例如,命令行,windows 图形界面程序,网页程序,手机App), 就会碰到困难, 因为目前代码的普遍问题是代码都散落在main() 函数或者其他子函数中,我们很难把这些功能完整地剥离出来,作为一个独立的模块满足不同的需求. 我们看到,不同的代码解决不同层面的问题,有些是内部数据的计算 (例如四则运算):有些是和用户输入相关的 (例如 scanf,cin,图形界面的输

四则运算,测试与封装。

测试与封装 5.1 程序开发简介: [开发环境]:eclipse [开发人员]:Ives & 郑胜斌 [博客地址]:http://www.cnblogs.com/IvesHe/ [开发时间]:2015-04-30 [版本]:5.1 [要求]: 封装 测试 [分工]: Ives:单元测试.界面.自定义异常. 郑胜斌:封装 Expression类. 封装: 概念 封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口.面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的对

四则运算封装

form1 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 四则运算 { public partial class Form1

四则运算《《《代码封装

设计思路: 因为TabControl可以设置不同页的选项卡.所以我用它来分页,进行出题,答题设置.然后用savefiledialog保存所出题目.设置两个RichTextBox保存所出题目和出好题后做题时显示的题目.用Count计算做题总数,Right计算做正确的数目.点击结束时弹出Form对话框显示做题情况. 具体实现代码: Form1.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Component

作业5 四则运算 测试与封装 5.1

刘恒 http://www.cnblogs.com/iliuh/ chaorenken http://www.cnblogs.com/lixuanzong/ 这个是主函数 package tion; import java.util.*; import java.applet.*; //引入Applet类 import java.awt.*; import java.awt.event.*; //引入系统事件类包. public class ta extends Applet implement

四则运算《《《代码封装 策略模式

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace sum 7 { 8 class Class1 9 { 10 public interface Calculator //声明计算接口 11 { 12 double Cal(double x, double y); 13 } 14 private double x; //定义x变量: