计算器简单封装和ASP.net
封装:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 计算器 { class Richnone { public string fuhao;//计算符号 public double result;//计算结果 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 void Add()//加法 { if (fuhao=="+") { result = X + Y; } } public void Sub()//减法 { if (fuhao == "-") { result = X - Y; } } public void Mul()//乘法 { if (fuhao == "*") { result = X * Y; } } public void Div()//除法 { if (fuhao == "/") { result = X / Y; } } } }
Form1代码
private void Form1_Load(object sender, EventArgs e) { if (File.Exists(path)) { this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText); open.Enabled = true; } save.Enabled = true; } 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); open.Enabled = true; save.Enabled = true; MessageBox.Show(" 读取成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.richTextBox1.Show(); } } private void save_Click(object sender, EventArgs e) { SaveFileDialog TxTSaveDialog = new SaveFileDialog(); TxTSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF"; richTextBox1.Text = textBox2.Text; 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 add_Click(object sender, EventArgs e) { SUAN.Text = "+"; } private void sub_Click(object sender, EventArgs e) { SUAN.Text = "-"; } private void mul_Click(object sender, EventArgs e) { SUAN.Text = "*"; } private void div_Click_1(object sender, EventArgs e) { SUAN.Text = "/"; } //实例对象 Richnone ric = new Richnone(); ric.X = double .Parse( richTextBox1.Text);//第一个数 ric.Y = double.Parse(textBox2.Text);//第二个数 ric.fuhao = SUAN.Text;//运算符号 ric.result = result;//结果 ric.Add();//加法 ric.Sub();//减法 ric.Mul();//乘法 ric.Div();//除法 //int sum; //sum = int.Parse(richTextBox1.Text) + int.Parse(textBox2.Text); if (e.KeyCode == Keys.Enter) { if (textBox5.Text == ric.result.ToString()) { right++; MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); } Count++; richTextBox1.Clear(); textBox2.Clear(); textBox5.Clear(); } }截图
ASP.net
后台代码
protected void Button1_Click(object sender, EventArgs e) { ric.X =double .Parse( TextBox1.Text); ric.Y =double .Parse( TextBox3.Text); ric.fuhao = DropDownList1.SelectedValue; ric.result = result; ric.Add(); ric.Sub(); ric.Mul(); ric.Div(); if (TextBox4.Text == ric.result.ToString()) { Response.Write("<script>alert('回答正确')</script>"); } else { Response.Write("<script>alert('回答错误')</script>"); } } protected void DropDownList1_TextChanged(object sender, EventArgs e) { string fuhao = DropDownList1.SelectedValue; switch (fuhao) { case"+": DropDownList1.SelectedValue = "+"; break; case"-": DropDownList1.SelectedValue = "-"; break; case"*": DropDownList1.SelectedValue = "*"; break; case"/": DropDownList1.SelectedValue = "/"; break; default: break; } }
封装
public class Richnone { public string fuhao;//计算符号 public double result; 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 void Add()//加法 { if (fuhao == "+") { result = X + Y; } } public void Sub() { if (fuhao == "-") { result = X - Y; } } public void Mul() { if (fuhao == "*") { result = X * Y; } } public void Div() { if (fuhao == "/") { result = X / Y; } } }
截图