四个整数运算
1.计算类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 封装1 { public class Calcuater { private int numberA; private int numberB; public int NumberA { get { return numberA; } set {numberA=value;} } public int NumberB { get { return numberB; } set { numberB = value; } } public virtual int getresult() { int result=0; return result; } } public class Add : Calcuater { public override int getresult() { int result = 0; result = NumberA + NumberB; return result; } } public class Sub : Calcuater { public override int getresult() { int result = 0; result = NumberA - NumberB; return result; } } public class Mul : Calcuater { public override int getresult() { int result = 0; result = NumberA * NumberB; return result; } } public class Div : Calcuater { public override int getresult() { int result = 0; if (NumberB == 0) { throw new Exception("除数不能为零!"); } else { result = NumberA / NumberB; } return result; } } }
2.定义环境
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 封装1 { class factory { public static Calcuater create(string operte) { Calcuater oper=null; switch (operte) { case "+": oper = new Add(); break; case "-": oper = new Sub(); break; case "*": oper = new Mul(); break; case "/": oper = new Div(); break; } return oper; } } }
3.form设计
4.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; namespace 封装1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int rest; private void Form1_Load(object sender, EventArgs e) { chuti(); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { int a =int.Parse( textBox1.Text); int b = int.Parse(textBox2.Text); int c = int.Parse(textBox4.Text); int d = int.Parse(textBox5.Text); string f = label1.Text; string h = label2.Text; string o = label3.Text; if (e.KeyCode == Keys.Enter) { if ( h=="/"||h=="*") { Calcuater calas = factory.create(h); calas.NumberA = b; calas.NumberB = c; int re = calas.getresult(); if (f == "*" || f == "/") { calas = factory.create(f); calas.NumberA = a; calas.NumberB = re; int re1 = calas.getresult(); calas = factory.create(o); calas.NumberA = re1; calas.NumberB = d; rest = calas.getresult(); } if (o == "*" || o == "/") { calas = factory.create(o); calas.NumberA = d; calas.NumberB = re; int re1 = calas.getresult(); calas = factory.create(f); calas.NumberA = re1; calas.NumberB = a; rest = calas.getresult(); } } if ( h == "+" || h == "-") { Calcuater calas = factory.create(f); calas.NumberA = a; calas.NumberB = b; int re = calas.getresult(); calas = factory.create(o); calas.NumberA = c; calas.NumberB = d; int re1 = calas.getresult(); calas = factory.create(h); calas.NumberA = re; calas.NumberB = re1; rest = calas.getresult(); } if (textBox3.Text == rest.ToString()) { MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); MessageBox.Show(rest.ToString()); } textBox3.Clear(); chuti(); } } private void chuti() { Random random = new Random(); textBox1.Text = random.Next(0, 100).ToString(); textBox2.Text = random.Next(0, 100).ToString(); textBox4.Text = random.Next(0, 100).ToString(); textBox5.Text = random.Next(0, 100).ToString(); string[] operte = new string[] { "+", "-", "*", "/" }; label1.Text = operte[new Random().Next(0, 4)]; string[] operte1 = new string[] { "-", "*", "+", "/" }; label2.Text = operte1[new Random().Next(0, 4)]; string[] operte2 = new string[] { "*", "-", "/", "+" }; label3.Text = operte2[new Random().Next(0, 4)]; } } }
5.运行结果
6.总的来说还是有些问题遇到不能整除的书会默认取模。导致在除数运算时可能会出现除以零的情况。