我在编写一个计算器时遇到了问题,它可以在文本框(如25*3(6-5)^2 )中显示一个均衡器,然后将它们放入不同的数组中,从而破坏ofc的订单,
在将它们放入文本框之前,我首先尝试将它们分开,所以我使用了以下代码:
namespace CalculatorForDS
{
public partial class Form1 : Form
{
public int i=0, j=0, x;
int[] a;
int[] b;
char[] c;
void Process()
{
a = new int[100];
b = new int[100];
c = new char[100];
}
public Form1()
{
InitializeComponent();
Process();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 1;
a[i] = a[i] * 10^j + 1;
j++;
x = a[i];
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "(";
c[i] = '(';
i++;
j = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 2;
a[i] = a[i] * 10^j + 2;
j++;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 3;
a[i] = a[i] * 10^j + 3;
j++;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 4;
a[i] = a[i] * 10^j + 4;
j++;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 5;
a[i] = a[i] * 10^j + 5;
j++;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 6;
a[i] = a[i] * 10^j + 6;
j++;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 7;
a[i] = a[i] * 10^j + 7;
j++;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 8;
a[i] = a[i] * 10^j + 8;
j++;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 9;
a[i] = a[i] * 10^j + 9;
j++;
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ")";
c[i] = ')';
i++;
j = 0;
}
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "^";
c[i] = '^';
i++;
j = 0;
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "/";
c[i] = '/';
i++;
j = 0;
}
private void button19_Click(object sender, EventArgs e)
{
int lenght = textBox1.TextLength - 1;
string text = textBox1.Text;
textBox1.Clear();
for (int i = 0; i < lenght; i++)
textBox1.Text = textBox1.Text + text[i];
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "*";
c[i] = '*';
i++;
j = 0;
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "-";
c[i] = '-';
i++;
j = 0;
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "+";
c[i] = '+';
i++;
j = 0;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
c[i] = '.';
i++;
j = 0;
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
a[i] = a[i] + j * 0;
j++;
}
private void button20_Click(object sender, EventArgs e)
{
listBox1.Items.Add(a[i]);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}但是问题是,例如,当我写1和5时,它在文本框中显示了15,但是我使用列表框检查数组ai中的值,它给我12;或者115 = 84!
我真的搞不清出了什么问题,
所以我想也许我可以换一种方法,把它们放到文本框里,然后把文本框转换成int和char或者什么的,用于数字和操作数。
我的主要项目是使用堆栈,并使用输入的textbox1均衡到后缀版本并显示,
但是,由于我在这个项目之前了解C++,但对C#一无所知,所以我想先尝试一些简单的代码,然后从网络上学到一些东西。
如果有人能帮我,我真的很感激
发布于 2013-12-19 06:17:44
我对我的程序做了很少的修改,但最终它仍然有问题,比如输入,比如
123
而不是像123变成
1203
因此,我从一开始就重新编写了我的程序,并使用
String x;
char ch = x[i];
if ( ch == '+' || ch =='-' || ch =='*' || ch == '/' || ch == '^' )
{//Here comes the Operators, as for me it works for pushing operators into stack }
else
{//Here comes the Operands, as for me , it works for putting them in another String }使用上面的代码,我可以在我的程序中执行我想做的事情,该程序将Infix表达式转换为后缀:D。
https://stackoverflow.com/questions/20215476
复制相似问题