首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >试图将计算器文本框项转换为int和char ( for +*等)或使它们从一开始就分开

试图将计算器文本框项转换为int和char ( for +*等)或使它们从一开始就分开
EN

Stack Overflow用户
提问于 2013-11-26 11:11:17
回答 1查看 490关注 0票数 1

我在编写一个计算器时遇到了问题,它可以在文本框(如25*3(6-5)^2 )中显示一个均衡器,然后将它们放入不同的数组中,从而破坏ofc的订单,

在将它们放入文本框之前,我首先尝试将它们分开,所以我使用了以下代码:

代码语言:javascript
运行
复制
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#一无所知,所以我想先尝试一些简单的代码,然后从网络上学到一些东西。

如果有人能帮我,我真的很感激

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-19 06:17:44

我对我的程序做了很少的修改,但最终它仍然有问题,比如输入,比如

123

而不是像123变成

1203

因此,我从一开始就重新编写了我的程序,并使用

代码语言:javascript
运行
复制
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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20215476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档