以下内容可且仅可供参考,如有错误欢迎指正。
部分内容借鉴自百度
侵删致歉
位切换键盘的实现用了复杂的拖64给label的方法,此功能可以在自己计算机上的计算器里找到。
目录
一、设计简介
1.设计背景
2.开发工具及环境
二、需求分析
1.设计功能要求
2.其他要求
三、相关工作
1.设计基础
2.功能需求
四、设计原理
详见代码
五、源代码
1.界面
2. Form1.cs
3.Form2.cs
3.Program.cs
伴随着日益升高的物质生活水平以及日常生活中对基本的计算需求的提高。如今,网络快速发展,对算数要求的需求日益增大,计算的效率和性能和人们的生活紧密相关。对此,使用c#语言开发出一个为人们生活带来便利的计算器,以期带来对数据计算的提高。该系统实现了对数的加、减、乘、除以及专业模式的运算。
1) 开发工具及介绍
Microsoft Visual Studio(简称VS)。VS是美国微软公司的开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台,包括Microsoft Windows、Windows Mobile、Windows CE、.NET Framework、.Net Core、.NET Compact Framework和Microsoft Silverlight 及Windows Phone。
2) 开发环境
window10系统、window11系统、Microsoft Visual Studio2019、2022。
1)实现加、减、乘、除、求开方、求倒数等。
2)实现进制之间的转换功能:十进制、二进制、十六进制、八进制。
1)界面美观,功能丰富
2)开放平台:vs2010及以上
使用Windows窗体应用程序,设计出美观丰富的用户界面(UI)以及相关代码编写设计程序而集成的一个简易的计算器,通过Windows窗体应用程序来添加控件,来组成所需要的运算和数字来达到简易模式以及专业模式。
图形化用户界面,设计的简易计算器,用户通过button按钮选择用户想要的算法、数字以及运算符号,进行简易模式以及专业模式。具体如下:
1)当进行简易模式的时候
2)当进行专业模式的时候
Form1.cs[设计]
Form2.cs[设计]
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.Text.RegularExpressions;
using System.IO;
namespace _1._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static List temp = new List();
private static long tempx = 0;
private void Add(object sender, EventArgs e)
{ //数字和符号的Click事件
Button button = (Button)sender;
textBox1.Text += button.Text;
}
private void Reset(object sender, EventArgs e)
{ // CE的Click事件
textBox1.Text = "";
}
private void Delete(object sender, EventArgs e)
{ // ←的Click事件
//移除最后一个字符
if (textBox1.TextLength > 0)
textBox1.Text = textBox1.Text.Remove(textBox1.TextLength - 1);
}
private void Equal(object sender, EventArgs e)
{ // = 的Click事件,计算并显示
//自动清除历史文件夹
Cleanf();
List zhonglist = ToList(textBox1.Text);
List houlist = houzhui(zhonglist);
List s = zhonglist;
s.Add("=");
s.Add(Calculate(houlist));
string s1 = string.Join("", s);
if (temp.Count > 9)//最多存十条记录
{
temp.RemoveAt(0);
}
temp.Add(s1);
textBox2.Text = string.Join(",",temp);
textBox1.Text = Calculate(houlist);
}
//中缀转List
static List ToList(string s)
{
List ls = new List();//存放中缀表达式
String str = ""; //拼接多位数、小数
char c = ' '; //遍历输入的字符
int i = 0;
do
{
if((s[i] == '-' && i == 0) || (i > 0 && s[i] == '-' && !char.IsDigit(s[i - 1])))//在负数前加零方便后续计算
{
ls.Add("0");
}
if (!char.IsDigit(c = s[i])&&c != '.') //如果c不是数字和'.',就加入ls
{
ls.Add(c.ToString());
i++; //i后移
}
else//如果是数字,需要考虑多位数
{
str = ""; //先将str清空
while (i < s.Length && (char.IsDigit(c = s[i]) || c == '.'))
{
str += c;
i++;
}
ls.Add(str);
}
} while (i < s.Length);
return ls;
}
//中缀改后缀
static List houzhui(List ls)
{
//定义两个栈,一个存临时结果,一个存运算符
Stack operators = new Stack();
List num = new List();
//遍历 ls
foreach (var item in ls)
{
Regex regex = new Regex("[+-]?\\d+.?\\d*");//整数和浮点数
//如果是一个数,则加入num
if (regex.Match(item).Length > 0)
{
num.Add(item);
}
else if (item.Equals("("))//Equals:比较是否相同
{
operators.Push(item);
}
else if (item.Equals(")"))
{
//如果是右括号,则依次弹出operators栈顶的运算符,并压入num,直到遇到左括号为止,并将这对括号丢弃
while (!operators.Peek().Equals("("))//Peek()返回顶部对象
{
num.Add(operators.Pop());
}
operators.Pop(); //将 ( 弹出
}
else
{
//当item 的 优先级小于或等于operators栈顶运算符,将operators栈顶的运算符弹出并加入到num中,再次转到operators与operators中新的栈顶运算符比较
while (operators.Count != 0 && compare(operators.Peek()) >= compare(item))
{
num.Add(operators.Pop());
}
//还需要将 item 压入栈
operators.Push(item);
}
}
//将operators中剩余的运算符依次弹出并加入num
while (operators.Count != 0)
{
num.Add(operators.Pop());
}
return num; //按顺序输出就是对应的后缀表达式对应的List
}
//运算符优先级
public static int compare(string operation)
{
int res = 0;
switch (operation)
{
case "+":
res = 1;
break;
case "-":
res = 1;
break;
case "*":
res = 2;
break;
case "/":
res = 2;
break;
case "%":
res = 3;
break;
case "^":
res = 4;
break;
case "!":
res = 5;
break;
default:
Console.WriteLine("不存在该运算符");
break;
}
return res;
}
//计算后缀表达式
static string Calculate(List ls)
{
Regex regex = new Regex("[+-]?\\d+.?\\d*");
//创建栈
Stack stack = new Stack();
//遍历 list
foreach (var item in ls)
{
//使用正则表达式来取出数
if (regex.Match(item).Length > 0) //如果是数字,就压入栈中
{
stack.Push(regex.Match(item).Value);
}
else if (item.Equals("!"))//弹出一个数字进行计算
{
int num1 = int.Parse(stack.Pop());
int res = leicheng(num1);
stack.Push(res.ToString());
}
else //不是数就弹出两个数字进行计算
{
double num2 = double.Parse(stack.Pop());
if (stack.Count == 0)
{
stack.Push("0");
}
double num1 = double.Parse(stack.Pop());
double res = 0;
if (item.Equals("+"))
{
res = num1 + num2;
}
else if (item.Equals("-"))
{
res = num1 - num2;
}
else if (item.Equals("*"))
{
res = num1 * num2;
}
else if (item.Equals("÷"))
{
res = num1 / num2;
}
else if (item.Equals("%"))
{
res = num1 % num2;
}
else if (item.Equals("^"))
{
res = Math.Pow(num1, num2);
}
else
{
throw new Exception("表达式有误");
}
stack.Push(res.ToString());
}
}
//最后留在stack中的数据就是运算结果
return stack.Pop();
}
//累乘
private static int leicheng(int n)
{
if(n == 1)
{
return n;
}
else
{
return n * leicheng(n - 1);
}
}
//进制转换
//二进制的前缀为 0b
//八进制的前缀为 0
//十六进制的前缀为 0x
private void jinzhi2(object sender, EventArgs e)
{
String s = textBox1.Text;
switch(s[0])
{
case '0':
switch (s[1])
{
case 'x'://十六转二
s = s.Substring(2);//去掉前缀
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 16), 2);//先转十再转二
break;
default://八转二
s = s.Substring(1);
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 8), 2);
break;
}
break;
default://十转二
textBox1.Text = Convert.ToString(Convert.ToInt32(s), 2);
break;
}
}
private void jinzhi8(object sender, EventArgs e)
{
String s = textBox1.Text;
switch (s[0])
{
case '0':
switch (s[1])
{
case 'x'://十六转八
s = s.Substring(2);//去掉前缀
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 16), 8);//先转十再转八
break;
case 'b'://二转八
s = s.Substring(2);
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 2), 8);
break;
}
break;
default://十转八
textBox1.Text = Convert.ToString(Convert.ToInt32(s), 8);
break;
}
}
private void jinzhi10(object sender, EventArgs e)
{
String s = textBox1.Text;
switch (s[1])
{
case 'x'://十六转十
s = s.Substring(2);//去掉前缀
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 16));
break;
case 'b'://二转十
s = s.Substring(2);
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 2));
break;
default://八转十
s = s.Substring(1);
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 8));
break;
}
}
private void jinzhi16(object sender, EventArgs e)
{
String s = textBox1.Text;
switch (s[0])
{
case '0':
switch (s[1])
{
case 'b'://二转十六
s = s.Substring(2);//去掉前缀
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 2), 16); ;//先转十再转十六
break;
default://八转十六
s = s.Substring(1);
textBox1.Text = Convert.ToString(Convert.ToInt32(s, 8), 16);
break;
}
break;
default://十转十六
textBox1.Text = Convert.ToString(Convert.ToInt32(s), 16);
break;
}
}
private void Reset2(object sender, EventArgs e)
{ // 清除历史记录的Click事件
textBox2.Text = "";
temp.Clear();
Cleanf();
}
//清空历史记录文件
private void Cleanf()
{
File.Delete("历史记录1.txt");
FileStream f = File.Create("历史记录1.txt");
f.Close();
}
//读取文件到历史记录
private void Readf(object sender, EventArgs e)
{ //读取历史记录的Click事件
StreamReader sr = new StreamReader("历史记录1.txt");
string s;
if((s = sr.ReadLine()) != null)
{
String[] s1 = s.Split(',');
List s2 = new List(s1);
temp = s2;
textBox2.Text = string.Join(",", temp);
}
sr.Close();
}
//打开文件,
private void Openf(object sender, EventArgs e)
{ //保存历史记录的Click事件
StreamWriter sw = new StreamWriter("历史记录1.txt");
sw.WriteLine(string.Join(",", temp));
sw.Close();
}
//以下是位切换键盘设计代码,不会高级方法,拖了64个label控制0和1
private void jisuan(string s,int n)
{
long t = 1;
for(int i = 0;i < n - 1;i++)
{
t *= 2;
}
if(s == "0")
{
tempx -= t;
}
else
{
tempx += t;
}
label72.Text = Convert.ToString(tempx , 8);
label73.Text = Convert.ToString(tempx);
label74.Text = Convert.ToString(tempx, 16);
}
private void ex1(object sender, EventArgs e)
{
if(label1.Text == "0")
{
label1.Text = "1";
}
else
{
label1.Text = "0";
}
jisuan(label1.Text,1);
}
private void ex2(object sender, EventArgs e)
{
if (label2.Text == "0")
{
label2.Text = "1";
}
else
{
label2.Text = "0";
}
jisuan(label2.Text, 2);
}
private void ex3(object sender, EventArgs e)
{
if (label3.Text == "0")
{
label3.Text = "1";
}
else
{
label3.Text = "0";
}
jisuan(label3.Text, 3);
}
private void ex4(object sender, EventArgs e)
{
if (label4.Text == "0")
{
label4.Text = "1";
}
else
{
label4.Text = "0";
}
jisuan(label4.Text, 4);
}
private void ex5(object sender, EventArgs e)
{
if (label5.Text == "0")
{
label5.Text = "1";
}
else
{
label5.Text = "0";
}
jisuan(label5.Text, 5);
}
private void ex6(object sender, EventArgs e)
{
if (label6.Text == "0")
{
label6.Text = "1";
}
else
{
label6.Text = "0";
}
jisuan(label6.Text, 6);
}
private void ex7(object sender, EventArgs e)
{
if (label7.Text == "0")
{
label7.Text = "1";
}
else
{
label7.Text = "0";
}
jisuan(label7.Text, 7);
}
private void ex8(object sender, EventArgs e)
{
if (label8.Text == "0")
{
label8.Text = "1";
}
else
{
label8.Text = "0";
}
jisuan(label8.Text, 8);
}
private void ex9(object sender, EventArgs e)
{
if (label9.Text == "0")
{
label9.Text = "1";
}
else
{
label9.Text = "0";
}
jisuan(label9.Text, 9);
}
private void ex10(object sender, EventArgs e)
{
if (label10.Text == "0")
{
label10.Text = "1";
}
else
{
label10.Text = "0";
}
jisuan(label10.Text, 10);
}
private void ex11(object sender, EventArgs e)
{
if (label11.Text == "0")
{
label11.Text = "1";
}
else
{
label11.Text = "0";
}
jisuan(label11.Text, 11);
}
private void ex12(object sender, EventArgs e)
{
if (label12.Text == "0")
{
label12.Text = "1";
}
else
{
label12.Text = "0";
}
jisuan(label12.Text, 12);
}
private void ex13(object sender, EventArgs e)
{
if (label13.Text == "0")
{
label13.Text = "1";
}
else
{
label13.Text = "0";
}
jisuan(label13.Text, 13);
}
private void ex14(object sender, EventArgs e)
{
if (label14.Text == "0")
{
label14.Text = "1";
}
else
{
label14.Text = "0";
}
jisuan(label14.Text, 14);
}
private void ex15(object sender, EventArgs e)
{
if (label15.Text == "0")
{
label15.Text = "1";
}
else
{
label15.Text = "0";
}
jisuan(label15.Text, 15);
}
private void ex16(object sender, EventArgs e)
{
if (label16.Text == "0")
{
label16.Text = "1";
}
else
{
label16.Text = "0";
}
jisuan(label16.Text, 16);
}
private void ex17(object sender, EventArgs e)
{
if (label17.Text == "0")
{
label17.Text = "1";
}
else
{
label17.Text = "0";
}
jisuan(label17.Text, 17);
}
private void ex18(object sender, EventArgs e)
{
if (label18.Text == "0")
{
label18.Text = "1";
}
else
{
label18.Text = "0";
}
jisuan(label18.Text, 18);
}
private void ex19(object sender, EventArgs e)
{
if (label19.Text == "0")
{
label19.Text = "1";
}
else
{
label19.Text = "0";
}
jisuan(label19.Text, 19);
}
private void ex20(object sender, EventArgs e)
{
if (label20.Text == "0")
{
label20.Text = "1";
}
else
{
label20.Text = "0";
}
jisuan(label20.Text, 20);
}
private void ex21(object sender, EventArgs e)
{
if (label21.Text == "0")
{
label21.Text = "1";
}
else
{
label21.Text = "0";
}
jisuan(label21.Text, 21);
}
private void ex22(object sender, EventArgs e)
{
if (label22.Text == "0")
{
label22.Text = "1";
}
else
{
label22.Text = "0";
}
jisuan(label22.Text, 22);
}
private void ex23(object sender, EventArgs e)
{
if (label23.Text == "0")
{
label23.Text = "1";
}
else
{
label23.Text = "0";
}
jisuan(label23.Text, 23);
}
private void ex24(object sender, EventArgs e)
{
if (label24.Text == "0")
{
label24.Text = "1";
}
else
{
label24.Text = "0";
}
jisuan(label24.Text, 24);
}
private void ex25(object sender, EventArgs e)
{
if (label25.Text == "0")
{
label25.Text = "1";
}
else
{
label25.Text = "0";
}
jisuan(label25.Text, 25);
}
private void ex26(object sender, EventArgs e)
{
if (label26.Text == "0")
{
label26.Text = "1";
}
else
{
label26.Text = "0";
}
jisuan(label26.Text, 26);
}
private void ex27(object sender, EventArgs e)
{
if (label27.Text == "0")
{
label27.Text = "1";
}
else
{
label27.Text = "0";
}
jisuan(label27.Text, 27);
}
private void ex28(object sender, EventArgs e)
{
if (label28.Text == "0")
{
label28.Text = "1";
}
else
{
label28.Text = "0";
}
jisuan(label28.Text, 28);
}
private void ex29(object sender, EventArgs e)
{
if (label29.Text == "0")
{
label29.Text = "1";
}
else
{
label29.Text = "0";
}
jisuan(label29.Text, 29);
}
private void ex30(object sender, EventArgs e)
{
if (label30.Text == "0")
{
label30.Text = "1";
}
else
{
label30.Text = "0";
}
jisuan(label30.Text, 30);
}
private void ex31(object sender, EventArgs e)
{
if (label31.Text == "0")
{
label31.Text = "1";
}
else
{
label31.Text = "0";
}
jisuan(label31.Text, 31);
}
private void ex32(object sender, EventArgs e)
{
if (label32.Text == "0")
{
label32.Text = "1";
}
else
{
label32.Text = "0";
}
jisuan(label32.Text, 32);
}
private void ex33(object sender, EventArgs e)
{
if (label33.Text == "0")
{
label33.Text = "1";
}
else
{
label33.Text = "0";
}
jisuan(label33.Text, 33);
}
private void ex34(object sender, EventArgs e)
{
if (label34.Text == "0")
{
label34.Text = "1";
}
else
{
label34.Text = "0";
}
jisuan(label34.Text, 34);
}
private void ex35(object sender, EventArgs e)
{
if (label35.Text == "0")
{
label35.Text = "1";
}
else
{
label35.Text = "0";
}
jisuan(label35.Text, 35);
}
private void ex36(object sender, EventArgs e)
{
if (label36.Text == "0")
{
label36.Text = "1";
}
else
{
label36.Text = "0";
}
jisuan(label36.Text, 36);
}
private void ex37(object sender, EventArgs e)
{
if (label37.Text == "0")
{
label37.Text = "1";
}
else
{
label37.Text = "0";
}
jisuan(label37.Text, 37);
}
private void ex38(object sender, EventArgs e)
{
if (label38.Text == "0")
{
label38.Text = "1";
}
else
{
label38.Text = "0";
}
jisuan(label38.Text, 38);
}
private void ex39(object sender, EventArgs e)
{
if (label39.Text == "0")
{
label39.Text = "1";
}
else
{
label39.Text = "0";
}
jisuan(label39.Text, 39);
}
private void ex40(object sender, EventArgs e)
{
if (label40.Text == "0")
{
label40.Text = "1";
}
else
{
label40.Text = "0";
}
jisuan(label40.Text, 40);
}
private void ex41(object sender, EventArgs e)
{
if (label41.Text == "0")
{
label41.Text = "1";
}
else
{
label41.Text = "0";
}
jisuan(label41.Text, 41);
}
private void ex42(object sender, EventArgs e)
{
if (label42.Text == "0")
{
label42.Text = "1";
}
else
{
label42.Text = "0";
}
jisuan(label42.Text, 42);
}
private void ex43(object sender, EventArgs e)
{
if (label43.Text == "0")
{
label43.Text = "1";
}
else
{
label43.Text = "0";
}
jisuan(label43.Text, 43);
}
private void ex44(object sender, EventArgs e)
{
if (label44.Text == "0")
{
label44.Text = "1";
}
else
{
label44.Text = "0";
}
jisuan(label44.Text, 44);
}
private void ex45(object sender, EventArgs e)
{
if (label45.Text == "0")
{
label45.Text = "1";
}
else
{
label45.Text = "0";
}
jisuan(label45.Text, 45);
}
private void ex46(object sender, EventArgs e)
{
if (label46.Text == "0")
{
label46.Text = "1";
}
else
{
label46.Text = "0";
}
jisuan(label46.Text, 46);
}
private void ex47(object sender, EventArgs e)
{
if (label47.Text == "0")
{
label47.Text = "1";
}
else
{
label47.Text = "0";
}
jisuan(label47.Text, 47);
}
private void ex48(object sender, EventArgs e)
{
if (label48.Text == "0")
{
label48.Text = "1";
}
else
{
label48.Text = "0";
}
jisuan(label48.Text, 48);
}
private void ex49(object sender, EventArgs e)
{
if (label49.Text == "0")
{
label49.Text = "1";
}
else
{
label49.Text = "0";
}
jisuan(label49.Text, 9);
}
private void ex50(object sender, EventArgs e)
{
if (label50.Text == "0")
{
label50.Text = "1";
}
else
{
label50.Text = "0";
}
jisuan(label50.Text, 50);
}
private void ex51(object sender, EventArgs e)
{
if (label51.Text == "0")
{
label51.Text = "1";
}
else
{
label51.Text = "0";
}
jisuan(label51.Text, 51);
}
private void ex52(object sender, EventArgs e)
{
if (label52.Text == "0")
{
label52.Text = "1";
}
else
{
label52.Text = "0";
}
jisuan(label52.Text, 52);
}
private void ex53(object sender, EventArgs e)
{
if (label53.Text == "0")
{
label53.Text = "1";
}
else
{
label53.Text = "0";
}
jisuan(label53.Text, 53);
}
private void ex54(object sender, EventArgs e)
{
if (label54.Text == "0")
{
label54.Text = "1";
}
else
{
label54.Text = "0";
}
jisuan(label54.Text, 54);
}
private void ex55(object sender, EventArgs e)
{
if (label55.Text == "0")
{
label55.Text = "1";
}
else
{
label55.Text = "0";
}
jisuan(label55.Text, 55);
}
private void ex56(object sender, EventArgs e)
{
if (label56.Text == "0")
{
label56.Text = "1";
}
else
{
label56.Text = "0";
}
jisuan(label56.Text, 56);
}
private void ex57(object sender, EventArgs e)
{
if (label57.Text == "0")
{
label57.Text = "1";
}
else
{
label57.Text = "0";
}
jisuan(label57.Text, 57);
}
private void ex58(object sender, EventArgs e)
{
if (label58.Text == "0")
{
label58.Text = "1";
}
else
{
label58.Text = "0";
}
jisuan(label58.Text, 58);
}
private void ex59(object sender, EventArgs e)
{
if (label59.Text == "0")
{
label59.Text = "1";
}
else
{
label59.Text = "0";
}
jisuan(label59.Text, 59);
}
private void ex60(object sender, EventArgs e)
{
if (label60.Text == "0")
{
label60.Text = "1";
}
else
{
label60.Text = "0";
}
jisuan(label60.Text, 60);
}
private void ex61(object sender, EventArgs e)
{
if (label61.Text == "0")
{
label61.Text = "1";
}
else
{
label61.Text = "0";
}
jisuan(label61.Text, 61);
}
private void ex62(object sender, EventArgs e)
{
if (label62.Text == "0")
{
label62.Text = "1";
}
else
{
label62.Text = "0";
}
jisuan(label62.Text, 62);
}
private void ex63(object sender, EventArgs e)
{
if (label63.Text == "0")
{
label63.Text = "1";
}
else
{
label63.Text = "0";
}
jisuan(label63.Text, 63);
}
private void ex64(object sender, EventArgs e)
{
if (label64.Text == "0")
{
label64.Text = "1";
}
else
{
label64.Text = "0";
}
jisuan(label64.Text, 64);
}
private void label79_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//
}
}
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.Text.RegularExpressions;
using System.IO;
namespace _1._2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static List temp = new List();
private void button2_Click(object sender, EventArgs e)//CE按钮
{
textBox1.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
}
private void Add(object sender, EventArgs e)
{ //数字和符号的Click事件
Button button = (Button)sender;
textBox1.Text += button.Text;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)//delect按钮
{
if (textBox1.TextLength > 0)
textBox1.Text = textBox1.Text.Remove(textBox1.TextLength - 1);
}
private void button9_Click(object sender, EventArgs e)//“=”按钮
{
Cleanf();
List zhonglist = ToList(textBox1.Text);
List houlist = houzhui(zhonglist);
List s = zhonglist;
s.Add("=");
s.Add(Calculate(houlist));
string s1 = string.Join("", s);
if (temp.Count > 9)//最多存十条记录
{
temp.RemoveAt(0);
}
temp.Add(s1);
textBox2.Text = string.Join(",", temp);
textBox1.Text = Calculate(houlist);
}
//中缀转List
static List ToList(string s)
{
List ls = new List();//存放中缀表达式
String str = ""; //拼接多位数、小数
char c = ' '; //遍历输入的字符
int i = 0;
do
{
if ((s[i] == '-' && i == 0) || (i > 0 && s[i] == '-' && !char.IsDigit(s[i - 1])))//在负数前加零方便后续计算
{
ls.Add("0");
}
if (!char.IsDigit(c = s[i]) && c != '.') //如果c不是数字和'.',就加入ls
{
ls.Add(c.ToString());
i++; //i后移
}
else//如果是数字,需要考虑多位数
{
str = ""; //先将str清空
while (i < s.Length && (char.IsDigit(c = s[i]) || c == '.'))
{
str += c;
i++;
}
ls.Add(str);
}
} while (i < s.Length);
return ls;
}
static List houzhui(List ls)
{
//定义两个栈,一个存临时结果,一个存运算符
Stack operators = new Stack();
List num = new List();
//遍历 ls
foreach (var item in ls)
{
Regex regex = new Regex("[+-]?\\d+.?\\d*");//整数和浮点数
//如果是一个数,则加入num
if (regex.Match(item).Length > 0)
{
num.Add(item);
}
else if (item.Equals("("))//Equals:比较是否相同
{
operators.Push(item);
}
else if (item.Equals(")"))
{
//如果是右括号,则依次弹出operators栈顶的运算符,并压入num,直到遇到左括号为止,并将这对括号丢弃
while (!operators.Peek().Equals("("))//Peek()返回顶部对象
{
num.Add(operators.Pop());
}
operators.Pop(); //将 ( 弹出
}
else
{
//当item 的 优先级小于或等于operators栈顶运算符,将operators栈顶的运算符弹出并加入到num中,再次转到operators与operators中新的栈顶运算符比较
while (operators.Count != 0 && compare(operators.Peek()) >= compare(item))
{
num.Add(operators.Pop());
}
//还需要将 item 压入栈
operators.Push(item);
}
}
//将operators中剩余的运算符依次弹出并加入num
while (operators.Count != 0)
{
num.Add(operators.Pop());
}
return num; //按顺序输出就是对应的后缀表达式对应的List
}
//运算符优先级
public static int compare(string operation)
{
int res = 0;
switch (operation)
{
case "+":
res = 1;
break;
case "-":
res = 1;
break;
case "^":
res = 3;
break;
case "*":
res = 2;
break;
case "÷":
res = 3;
break;
case "%":
res = 4;
break;
case "!":
res = 5;
break;
default:
Console.WriteLine("不存在该运算符");
break;
}
return res;
}
//计算后缀表达式
static string Calculate(List ls)
{
Regex regex = new Regex("[+-]?\\d+.?\\d*");
//创建栈
Stack stack = new Stack();
//遍历 list
foreach (var item in ls)
{
//使用正则表达式来取出数
if (regex.Match(item).Length > 0) //如果是数字,就压入栈中
{
stack.Push(regex.Match(item).Value);
}
else //不是数就弹出两个数字进行计算
{
double num2 = double.Parse(stack.Pop());
double num1 = double.Parse(stack.Pop());
double res = 0;
if (item.Equals("+"))
{
res = num1 + num2;
}
else if (item.Equals("-"))
{
res = num1 - num2;
}
else if (item.Equals("×"))
{
res = num1 * num2;
}
else if (item.Equals("÷"))
{
res = num1 / num2;
}
else if (item.Equals("%"))
{
res = num1 % num2;
}
else
{
throw new Exception("表达式有误");
}
stack.Push(res.ToString());
}
}
//最后留在stack中的数据就是运算结果
return stack.Pop();
}
private void Cleanf()
{
File.Delete("历史记录2.txt");
FileStream f = File.Create("历史记录2.txt");
f.Close();
}
private void button22_Click(object sender, EventArgs e)//“读取历史”按钮
{
StreamReader sr = new StreamReader("历史记录2.txt");
String s;
if ((s = sr.ReadLine() )!= null)
{
String[] s1 = s.Split(',');
List s2 = new List(s1);
temp = s2;
textBox2.Text = string.Join(",", temp);
}
sr.Close();
}
private void button23_Click(object sender, EventArgs e)//“清除历史记录”
{
textBox2.Text = "";
temp.Clear();
Cleanf();
}
private void button24_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter("历史记录2.txt");
sw.WriteLine(string.Join(",", temp));
sw.Close();
}
private void label1_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace _1._2
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//跳转窗口
Form2 EasyModel = new Form2();
Form1 PerModel = new Form1();
EasyModel.ShowDialog();
for(int i=0;i<1;i++)
{
if (EasyModel.DialogResult == DialogResult.OK)
{
PerModel.ShowDialog();
}
if (PerModel.DialogResult == DialogResult.Cancel)
{
EasyModel.ShowDialog();
i = -1;
}
}
}
}
}