刚开始对封装还是不熟悉,自己在电脑搞了可长时间还是不太好,又请教的别人,让人家帮忙做出来的,虽然过程很艰辛,不过自己也在进步,计算器做的还不算完美,自己在下面完善吧,加油!
一、封装:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using jisuanqi;
namespace jisuanqi
{
public abstract class Calculator //抽象类 Calculator
{
public abstract double Cal(double a, double b); //抽象方法Cal
}
public class Add : Calculator //派生类Add继承抽象类Calculator
{
public override double Cal(double a, double b)//并重写了抽象方法Cal
{
double result = 0;
result = a + b;
return result;
}
}
}
public class Sub : Calculator
{
public override double Cal(double a, double b)
{
double result = 0;
result = a - b;
return result;
}
}
public class Mul : Calculator
{
public override double Cal(double a, double b)
{
double result = 0;
result = a * b;
return result;
}
}
public class Div : Calculator
{
public override double Cal(double a, double b)
{
double result = 0;
result = a / b;
return result;
}
}
public class Every
{
private Calculator calculate = null;//实例化一个基类的引用对象
public Every(Calculator _cal)//_cal为派生类的一个对象
{
this.calculate = _cal; //把派生类的对象赋给基类的引用对象
}
public double Cal(double a, double b, String C)
{
return this.calculate.Cal(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 jisuanqi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = "E:
tf";
public static int Count = 0;
public static int zq = 0;
public static int lt;
public static int time;
public static int sum;
int i = 1;
public static String AddC = "+";
public static String SubC = "-";
public static String MulC = "*";
public static String DivC = "/";
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(path))
{
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
open.Enabled = false;
}
save.Enabled = false;
}
private void open_Click(object sender, EventArgs e) //打开文件的方法
{
OpenFileDialog TxTOpenDialog = new OpenFileDialog();
TxTOpenDialog.Filter = "(*.txt)|*.txt";
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 = "(*.txt)|*.txt";
if (File.Exists(path))
{
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
richTextBox1.Clear();
save.Enabled = false;
}
else
{
if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
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;
}
}
public void bianxie() //编写试题的方法
{
left.Text = "";
fuhao.Text = "";
right.Text = "";
}
private void open2_Click(object sender, EventArgs e) //打开试题的方法
{
OpenFileDialog TxTOpenDialog = new OpenFileDialog();
TxTOpenDialog.Filter = "(*.txt)|*.txt";
if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
{
path = TxTOpenDialog.FileName;
this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
save.Enabled = false;
open.Enabled = false;
MessageBox.Show("导入成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void daan1_Click(object sender, EventArgs e)
{
if (daan1.Text == "显示答案")
{
daan.PasswordChar = Convert.ToChar(0);
daan1.Text = "隐藏答案";
}
else if (daan1.Text == "隐藏答案")
{
daan.PasswordChar = '.';
daan1.Text = "显示答案";
}
}
private void kaishi_Click(object sender, EventArgs e)
{
string[] ll = new string[100];
ll = File.ReadAllLines("writer.txt");
textBox1.Text = ll[0];
string[] lli = new string[100];
lli = File.ReadAllLines("writer1.txt");
textBox2.Text = lli[0];
string[] llp = new string[100];
llp = File.ReadAllLines("writer2.txt");
textBox3.Text = llp[0];
int minute;
try
{
minute = int.Parse(this.shijian.Text);
}
catch (System.Exception ex)
{
this.shijian1.Text = "输入错误";
return;
}
lt = minute;
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (lt <= 0)
{
timer1.Enabled = false;
MessageBox.Show("答题时间到!");
}
this.shijian1.Text = "剩余时间" + lt.ToString() + "秒";
lt--;
}
private void button1_Click(object sender, EventArgs e) //请编辑下道题的事件
{
Count++;
ti.Text = Count.ToString();
StreamWriter writer = File.AppendText("writer.txt");
writer.WriteLine(left.Text);
writer.Close();
StreamWriter writer1 = File.AppendText("writer1.txt");
writer1.WriteLine(fuhao.Text);
writer1.Close();
StreamWriter writer2 = File.AppendText("writer2.txt");
writer2.WriteLine(right.Text);
writer2.Close();
richTextBox1.Text += left.Text + fuhao.Text + right.Text + label2.Text + "" + "
";
//string f = fuhao.Text;
//switch (f)
//{
//case "+":
//sum = int.Parse(left.Text) + int.Parse(right.Text);
//bianxie();
//break;
//case "-":
//sum = int.Parse(left.Text) - int.Parse(right.Text);
//bianxie();
//break;
//case "×":
//sum = int.Parse(left.Text) * int.Parse(right.Text);
//bianxie();
//break;
//case "÷":
//sum = int.Parse(left.Text) / int.Parse(right.Text);
//bianxie();
//break;
//default:
//break;
//}
left.Clear();
right.Clear();
}
private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
double a = double.Parse(left.Text);
string C = fuhao.Text;
double b = double.Parse(right.Text);
Every every = null;
if (C.Equals(AddC))
{
every = new Every(new Add());
}
else if (C.Equals(SubC))
{
every = new Every(new Sub());
}
else if (C.Equals(MulC))
{
every = new Every(new Mul());
}
else if (C.Equals(DivC))
{
every = new Every(new Div());
}
string answer = every.Cal(a, b, C).ToString();
if (e.KeyCode == Keys.Enter)
{
string result = left.Text + fuhao.Text + right.Text;
Count++;
if (TextBox4.Text == answer)
zhengque++;
MessageBox.Show("回答正确");
}
else
{
zhengque++;
MessageBox.Show("回答错误");
}
}
private void button2_Click(object sender, EventArgs e)
{
File.WriteAllText("writer.txt", string.Empty);
File.WriteAllText("writer1.txt", string.Empty);
File.WriteAllText("writer2.t
三、窗体显示