封装、策略
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 算 { public interface Isuan { int calculate(int a, int b); } public class add : Isuan { public int calculate(int a,int b) { return a + b; } } public class sub : Isuan { public int calculate(int a, int b) { return a - b; } } public class mal : Isuan { public int calculate(int a, int b) { return a * b; } } public class div : Isuan { public int calculate(int a, int b) { return a / b; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace 算 { class fuhao { private Isuan fh; public fuhao(string a) { switch(a) { case"+": fh = new add(); break; case "-": fh = new sub(); break; case "*": fh = new mal(); break; case "/": fh = new div(); break; } } public int jisuan(int a, int b) { return fh.calculate(a, b); } } }
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(); } private int An; private void button2_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { fuhao clacuter = new fuhao(comboBox1.Text); An = clacuter.jisuan(int.Parse(textBox1.Text), int.Parse(textBox2.Text)); if (textBox3.Text == An.ToString()) { MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); } textBox1.Clear(); textBox2.Clear(); comboBox1.Text = ""; textBox3.Clear(); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { fuhao clacuter = new fuhao(comboBox1.Text); An = clacuter.jisuan(int.Parse(textBox1.Text), int.Parse(textBox2.Text)); if (textBox3.Text == An.ToString()) { MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); } textBox1.Clear(); textBox2.Clear(); comboBox1.Text = ""; textBox3.Clear(); } } } }
asp换脸
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///fuhao 的摘要说明 /// </summary> public class fuhao { private Isuan s; public fuhao(string a) { switch (a) { case"+": s = new add(); break; case "-": s = new sub(); break; case "*": s = new cml(); break; case "/": s = new div(); break; } } public int suan(int a, int b) { return s.cla(a,b); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///suan 的摘要说明 /// </summary> public interface Isuan { int cla(int a, int b); } public class add:Isuan { public int cla(int a, int b) { return a + b; } } public class sub:Isuan { public int cla(int a, int b) { return a - b; } } public class cml:Isuan { public int cla(int a, int b) { return a * b; } } public class div:Isuan { public int cla(int a, int b) { return a / b; } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { private int An; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { fuhao b = new fuhao(TextBox2.Text); An=b.suan(int.Parse(TextBox1.Text),int.Parse(TextBox3.Text)); if (TextBox4.Text == An.ToString()) { Response.Write("回答正确!"); } else { Response.Write("回答错误!"); } } }
时间: 2024-10-12 02:20:47