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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public String expression;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
expression = textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = expression + "=" + Result(expression);
}
private int Result(String expression)
{
int length = expression.Length;
char[] a = expression.ToCharArray(0, length);
int[] data = new int[10];
char[] oper = new char[10];
int datacount = 0, opercount = 0;
int temp = 0;
for (int m = 0; m < length; m++)
{
if (Char.IsDigit(a[m]) == true)
{
temp *= 10;
temp += a[m]-'0';
}
else
{
data[datacount] = temp;
datacount++;
temp = 0;
oper[opercount] = a[m];
opercount++;
}
}
data[datacount] = temp;
datacount++;
int temp1 = 0;
int tempdata = 0;
while (temp1<opercount){
int para1 = data[tempdata];
tempdata++;
int para2 = data[tempdata];
if (oper[temp1] == '+')
{ data[tempdata] = para1 + para2; }
else if (oper[temp1] == '-')
{ data[tempdata] = para1 - para2; }
else if (oper[temp1] == '*')
{ data[tempdata] = para1 *para2; }
else if (oper[temp1] == '/')
{ data[tempdata] = para1 / para2; }
temp1++;
}
return data[tempdata];
}
}
}
C#作业——算术式计算器(无优先级)
最新推荐文章于 2021-05-21 10:40:45 发布