四则运算的封装
2015-11-29 16:17
134 查看
封装:
Form1:
Form2:
测试结果:
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(); } } }
测试结果:
相关文章推荐
- SharedPreference用法
- 计算器之策略模式
- Using Process Memory Matrix script for understanding Oracle process memory usage
- Android开发:一个SharedPreference的工具类,方便保存和读取数据
- 理解OC“属性”这一概念
- CodeForces 120F|Spiders|树形DP|求树最长链
- Android studio修改变更Eclipse快捷键
- DWZ学习总结《一》
- Java的接口问题
- 15泛型_15.1与C++比较
- 开源工程系列之INA219功率计
- 内存溢出和内存泄漏
- GHOST 博客安装中文全攻略
- sun关于tmp文件的描述
- Android IPC方式
- iOS中的文件I/O
- IOS 手势之左右滑动UISwipeGestureRecognizer
- UML——包图
- c++运算符重载
- python 过滤出某后缀名文件