学习的大佬的思路,自己实现了一下
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;
namespace jisuanqi3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static double suanfa(string wenben)
{
List<string> wenbenf = new List<string>();//声明一个字符串集合,用来存储分割后的字符串
List<int> kuohaosuoyin = new List<int>();//声明一个整数集合,用来存储括号在字符串中的索引
bool chengchu;//声明一个bool变量,用来存储算式中是否有乘除
double jieguo;//声明一个小数,用来存储最后结果
wenbenf = fenge(wenben);//调用方法fenge,将wenben中运算符间的数字分割到一起
kuohaosuoyin = huoqukuohaosuoyin(wenbenf); //调用方法huoqukuohaosuoyin,把wenbenf给他,以获得括号在字符串中的索引
chengchu = panduanchengchu(wenbenf);//调用方法have,把li给他以获得 bool hav 的值
if (kuohaosuoyin.Count > 1)//判断括号索引的元素个数是否大于1
{
jieguo = daikuohao(kuohaosuoyin, wenbenf, chengchu);//调用带括号的计算方法
}
else
{
jieguo = jisuan(wenbenf, chengchu);//直接调用计算方法
}
return jieguo;//返回结果
}
public static List<string> fenge(string wenben)//方法:将文本中运算符间的数字分割到一起,成为一个字符串集合
{
char[] jihe = wenben.ToCharArray();//将字符串分割成字符集合
string num = "";//声明一个空的字符串用来存储每次分割到的字符串
int where = 0;//声明一个空的整数,用来存储字符集合的当前索引
bool a = false;//声明一个bool,当后括号后面仍然有内容时,用来写入后括号后面的运算符
List<string> wenbenf = new List<string>();//声明一个字符串数组来存储分割后的结果
for (int c = 0; c < jihe.Length; c++)//循环读取字符串集合中的每一个元素
{
if (jihe[c] == ')')//判断是否为后括号
{
for (int i = where; i < c; i++)//如果是则读取where到c的每一个元素
{
num = num + jihe[i];//写入字符串num
}
wenbenf.Add(num);//将字符串num写入wenbenf中
wenbenf.Add(")");//将后括号写入wenbenf中
where = c + 1;//将where设置为c+1
num = "";//写入完毕清空支付穿
a = true;//设置一个bool
}
else if (a)//当后括号后面仍然有内容时,用来写入后括号后面的运算符
{
wenbenf.Add(Convert.ToString(jihe[c]));
where = c + 1;
num = "";
a = false;
}
else if (jihe[c] == '+' || jihe[c] == '-' || jihe[c] == '*' || jihe[c] == '/')//判断是否为运算符
{
for (int i = where; i < c; i++)//读取where到c中间的字符,写入到字符串num
{
num = num + jihe[i];
}
wenbenf.Add(num);//将num写入字符串集合wenbenf中
switch (jihe[c])//判断运算符为哪一个,并写入字符串集合 wenbenf中
{
case '+': wenbenf.Add("+"); break;
case '-': wenbenf.Add("-"); break;
case '*': wenbenf.Add("*"); break;
case '/': wenbenf.Add("/"); break;
}
where = c + 1;写入完毕更改索引where
num = string.Empty;//清空字符串
}
if (jihe[c] == '(')//判断是否为前括号,如是写入字符串集合 wenbenf中
{
wenbenf.Add("(");
where = c + 1;//写入完毕更改索引where
}
}
num = string.Empty;//清空字符串
if (jihe[jihe.Length - 1] != ')')//判断最后一个字符是否为后括号,如是则直接返回,不是则写入where到jihe最后一位
{
for (int i = where; i < jihe.Length; i++)
{
num = num + jihe[i];
}
wenbenf.Add(num);
}
return wenbenf;
}
public static List<int> huoqukuohaosuoyin(List<string> wenbenf)//方法:获取括号索引
{
int num = 0;//括号(组)的个数
List<int> where = new List<int>();
for (int i = 0; i < wenbenf.Count; i++)
{
if (wenbenf[i] == "(")
{
num = num + 1;
where.Add(i);
}
if (wenbenf[i] == ")")
{
where.Add(i);
}
}
return where;
}
public static bool panduanchengchu(List<string> wenbenf)//方法:判断字符串中是否有乘除。有返回true,没有返回fales
{
bool chengchu = false;
for (int i = 0; i < wenbenf.Count; i++)
{
if (wenbenf[i] == "*" || wenbenf[i] == "/")
{
chengchu = true;
return chengchu;
}
}
return chengchu;
}
public static double jisuan(List<string> wenbenf, bool chengchu)//方法:计算不带括号的字符串结果
{
int i = 0;
double c = 0;//计算结果
bool Chengchu;
if (chengchu)//有乘除法的时候优先按顺序将乘除符号两边的数字运算
{
while (true)
{
i++;
if (wenbenf[i] == "*")
{
c = double.Parse(wenbenf[i - 1]) * double.Parse(wenbenf[i + 1]);
wenbenf[i - 1] = Convert.ToString(c);
wenbenf.RemoveAt(i);
wenbenf.RemoveAt(i);
i = 0;
}
else if (wenbenf[i] == "/")
{
c = double.Parse(wenbenf[i - 1]) / double.Parse(wenbenf[i + 1]);
wenbenf[i - 1] = Convert.ToString(c);
wenbenf.RemoveAt(i);
wenbenf.RemoveAt(i);
i = 0;
}
Chengchu = panduanchengchu(wenbenf);//判断字符串中是否还有乘除法
if (!Chengchu)
{
break;
}
c = 0;
}
c = 0;
}
if (wenbenf.Count != 1)
{
while (true)
{
if (wenbenf[1] == "+")
{
c = double.Parse(wenbenf[0]) + double.Parse(wenbenf[2]);
wenbenf[0] = Convert.ToString(c);
wenbenf.RemoveAt(1);
wenbenf.RemoveAt(1);
}
else if (wenbenf[1] == "-")
{
c = double.Parse(wenbenf[0]) - double.Parse(wenbenf[2]);
wenbenf[0] = Convert.ToString(c);
wenbenf.RemoveAt(1);
wenbenf.RemoveAt(1);
}
c = 0;
if (wenbenf.Count == 1)//当文本内容只剩1时结束
{
break;
}
}
}
return double.Parse(wenbenf[0]);
}
public static double daikuohao(List<int> kuohaosuoyin, List<string> wenbenf, bool chengchu)//方法:计算带括号的字符串结果
{
List<string> kuohaoneirong = new List<string>();
double jieguo = 0;//结果
int cut = 0;//括号内容数量
int kuohaoshuliang = kuohaosuoyin.Count / 2;//括号数量
List<int> kuohaosuoyin1 = new List<int>();//括号索引
kuohaosuoyin1 = kuohaosuoyin;//括号索引
for (int i = 0; i < kuohaoshuliang; i++)//循环处理每个括号
{
for (int b = kuohaosuoyin1[0] + 1; b < kuohaosuoyin1[1]; b++)//循环读取括号中的内容,存储进kuohaoneirong中
{
kuohaoneirong.Add(wenbenf[b]);
cut = cut + 1;//每读取到一次,括号内容数量加1,
}
cut = cut + 1;//多加一个是给后括号的
bool Chengchu = panduanchengchu(kuohaoneirong);//判断括号内容中是否有乘除号
jieguo = jisuan(kuohaoneirong, Chengchu);//计算括号内容结果
wenbenf[kuohaosuoyin1[0]] = Convert.ToString(jieguo);//将计算结果写入原前括号所在的位置
for (int c = 0; c < cut; c++)//循环删除字符串中,原前括号索引到后括号索引的内容
{
wenbenf.RemoveAt(kuohaosuoyin1[0] + 1);
}
kuohaoneirong.Clear();//清空kuohaoneirong
kuohaosuoyin1 = huoqukuohaosuoyin(wenbenf);//再次获取剩余的字符串中的括号索引
cut = 0;//初始化括号内容数量
}
jieguo = jisuan(wenbenf, chengchu);//处理完括号里面的内容后,再次计算字符串结果
return jieguo;//返回结果
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0";//给文本框赋初值0
}
private void button1_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")//判断文本框内容不为0时,文本框加等于1,为零时,文本框内容等于0
{
textBox1.Text += "1";
}
else
{
textBox1.Text = "1";
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "2";
}
else
{
textBox1.Text = "2";
}
}
private void button3_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "3";
}
else
{
textBox1.Text = "3";
}
}
private void button4_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "4";
}
else
{
textBox1.Text = "4";
}
}
private void button5_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "5";
}
else
{
textBox1.Text = "5";
}
}
private void button6_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "6";
}
else
{
textBox1.Text = "6";
}
}
private void button7_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "7";
}
else
{
textBox1.Text = "7";
}
}
private void button8_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "8";
}
else
{
textBox1.Text = "8";
}
}
private void button9_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "0")
{
textBox1.Text += "9";
}
else
{
textBox1.Text = "9";
}
}
private void button10_Click_1(object sender, EventArgs e)//0
{
string a = null;
a = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
if (textBox1.Text != "0")
{
textBox1.Text += "0";
}
else
{
textBox1.Text = "0";
}
}
private void buttonWait_Click_1(object sender, EventArgs e)//=
{
try
{
string A = "";
double jieguo;
jieguo = suanfa(textBox1.Text);
textBox2.Text = textBox1.Text + "=";
A = Convert.ToString(jieguo);
if (A != "非数字" && A != "正无穷大")
{
textBox1.Text = A;
}
else
{
MessageBox.Show("0不能作为被除数");
textBox1.Text = "0";
}
}
catch (Exception)
{
MessageBox.Show("请输入合理算式");
textBox1.Text = "0";
}
}
private void buttonDivide_Click_1(object sender, EventArgs e)//chu
{
string a = null;
a = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
if (a != "+" && a != "-" && a != "*" && a != "/" && a != "%")
{
textBox1.Text += "/";
}
else
{
a = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
textBox1.Text = a + "/";
}
}
private void buttonRide_Click_1(object sender, EventArgs e)//*
{
string a = null;
a = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
if (a != "+" && a != "-" && a != "*" && a != "/" && a != "%")
{
textBox1.Text += "*";
}
else
{
a = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
textBox1.Text = a + "*";
}
}
private void buttonMinus_Click_1(object sender, EventArgs e)//-
{
string a = null;
a = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
if (a != "+" && a != "-" && a != "*" && a != "/" && a != "%")
{
textBox1.Text += "-";
}
else
{
a = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
textBox1.Text = a + "-";
}
}
private void buttonAdd_Click_1(object sender, EventArgs e)//+
{
string a = null;
a = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);//获取文本框最后一个内容
if (a != "+" && a != "-" && a != "*" && a != "/" && a != "%")//不等于运算符号时写入符号,等于运算符号时重新输入
{
textBox1.Text += "+";
}
else
{
a = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
textBox1.Text = a + "+";
}
}
private void button13_Click_1(object sender, EventArgs e)//CE
{
if (textBox1.Text.Length <= 1)
{
textBox1.Text = Convert.ToString(0);
}
if (textBox1.Text.Length > 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
private void buttonClear_Click_1(object sender, EventArgs e)//AC
{
textBox1.Text = "0";
}
private void button12_Click_1(object sender, EventArgs e)//前括号
{
char a;
int b = 0;
int c = 0;
char[] jihe = textBox1.Text.ToCharArray();//将字符串分割成字符集合
for (int i = 0; i < jihe.Length; i++)//循环读取字符串集合中的每一个元素
{
a = jihe[i];
if (a == '(')
{
b++;
}
else if (a == ')')
{
c++;
}
}
a = jihe[jihe.Length - 1];
if (a == '+' || a == '-' || a == '*' || a == '/')//当最后一个字符等于运算符
{
if (b <= c)//当前括号数量不大于后括号数量时才允许继续输入前括号
{
textBox1.Text += "(";
}
}
}
private void button11_Click_1(object sender, EventArgs e)//后括号
{
char a;
int b = 0;
int c = 0;
char[] jihe = textBox1.Text.ToCharArray();//将字符串分割成字符集合
for (int i = 0; i < jihe.Length; i++)//循环读取字符串集合中的每一个元素
{
a = jihe[i];
if (a == '(')
{
b++;
}
else if (a == ')')
{
c++;
}
}
a = jihe[jihe.Length - 1];
if (a != '+' || a != '-' || a != '*' || a != '/')//当最后一个字符不等于运算符
{
if (b > c)//当前括号数量大于后括号数量时才允许继续输入后括号
{
textBox1.Text += ")";
}
}
}
private void buttonDot_Click(object sender, EventArgs e)//小数点
{
textBox1.Text += ".";
}
}
}