SQL Server 数据库实验课第二周——计算器
·
用Visual Studio 2019做一个计算器
1.创建新项目

模式选择window窗体应用

项目名称calcular

开始做计算器

可从左边的工具箱拖拽需要用的组件到Form1上,我们用Button和TextBox两个组件来做

Form1.cs记录的是可视化界面的源码情况,可在里面修改代码

如果不小心关闭了一些界面可在右上角搜索框搜索,

右边显示的是该组件的属性,可在里面规定字体一类,该图是textBox属性

设计好界面后去Form1.cs修改代码,编辑文本组件相应的事件(途中C是归零)
运行代码,形成可使用的计算器
思路
因为数据结构之前学过堆栈计算后缀表达式,故
我设计的计算器是先用string s记录textBox中的字符串,然后将中缀表达式转换成后缀表达式,再计算后缀表达式


原理
中缀表达式s转换为后缀表达式s1然后计算
用string s1记录转换成功后的后缀表达式
一、中缀->后缀
遍历字符串s
1.遇到数字或小数点,直接放入s1(加入空格是为了区分数字,防止如1和2放在一起成了12)
代码
while (i<s.Length &&(s[i] >= '0' && s[i] <= '9'||s[i]=='.'))
{
s1 += s[i];
i++;
}
i--;
s1 += " ";
2.遇到运算符,将运算符与栈顶元素比较,若优先级高于栈顶运算符则压入栈,若优先级低于栈顶运算符,则将栈顶运算符弹出(即用s1记录)再比较
代码
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
{
if (op.Count == 0 || dic[s[i]] > dic[op.Peek()])
{
op.Push(s[i]);//栈为空,或者s[i]优先级大于栈顶,入栈
}
else
{
Stack<Char> op1 = new Stack<Char>();
while (op.Count > 0 && dic[s[i]] <= dic[op.Peek()])//s[i]优先级小于等于栈顶元素
{
s1 += op.Peek() + " ";
op.Pop();
}
op.Push(s[i]);
}
字符串s遍历完成,s1记录运算符顺序
while (op.Count > 0)
{
s1 += op.Peek();
op.Pop();
}
二、计算后缀表达式
令s=s1,遍历s
Stack num = new Stack();
1.遇到数字直接入栈
2.遇到运算符,出栈num第一个和第二个数来运算,并将运算结果入栈
3.最后输出num中的结果
代码
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;
namespace Calculator
{
public partial class C : Form
{
public C()
{
InitializeComponent();
}
Double LeftNum, Rightnum, Result;
String Flag;
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button_Add_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
private void button_multiply_Click(object sender, EventArgs e)
{
textBox1.Text += "*";
}
private void Divide_Click(object sender, EventArgs e)
{
textBox1.Text += "/";
}
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
private void button_Clear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button_reduce_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
private void button_equal_Click(object sender, EventArgs e)
{
String s = textBox1.Text;
String s1 = "";
Stack<Char> op = new Stack<Char>();
Dictionary<char, int> dic = new Dictionary<char, int>();
dic['+'] = dic['-'] = 1;
dic['*'] = dic['/'] = 2;
for (int i = 0; i < s.Length; i++)//中缀表达式转换后缀表达式
{
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
{
if (op.Count == 0 || dic[s[i]] > dic[op.Peek()])
{
op.Push(s[i]);//栈为空,或者s[i]优先级大于栈顶,入栈
}
else
{
Stack<Char> op1 = new Stack<Char>();
while (op.Count > 0 && dic[s[i]] <= dic[op.Peek()])//s[i]优先级小于等于栈顶元素
{
s1 += op.Peek() + " ";
op.Pop();
}
op.Push(s[i]);
}
}
else
{
while (i<s.Length &&(s[i] >= '0' && s[i] <= '9'||s[i]=='.'))
{
s1 += s[i];
i++;
}
i--;
s1 += " ";
}
}
while (op.Count > 0)
{
s1 += op.Peek();
op.Pop();
}
//转换为后缀表达式s1
s = s1;
Stack<Double> num = new Stack<double>();
for (int i = 0; i < s.Length; i++)
{
if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
{
double a = num.Peek();
num.Pop();
double b = num.Peek();
num.Pop();
if (s[i] == '/') num.Push(b / a);
else if (s[i] == '-') num.Push(b - a);
else if (s[i] == '+') num.Push(a + b);
else if (s[i] == '*') num.Push(a * b);
}
else if(s[i]!=' ')
{
string sm = "";
while (i < s.Length && (s[i] >= '0' && s[i] <= '9' || s[i] == '.'))
{
sm += s[i];
i++;
}
i--;
Double d = Convert.ToDouble(sm);
num.Push(d);
}
}
s1 = Convert.ToString(num.Peek());
textBox1.Text = s1;
}
}
}
更多推荐
所有评论(0)