Hi!!!
i have made a window application for solving mathematical expression using bodmas rule....
In that I am getting error IndexOutOfBound exception

private void button5_Click(object sender, EventArgs e)
        {

string[] operands = this.textBox1.Text.Split('+', '-', '*', '/'); string[] operators = this.textBox1.Text.Split('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
                List<string> l1 = operators.ToList();
                
                foreach(string i in l1)                
                this.listBox1.Items.Add(i);
                
               
                         //Multiply and divide
                    for(int i = 0; i < operators.Length; ++i)
                    {
                        char[] a1 = operators[i].ToCharArray();
                        char  op =a1[i];
                        int ss = Convert.ToInt32(operands[i]);
                        int ss1 = Convert.ToInt32(operands[i + 1]);
                        if(op == '/' || op == '*')
                        {
                            double answer = 0;
                            switch(op)
                            {
                                case '*':
                                    answer = ss * ss1;
                                    break;
                                case '/':
                                    if(ss1 == 0) 
                                        MessageBox.Show("Cannot divide by zero");
                                    answer = ss / ss1;
                                    break;
                            }
                            ss = (int)answer; 
                            ClearUsedTokens(operands,operators,i);
                        }
                    }
                    //Add and subtract
                    for(int i = 0; i < operators.Length; ++i)
                    {
                        int vv = Convert.ToInt32(operands[i]);
                        int vv1 = Convert.ToInt32(operands[i + 1]);
                        char[] a11 = operators[i].ToCharArray();
                        char op1 = a11[i];
                        if(op1 == '+' || op1 == '-')
                        {
                            double answer = 0;
                            switch(op1)
                            {
                                case '+':
                                    answer = vv + vv1;
                                    break;
                                case '-':
                                    answer = vv - vv1;
                                    break;
                            }
                            vv = (int)answer;
                           ClearUsedTokens(operands,operators,i);
                        }
                    }
                    this.textBox2.Text= operands[0].ToString();
                }
}

Its giving error for line:" char op =a1; "
Can anyone tell me how to modify my code.......

for(int i = 0; i < operators.Length; ++i)
                    {
                        char[] a1 = operators[i].ToCharArray();
                        char  op =a1[i];

Your operators array is an array of strings with a Lenght of the number of operators in your textbox. All these strings have a Lenght of 1. At any one moment a1(also an array) contains only one char. You are looping through the a1 char array with the index of the operator array. This is likely going to give the trouble you have.
If that is not the solution then I must be feeling as fuzzy as your code:confused:
You are mixing some things up. Try to rewrite and clean up a bit if you can.

Also , can u tell me how to remove array block from memory.Like, here, I am making values null but i want to remove these blocks permanently....

 public void ClearUsedTokens(string[] operands,string[] operators,int i)
     {
         operators[i] = null;
         operands[i] = null;

     }

here I am collecting all operators together in one array and operands in another array.Then,i am accessing first two operans and one operator one by one and applying operation.
Now I want that when operator is applied on first two operands the result should be stored on the place of second operand i.e. operand[i+1] in the array . And operand[i] and operator[i] should be removed from arrays.So that in next step I may use next operator, and next two operands and solve expression by bodmas rule.......
Can u tell me how to solve expression and modify my code.....
As I am not properly able to code my application..
kindly help me.....

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You should not use arrays for this sort of thing. A Stack is the preferred data type. Make two, one for operators and one for operands. A stack object has in essence two methods Push and Pop. It works like this: pop an operator from the operator stack pop two operands from the other. perform calculation and push the result back on the operand stack.
Need to go now, try to work something out for yourself first. I will stay in touch, or others will surely also be able to help you.

This article has been dead for over six months. Start a new discussion instead.