-5

enter image description here

I have a calciulator. it works fine example enter 10-5 in textbox1 and result shown on textbox2. but i want to calculate more. example 8-5*3-1 like this. or 7-2+3 and so on.
how it will be ? here is my code

public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        float ricxvi1, pasuxi;
        int datvla;

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();

           textBox1.Text = textBox1.Text + 7;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox1.Text = textBox1.Text + 0;
            textBox2.Clear();
            textBox2.Text = textBox2.Text + 0;
            datvla = 0; 
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 4;
        }

        private void button17_Click(object sender, EventArgs e)
        {
                ricxvi1 =  ricxvi1 = float.Parse(textBox1.Text);
                datvla = 1;
                textBox1.Text += "-"; 
        }
        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 1;
        }
        private void button11_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();

            textBox1.Text = textBox1.Text + 2;
        }
        private void button12_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 3;  
        }

        private void mimateba_Click_1(object sender, EventArgs e)
        {
             ricxvi1 = float.Parse(textBox1.Text);

           datvla = 2;
           textBox1.Text += "+";
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 5;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 6;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ricxvi1 = float.Parse(textBox1.Text);

            datvla = 3;
            textBox1.Text += "*";

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
             textBox1.Text = textBox1.Text + 8;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
             textBox1.Text = textBox1.Text + 9;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            ricxvi1 = float.Parse(textBox1.Text);
            datvla = 4;
            textBox1.Text += "/";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 0;
        }

        private void button16_Click_1(object sender, EventArgs e)
        {

            switch (datvla)
            {
                case 1:

                    pasuxi = ricxvi1 - float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));

                    textBox2.Text = pasuxi.ToString();
                    break;
                case 2:
                    pasuxi = ricxvi1 + float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                case 3:
                    pasuxi = ricxvi1 * float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                case 4:
                    pasuxi = ricxvi1 / float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                default:
                    break;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")

                textBox1.Clear();
            textBox1.Text = textBox1.Text + ".";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        }
refreshg
  • 35
  • 1
  • 9
  • 3
    Please read carefully [how to ask questions at SO site](http://stackoverflow.com/help/how-to-ask). Main idea - **you should provide your code and describe your problem** (unexpected results or error). Currently this question only describes your task, but tells nothing about your specific problem and doesn't show any code which you have problem with. – Sergey Berezovskiy Feb 18 '16 at 09:45
  • Are You looking for something like this ? http://stackoverflow.com/questions/21950093/string-calculator –  Feb 18 '16 at 09:58
  • no there is other problem. string convert to int to calculate result. – refreshg Feb 18 '16 at 10:03
  • thanks i use new DataTable().Compute(math, null).ToString(); mthod from stackoverflow.com/questions/21950093/string-calculator . many thanks – refreshg Feb 18 '16 at 10:40

2 Answers2

0

You have to build a parser to understand the calculations done. Regex or manually parsing is very hard, probably harder than using a parser.

A good parser is ANTLR, which also has a sample grammar for a calculator.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

You'll most likely want to create a method which takes in as many decimal values as are entered. Such as my example below, with this you can pass in the entered digits to the method. Then inside the method call calculation methods based on the make up. My point is that you don't want business or data logic handled in the UI and as you have a common task of checking, clearing and assigning to the textbox go for a method that'll handle that for you.Multiple input method