c#简单的科学计算器(实现功能:括弧 负数 小数运算)
作者:互联网
wdnm,你说俺一个学信息安全的,为啥要学c#呢。。。。。哎。。都是实验室逼得。。。。学长还一个比一个消极。。。你说俺咋整啊。。。。
初学学了三四天c#,学长让我实现一个计算器,难度还是大大的。。。
写的很简单。。。也很笨拙。。。好多可以优化的地方我懒得改了。。。
我好想忘整一个 “撤销键”。。。(⊙﹏⊙)
难点
1.计算器算法:
刚开始自己想了一个贼麻烦的计算方法,十分麻烦。被学长疯狂diss。。。。。
算法其实还是很简单的,下面这篇博客讲的就很好:
https://blog.csdn.net/wanzhen4330/article/details/81952851
2.窗体设计:
刚开始学习设计窗体贼吉尔惆怅。。。。看了视频教学之后就好多了;
p2讲的很详细,俺全靠这一个视频才活下来的;
https://www.bilibili.com/video/av1422127/?p=2
3.类的调用:
刚开始不会调用类啊!c#和c++不一样啊!c#的头文件什么鬼啊!!!!
虽然俺c++的类也只会一丢丢。。。。
看了视频之后才会用。。。。p8;
https://www.bilibili.com/video/av1422127/?p=8
写完之后觉得自己还是没有充分利用类,没有把各种方法进行分类
效果
可以连续计算(^U^)ノ~YO
suprise 妈* *克er!!!
代码
//Form1.cs
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 Form1 : Form
{
private string shizi="";
private string value;
private bool flag=false;
public Form1()
{
InitializeComponent();
}
private void btn_0_Click(object sender, EventArgs e)
{
shizi += '0';
textBox1.AppendText("0");
textBox1.ScrollToCaret();
}
private void btn_1_Click(object sender, EventArgs e)
{
shizi += '1';
textBox1.AppendText("1");
textBox1.ScrollToCaret();
}
private void btn_2_Click(object sender, EventArgs e)
{
shizi += '2';
textBox1.AppendText("2");
textBox1.ScrollToCaret();
}
private void btn_3_Click(object sender, EventArgs e)
{
shizi += '3';
textBox1.AppendText("3");
textBox1.ScrollToCaret();
}
private void btn_4_Click(object sender, EventArgs e)
{
shizi += '4';
textBox1.AppendText("4");
textBox1.ScrollToCaret();
}
private void btn_5_Click(object sender, EventArgs e)
{
shizi += '5';
textBox1.AppendText("5");
textBox1.ScrollToCaret();
}
private void btn_6_Click(object sender, EventArgs e)
{
shizi += '6';
textBox1.AppendText("6");
textBox1.ScrollToCaret();
}
private void btn_7_Click(object sender, EventArgs e)
{
shizi += '7';
textBox1.AppendText("7");
textBox1.ScrollToCaret();
}
private void btn_8_Click(object sender, EventArgs e)
{
shizi += '8';
textBox1.AppendText("8");
textBox1.ScrollToCaret();
}
private void btn_9_Click(object sender, EventArgs e)
{
shizi += '9';
textBox1.AppendText("9");
textBox1.ScrollToCaret();
}
private void btn_point_Click(object sender, EventArgs e)
{
shizi += '.';
textBox1.AppendText(".");
textBox1.ScrollToCaret();
}
private void btn_l_bracket_Click(object sender, EventArgs e)
{
shizi += '(';
textBox1.AppendText("(");
textBox1.ScrollToCaret();
}
private void btn_r_bracket_Click(object sender, EventArgs e)
{
shizi += ')';
textBox1.AppendText(")");
textBox1.ScrollToCaret();
}
private void btn_add_Click(object sender, EventArgs e)
{
shizi += '+';
textBox1.AppendText("+");
textBox1.ScrollToCaret();
}
private void btn_sub_Click(object sender, EventArgs e)
{
shizi += '-';
textBox1.AppendText("-");
textBox1.ScrollToCaret();
}
private void btn_mul_Click(object sender, EventArgs e)
{
shizi += '*';
textBox1.AppendText("*");
textBox1.ScrollToCaret();
}
private void btn_div_Click(object sender, EventArgs e)
{
shizi += '/';
textBox1.AppendText("/");
textBox1.ScrollToCaret();
}
private void btn_remain_Click(object sender, EventArgs e)
{
shizi += '%';
textBox1.AppendText("%");
textBox1.ScrollToCaret();
}
private void btn_val_Click(object sender, EventArgs e)
{
if (shizi.Length == 0) return;
textBox1.Text += "\r\n=\r\n";
diaoyong temp = new diaoyong();
value = temp.getstring(shizi);
//textBox1.Text += value ;
shizi = value;
textBox1.AppendText(value);
textBox1.ScrollToCaret();
}
private void btn_clear_Click(object sender, EventArgs e)
{
if(shizi.Length>0)
shizi = shizi.Remove(0);
//value = value.Remove(0);
textBox1.Text = string.Empty;
}
}
}
算法实现代码↓
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 简单计算器
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class diaoyong
{
//private string shizi;
private Stack<char> symbol = new Stack<char>(1000);//符号栈
private Stack<double> num = new Stack<double>(1000);//数栈
//获得运算式子
public string getstring(string shizi)
{
string shabi = "wdnm!你个**式子写错了!";//绝对有2B来捣乱!
//先检查式子是否正确
if (!detection_all(shizi)) return shabi;
if (!detection_bracket(shizi)) return shabi;
//MessageBox.Show("??");
return stack_operation(shizi);
}
private string stack_operation(string shizi)
{
int flag1 = 0;//负数判断
int flag2 = 0;//小数判断
double temp = 0;
for(int i=0;i<shizi.Length;i++)
{
if (detection_num(shizi[i]))
{
if (flag1 != 0 && flag2 != 0)//负小数
{
temp -= stringTOdouble(shizi[i], flag2);
flag2++;
}
else if (flag1 != 0 && flag2 == 0)//负整数
temp = temp * 10 - (shizi[i] - '0');
else if (flag1 == 0 && flag2 != 0)//正小数判断
temp += stringTOdouble(shizi[i], flag2++);
else temp = temp * 10 + (shizi[i] - '0');//正整数判断
}
else
{
switch(shizi[i])
{
case '.': flag2++;break;
case '-': if (detection_negative(shizi, i)) flag1 = 1;
else { symbol.Push('-'); num.Push(temp);temp = 0;flag1 = 0;flag2 = 0; }
break;
case '+': symbol.Push('+'); if (shizi[i - 1] != ')') num.Push(temp); temp = 0; flag1 = 0; flag2 = 0; break;
case '*': symbol.Push('*'); if (shizi[i - 1] != ')') num.Push(temp); temp = 0; flag1 = 0; flag2 = 0; break;
case '/': symbol.Push('/'); if (shizi[i - 1] != ')') num.Push(temp); temp = 0; flag1 = 0; flag2 = 0; break;
case '%': symbol.Push('%'); if (shizi[i - 1] != ')') num.Push(temp); temp = 0; flag1 = 0; flag2 = 0; break;
case '(': symbol.Push('('); flag1 = 0; flag2 = 0; break;
case ')': if(shizi[i-1]!=')') num.Push(temp);
temp = 0; flag1 = 0; flag2 = 0; run_data(); break;
}
}
}
if (shizi[shizi.Length - 1] != ')')
{
num.Push(temp);
}
if(num.Count>1)
run_data();
//double cnt = num.Count();
//string ccnt = cnt.ToString();
//MessageBox.Show(ccnt);
double ttemp = num.Peek();
string amazing = ttemp.ToString();
return amazing;
}
private void run_data()
{
double temp1;
double temp2;
char temp3;
while(num.Count>1&&symbol.Count>0)
{
temp3 = symbol.Peek();
symbol.Pop();
if (temp3 == '(') break;
temp2 = num.Peek();
num.Pop();
temp1 = num.Peek();
num.Pop();
switch(temp3)
{
case '+': temp1 += temp2;break;
case '-': temp1 -= temp2;break;
case '*': temp1 *= temp2;break;
case '/': temp1 /= temp2;break;
case '%': temp1 %= temp2;break;
}
num.Push(temp1);
}
}
//string转为double小数
private double stringTOdouble(char temp,int x)
{
double num = 0;
num = temp - '0';
for(int i=1;i<=x;i++)
{
num /= 10;
}
return num;
}
//检测整体式子函数
private bool detection_all(string shizi)
{
int len = shizi.Length;
if (!detection_num(shizi[0]) && (shizi[0] != '(' && shizi[0] != '-')) return false; //首位检测
if (!detection_num(shizi[len - 1]) && shizi[len - 1] != ')') return false;//末位检测
for(int i=1;i<shizi.Length-1;i++)
{
if (detection_num(shizi[i])) continue;
if(shizi[i]=='-')
{
if (!detection_num(shizi[i - 1]) && (shizi[i - 1] != '(' && shizi[i - 1] != ')')) return false;
if (!detection_num(shizi[i + 1])&&shizi[i+1]!='(') return false;
}
else if(shizi[i]=='(')
{
if (detection_num(shizi[i - 1]) || shizi[i-1]==')') return false;
if (shizi[i + 1] == '*' || shizi[i + 1] == '/' || shizi[i + 1] == '%' || shizi[i + 1] == '+') return false;
}
else if(shizi[i]==')')
{
if (shizi[i - 1] == '*' || shizi[i - 1] == '/' || shizi[i - 1] == '%' || shizi[i - 1] == '+'||shizi[i-1]=='-') return false;
if (detection_num(shizi[i+1]) || shizi[i + 1] == '(') return false;
}
else if(shizi[i]=='+'||shizi[i]=='*'||shizi[i]=='/'||shizi[i]=='%'||shizi[i]=='.')
{
if (!detection_num(shizi[i - 1]) && shizi[i - 1] != ')') return false;
if (!detection_num(shizi[i + 1]) && shizi[i + 1] != '(') return false;
if (shizi[i] == '/' && shizi[i + 1] == '0') return false;
}
}
return true;
}
//检测负数规则函数
private bool detection_negative(string shizi,int i)
{
if (i == 0) return true;
if (shizi[i - 1] =='(')
return true;
return false;
}
//检测是否是数字函数
private bool detection_num(char temp)
{
if (temp >= '0' && temp <= '9') return true;
else return false;
}
//检测括弧顺序
private bool detection_bracket(string shizi)
{
int flag = 0;
for(int i=0;i<shizi.Length;i++)
{
if (shizi[i] == '(') flag++;
if (shizi[i] == ')') flag--;
if (flag < 0) return false;
}
if (flag != 0) return false;
return true;
}
}
}
啊??界面自己看着代码设计去!岂能让你直接copy!要啥自行车!
注意控件名字哈
标签:括弧,c#,void,private,shizi,textBox1,EventArgs,btn,小数 来源: https://blog.csdn.net/weixin_44009711/article/details/98104191