一、Form界面搭建
1.我们需要选择“C#”语言,然后选择“Windows窗体应用(.NET Framework )”,这个窗体类型一定不能选错,接着就下一步创建项目。
2.我先放我搭建好的计算器界面,但%的代码没有放进来,因此这按键不可用。
二、代码展示
Peogram.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 计算器2
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormCalculator());
}
}
}
FormCalculator.cs代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 计算器2
{
public partial class FormCalculator : Form
{
#region 方法二:对表达式分析
string Precede(string p, string q)
{
switch (p)
{
case "+":
case "-":
return ("*/(".IndexOf(q) != -1) ? "<" : ">";
case "*":
case "/":
return (q == "(") ? "<" : ">";
case "(":
return (q == ")") ? "=" : "<";
case ")":
return (q == "(") ? "?" : ">";
case "#":
return (q == "#") ? "=" : "<";
}
return "?";
}
Double Operate(Double a, char o, Double b)
{
switch (o)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return 0;
}
public Object Eval_2(string Expression)
{
Stack nArr = new Stack(), oArr = new Stack();
int j = 0;
Double a = 0, b = 0;
string w = "";
char o;
MatchCollection arr = Regex.Matches(Expression.Replace(" ", "") + "#", @"(((?<=(^|\())-)?\d+(\.\d+)?|\D)");
oArr.Push('#');
w = Convert.ToString(arr[j++]);
while (!(w == "#" && Convert.ToString(oArr.Peek()) == "#"))
{
if ("+-*/()#".IndexOf(w) != -1)
{
switch (Precede(oArr.Peek().ToString(), w))
{
case "<":
oArr.Push(w);
w = Convert.ToString(arr[j++]);
break;
case "=":
oArr.Pop();
w = Convert.ToString(arr[j++]);
break;
case ">":
o = Convert.ToChar(oArr.Pop());
b = Convert.ToDouble(nArr.Pop());
a = Convert.ToDouble(nArr.Pop());
nArr.Push(Operate(a, o, b));
break;
default:
return "Error";
}
}
else
{
nArr.Push(w);
w = Convert.ToString(arr[j++]);
}
}
return nArr.Pop();
}
#endregion
public FormCalculator()
{
InitializeComponent();
this.MaximizeBox = false;//禁止最大化
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void FormCalculator_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text += "4";
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text += "6";
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text += "9";
}
private void buttonDot_Click(object sender, EventArgs e)
{
this.textBox1.Text += ".";
}
private void button0_Click(object sender, EventArgs e)
{
this.textBox1.Text += "0";
}
private void buttonDiv_Click(object sender, EventArgs e)
{
this.textBox1.Text += "/";
}
private void buttonMul_Click(object sender, EventArgs e)
{
this.textBox1.Text += "*";
}
private void buttonSub_Click(object sender, EventArgs e)
{
this.textBox1.Text += "-";
}
private void buttonAdd_Click(object sender, EventArgs e)
{
this.textBox1.Text += "+";
}
private void buttonClear_Click(object sender, EventArgs e)
{
this.textBox1.Text = " ";
}
private void buttonparentheses1_Click(object sender, EventArgs e)
{
this.textBox1.Text += "(";
}
private void buttonparentheses2_Click(object sender, EventArgs e)
{
this.textBox1.Text += ")";
}
private void buttonPer_Click(object sender, EventArgs e)
{
this.textBox1.Text += "%";
}
//计算结果
private void buttonEqual_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
double result = (double)Eval_2(str);
this.textBox1.Text = result.ToString();
}
}
}
注:本篇博文只是随意写,还有很多细节未展示,一定会报错,不适于用来参考。