C#编写一个简易计算器
界面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/17cf4d9f7792896501a25637a2897b46.png)
代码
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 System.Collections;
namespace calculor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stack nums = new Stack();
Stack ops = new Stack();
int qmi(int a, int b)
{
int res = 1;
while (b > 0)
{
if ((b & 1) == 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
void Cal()
{
if (nums.Count <= 1)
{
if(ops.Count >= 0)
{
ops.Pop();
}
return;
}
double a = (double)nums.Peek(); nums.Pop();
double b = (double)nums.Peek(); nums.Pop();
char c = (char)ops.Peek(); ops.Pop();
double d = 1;
if (c == '+') d = a + b;
else if (c == '-') d = b - a;
else if (c == '*') d = b * a;
else if (c == '/')
{
if (a == 0)
{
MessageBox.Show("Invliad operator");
return;
}
else
{
d = b / a;
}
}
else if (c == '%') d = b % a;
else
{
d = Math.Pow(b, a);
}
nums.Push(d);
}
double Calculate(string str)
{
if (str[0] == '0') str = '0' + str;
string left = "";
for (int i = 0; i < str.Length; i++) left += "(";
str = left + str + ")";
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
int j = i, tmp = 0;
bool flag = false;
double t = 0;
while (j < str.Length && ((str[j] >= '0' && str[j] <= '9') || str[j] == '.'))
{
if(str[j] == '.')
{
tmp = j;
j++;
flag = true;
continue;
}
t = t * 10 + str[j] - '0';
j++;
}
i = j - 1;
if (flag)
{
for (int l = 0; l < j - tmp - 1; l++)
{
t *= 0.1;
}
}
nums.Push(t);
}
else
{
char c = str[i];
if (c == ' ') continue;
if (c == '(') ops.Push(c);
else if (c == '+' || c == '-')
{
if (c == '-' && i > 0 && !(str[i - 1] >= '0' && str[i - 1] <= '9') && str[i - 1] != ')' && str[i - 1] != ' ')
{
if (str[i + 1] == '(')
{
nums.Push(-1);
ops.Push('*');
}
else
{
int j = i + 1, tmp = 0;
double t = 0;
bool flag = false;
while (j < str.Length && ((str[j] >= '0' && str[j] <= '9') || str[j] == '.'))
{
if (str[j] == '.')
{
tmp = j;
j++;
flag = true;
continue;
}
t = t * 10 + str[j] - '0';
j++;
}
i = j - 1;
if (flag)
{
for (int l = 0; l < j - tmp - 1; l++)
{
t *= 0.1;
}
}
nums.Push(-t);
}
}
else
{
while ((char)ops.Peek() != '(') Cal();
ops.Push(c);
}
}
else if (c == '*' || c == '/' || c == '%')
{
while ((char)ops.Peek() == '*' || (char)ops.Peek() == '/' || (char)ops.Peek() == '^' || (char)ops.Peek() == '%') Cal();
ops.Push(c);
}
else if (c == '^')
{
while ((char)ops.Peek() == '^') Cal();
ops.Push(c);
}
else if (c == ')')
{
while ((char)ops.Peek() != '(') Cal();
ops.Pop();
}
}
}
if(nums.Count != 0) return (double)nums.Peek();
return -1;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void equal_Click(object sender, EventArgs e)
{
string str = txt_Result.Text;
double res = Calculate(str);
if (res == -1)
{
txt_Result.Text = "";
return;
}
txt_Result.Text = res.ToString();
}
private void digitOne_Click(object sender, EventArgs e)
{
txt_Result.Text += digitOne.Text;
}
private void digitTwo_Click(object sender, EventArgs e)
{
txt_Result.Text += digitTwo.Text;
}
private void digitThree_Click(object sender, EventArgs e)
{
txt_Result.Text += digitThree.Text;
}
private void digitFour_Click(object sender, EventArgs e)
{
txt_Result.Text += digitFour.Text;
}
private void digitFive_Click(object sender, EventArgs e)
{
txt_Result.Text += digitFive.Text;
}
private void digitSix_Click(object sender, EventArgs e)
{
txt_Result.Text += digitSix.Text;
}
private void digitZero_Click(object sender, EventArgs e)
{
txt_Result.Text += digitZero.Text;
}
private void digitSeven_Click(object sender, EventArgs e)
{
txt_Result.Text += digitSeven.Text;
}
private void digitEight_Click(object sender, EventArgs e)
{
txt_Result.Text += digitEight.Text;
}
private void digitNine_Click(object sender, EventArgs e)
{
txt_Result.Text += digitNine.Text;
}
private void cal_Sqrt_Click(object sender, EventArgs e)
{
string str = txt_Result.Text;
double res = Calculate(str);
if (res == -1)
{
txt_Result.Text = "";
return;
}
if (res < 0)
{
MessageBox.Show("{数据不合法");
txt_Result.Text = "";
return;
}
double res1 = Math.Sqrt(res);
txt_Result.Text = res1.ToString();
}
private void Dot_Click(object sender, EventArgs e)
{
txt_Result.Text += Dot.Text;
}
private void cal_Multi_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_Multi.Text;
}
private void cal_Sub_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_Sub.Text;
}
private void cal_Add_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_Add.Text;
}
private void cal_Rem_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_Rem.Text;
}
private void left_brack_Click(object sender, EventArgs e)
{
txt_Result.Text += left_brack.Text;
}
private void right_brack_Click(object sender, EventArgs e)
{
txt_Result.Text += right_brack.Text;
}
private void cal_log_Click(object sender, EventArgs e)
{
string str = txt_Result.Text;
double res = Calculate(str);
if (res == -1)
{
txt_Result.Text = "";
return;
}
if (res < 0)
{
MessageBox.Show("{数据不合法");
txt_Result.Text = "";
return;
}
double res1 = Math.Log(res);
txt_Result.Text = res1.ToString();
}
private void btn_Clear_Click(object sender, EventArgs e)
{
txt_Result.Text = "";
}
private void cal_mi_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_mi.Text;
}
private void cal_Div_Click(object sender, EventArgs e)
{
txt_Result.Text += cal_Div.Text;
}
private void btn_backspace_Click(object sender, EventArgs e)
{
string str = txt_Result.Text;
string newStr = "";
int len = str.Length;
if (len > 0)
{
for(int i = 0; i < len - 1; i++)
{
newStr += str[i];
}
}
txt_Result.Text = newStr;
}
}
}