基本信息
源码名称:C# 数值表达式计算器 示例源码
源码大小:0.21M
文件格式:.zip
开发语言:C#
更新时间:2018-10-13
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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 计算器
{
public partial class 计算器 : Form
{
public 计算器()
{
InitializeComponent();
}
public int tag = 0,suanwan = 0;//上次输入的字符或数字的标志
Queue<string> lishi = new Queue<string>();
Queue<string> que = new Queue<string>();
Stack<double> st = new Stack<double>();
Stack<string> ch= new Stack<string>();
Queue<string> tmp = new Queue<string>();
public void jisuan(string s)
{
int k = 0, ctos = 0, stoc = 0;
for (int i = 0; i < s.Length; i )
{
if (s[i] <= '9' && s[i] >= '0' || s[i] == '.'||((i == 0||s[i-1]=='(')&&(s[i]=='-'||s[i]==' ')))
{
if (ctos == 0)
{
k = i;
ctos = 1;
stoc = 0;
}
}
else
{
if (stoc == 0)
{
que.Enqueue(s.Substring(k, i - k));
ctos = 0;
stoc = 1;
}
if (s[i] != '=')
que.Enqueue("" s[i]);
}
}
ch.Push("#");
//foreach (string x in que)
//{
// Console.WriteLine(x);
//}
//Console.WriteLine("结束");
string y;
while (que.Count > 0)
{
y = que.Dequeue();
if (y == " " || y == "-" || y == "*" || y == "/" || y == "(" || y == ")" )
{
if (ch.Peek() == "#" || y == "(")
{
ch.Push(y);
}
else if ( y == " " || y == "-")
{
string a = ch.Pop();
while(a!="#"&&a!="(")
{
double x, v;
v = st.Pop();
x = st.Pop();
if (a == " ")
st.Push(x v);
if (a == "-")
st.Push(x - v);
if (a == "*")
st.Push(x * v);
if (a == "/")
st.Push(x / v);
a = ch.Pop();
}
ch.Push(a);
ch.Push(y);
}
else if (y == "*" || y == "/")
{
string a = ch.Peek();
if (a == " " || a == "-" || a == "#" || a == "(")
{
ch.Push(y);
}
else if(a == "*" || a == "/")
{
a = ch.Pop();
while (a != "#" && a != "(" && a != " " && a != "-")
{
double x, v;
v = st.Pop();
x = st.Pop();
if (a == "*")
st.Push(x * v);
if (a == "/")
st.Push(x / v);
a = ch.Pop();
}
ch.Push(a);
ch.Push(y);
}
}
else if (y == ")")
{
string r = ch.Pop();
while (r != "(")
{
double x, v;
v = st.Pop();
x = st.Pop();
if (r == " ")
st.Push(x v);
if (r == "-")
st.Push(x - v);
if (r == "*")
st.Push(x * v);
if (r == "/")
st.Push(x / v);
r = ch.Pop();
}
}
}
else
{
try
{
st.Push(Convert.ToDouble(y));
}
catch//随便加的解决第一个夸好
{
}
}
}
//foreach (double x in st)
//{
// Console.WriteLine(x);
//}
string g = ch.Pop();
while (g != "#")
{
double x, v;
v = st.Pop();
x = st.Pop();
if (g == " ")
st.Push(x v);
if (g == "-")
st.Push(x - v);
if (g == "*")
st.Push(x * v);
if (g == "/")
st.Push(x / v);
g = ch.Pop();
}
try
{
label1.Text = st.Pop().ToString();
}
catch
{
}
ch.Clear();
que.Clear();
st.Clear();
suanwan = 1;
if (checkBox2.Checked == true)
{
groupBox1.Text = textBox1.Text label1.Text '\n';
}
}
public void buttonclick(char c)
{
if(suanwan == 1)
{
textBox1.Text = "";
suanwan = 0;
}
if (tag == 1 && (c == ' ' || c == '-' || c == '/' || c == '*'))
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
tag = 0;
}
else
tag = 0;
switch(c)
{
case '1':
textBox1.Text = '1'; break;
case '2':
textBox1.Text = '2'; break;
case '3':
textBox1.Text = '3'; break;
case '4':
textBox1.Text = '4'; break;
case '5':
textBox1.Text = '5'; break;
case '6':
textBox1.Text = '6'; break;
case '7':
textBox1.Text = '7'; break;
case '8':
textBox1.Text = '8'; break;
case '9':
textBox1.Text = '9'; break;
case '0':
textBox1.Text = '0'; break;
case '.':
textBox1.Text = '.'; break;
case ' ':
textBox1.Text = ' '; break;
case '-':
textBox1.Text = '-'; break;
case '*':
textBox1.Text = '*'; break;
case '/':
textBox1.Text = '/'; break;
case '(':
textBox1.Text = '('; break;
case ')':
textBox1.Text = ')'; break;
case '=':
textBox1.Text = '=';
jisuan(textBox1.Text);
break;
case '<':
if (textBox1.Text.Length>0)
textBox1.Text = textBox1.Text.Substring(0,textBox1.Text.Length-1);
break;
case 'A':
textBox1.Text = label1.Text; break;
}
}
private void button13_Click(object sender, EventArgs e)
{
buttonclick('1');
}
private void button14_Click(object sender, EventArgs e)
{
buttonclick('2');
}
private void button15_Click(object sender, EventArgs e)
{
buttonclick('3');
}
private void button9_Click(object sender, EventArgs e)
{
buttonclick('4');
}
private void button10_Click(object sender, EventArgs e)
{
buttonclick('5');
}
private void button11_Click(object sender, EventArgs e)
{
buttonclick('6');
}
private void button1_Click(object sender, EventArgs e)
{
buttonclick('7');
}
private void button6_Click(object sender, EventArgs e)
{
buttonclick('8');
}
private void button7_Click(object sender, EventArgs e)
{
buttonclick('9');
}
private void button18_Click(object sender, EventArgs e)
{
buttonclick('0');
}
private void button16_Click(object sender, EventArgs e)
{
buttonclick(' ');
tag = 1;
}
private void button12_Click(object sender, EventArgs e)
{
buttonclick('-');
tag = 1;
}
private void button8_Click(object sender, EventArgs e)
{
buttonclick('/');
tag = 1;
}
private void button3_Click(object sender, EventArgs e)
{
buttonclick('*');
tag = 1;
}
private void button19_Click(object sender, EventArgs e)
{
buttonclick('.');
}
private void button20_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
buttonclick('=');
}
private void button5_Click(object sender, EventArgs e)
{
buttonclick(')');
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
buttonclick('<');
}
private void label1_Click(object sender, EventArgs e)
{
}
private void 计算器_Load(object sender, EventArgs e)
{
groupBox1.Text = '\n' "";
label1.Text = "";
lishi.Clear();
}
private void button17_Click(object sender, EventArgs e)
{
buttonclick('A');
}
private void label2_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked == true)
this.Width = groupBox1.Width;
else
this.Width -= groupBox1.Width;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
buttonclick('(');
}
}
}