我创建了一个类库和一个Windows窗体应用。类库中是一个接口,接口声明了+、-、*、/运算。然后在Windows窗体应用中对接口进行实现。
1、首先布局计算器的按键布局
2、在类库的接口类中声明方法
3、双击form1窗体,进入编写代码的区域
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary1;
namespace Jisuanqi
{
public partial class Form1 : Form
{
double a, b;
bool c = false;
string d;
int length;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
//该部分注释可以实现输入一个数然后输入的运算符不会出现在textbox1,并且输入第二个数的时候是吧第一个数清空的
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
//if (c == true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "0";
}
private void button12_Click(object sender, EventArgs e)
{
c = true;
length = textBox1.Text.Length;
b = Convert.ToDouble(textBox1.Text);
d = "+";
textBox1.Text += d;
}
private void button13_Click(object sender, EventArgs e)
{
c = true;
length = textBox1.Text.Length;
b = Convert.ToDouble(textBox1.Text);
d = "-";
textBox1.Text += d;
}
private void button14_Click(object sender, EventArgs e)
{
c = true;
length = textBox1.Text.Length;
b = Convert.ToDouble(textBox1.Text);
d = "*";
textBox1.Text += d;
}
private void button15_Click(object sender, EventArgs e)
{
c = true;
length = textBox1.Text.Length;
b = Convert.ToDouble(textBox1.Text);
d = "/";
textBox1.Text += d;
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button11_Click(object sender, EventArgs e)
{
//在该事件中用到了实现接口Iachieve中的方法
switch (d)
{
case "+":
a = new Iachieve().Add(b, Convert.ToDouble(textBox1.Text.Remove(0, length + 1)));
//a = b + Convert.ToDouble(textBox1.Text.Remove(0,length+1 ));
break;
case "-":
a = new Iachieve().Sub(b, Convert.ToDouble(textBox1.Text.Remove(0, length + 1)));
//a = b - Convert.ToDouble(textBox1.Text.Remove(0, length + 1));
break;
case "*":
a = new Iachieve().Mul(b, Convert.ToDouble(textBox1.Text.Remove(0, length + 1)));
//a = b * Convert.ToDouble(textBox1.Text.Remove(0,length+1 ));
break;
case "/":
if(Convert.ToDouble(textBox1.Text.Remove(0, length + 1)) == 0)
{
textBox1.Text = "错误!";
a = 0;
break;
}
a = new Iachieve().Div(b, Convert.ToDouble(textBox1.Text.Remove(0, length + 1)));
//a = b / Convert.ToDouble(textBox1.Text.Remove(0, length + 1));
break;
}
textBox1.Text =textBox1.Text+ "\r\n"+a;
c = true;
}
private void button17_Click(object sender, EventArgs e)
{
//去掉 textBox1.Text中的最后一个字符
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
}
private void button1_Click(object sender, EventArgs e)
{
//if(c = true)
//{
// textBox1.Text = "";
// c = false;
//}
textBox1.Text += "1";
}
}
4、实现接口的类Iachieve
注意:添加的类库要想在form窗体中应用的话就要在form窗体中添加引用,并在代码区域用using它的命名空间。
结果: