this is suppose to be infix to postfix converter

what is the proplem with this code, it keep saying stack empty

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace postcalc
{
    public partial class Form1 : Form
    {
        Stack stack = new Stack();
        Stack revstack = new Stack();
        string lastnum;
        char[] ramy;
        bool empty = true;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ramy = textBox1.Text.ToCharArray();

            foreach (char c in ramy)
            {
                if (char.IsDigit(c))
                    lastnum = lastnum + c.ToString();
                else
                {
                    stack.Push(lastnum);

                    if (empty == true)
                    {
                        revstack.Push(c);
                        empty = false;
                    }
                    else
                    {
                        switch (c)
                        {
                            case '+':
                                while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/" ||
                                     revstack.Peek().ToString() == "+" || revstack.Peek().ToString() == "-")
                                    stack.Push(revstack.Pop());

                                revstack.Push(c);
                                break;
                            case '-':
                                while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/" ||
                                     revstack.Peek().ToString() == "+" || revstack.Peek().ToString() == "-")
                                    stack.Push(revstack.Pop());

                                revstack.Push(c);
                                break;
                            case '*':
                                while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                                    stack.Push(revstack.Pop());

                                revstack.Push(c);
                                break;
                            case '/':
                                while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                                    stack.Push(revstack.Pop());

                                revstack.Push(c);
                                break;
                        }
                    }
                }

            }
            stack.Push(lastnum);

            while (revstack.Peek().ToString() == "+" ||
                  revstack.Peek().ToString() == "-" ||
                  revstack.Peek().ToString() == "*" ||
                  revstack.Peek().ToString() == "/")
                stack.Push(revstack.Pop());

            while(stack.Peek().ToString() != "")
            revstack.Push(stack.Pop());

            while (revstack.Peek().ToString() != "")
                textBox2.Text = textBox2.Text + revstack.Pop().ToString();




        }
    }
}

The main problem is that all your code is in one function, making it difficult to reason about. You need to design your code around the limitations of your brain.

Also, you end up with an empty stack because you never check if your stack is empty.

yea ,, but why it keep saying the stack is emptyy when it have data in it ???

It doesn't have data in it. What makes you say it has data in it? If it had data in it, the exception wouldn't be thrown.

I don't see any place in your code where you check to see if the stack has data in it.

anyway here is other version

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace postcalc
{
    public partial class Form1 : Form
    {
        Stack stack = new Stack();
        Stack revstack = new Stack();
        string lastnum;
        char[] ramy;
        bool empty = true;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ramy = textBox1.Text.ToCharArray();

            foreach (char c in ramy)
            {
                if (char.IsDigit(c))
                    lastnum = lastnum + c.ToString();
                else
                {
                    stack.Push(lastnum);

                    if (empty)
                    {
                        revstack.Push(c);
                        empty = false;
                    }
                    else
                        check(c);
                 
                }

            }

            stack.Push(lastnum);

            checkingIfThereOtherOperator(); // checking if there any other operations


            reversestack(); // reverse the stack


            // displaynig the answer

            while (revstack.Peek().ToString() != "")
                textBox2.Text = textBox2.Text + revstack.Pop().ToString();




        }



        private void reversestack()
        {
            while (stack.Peek().ToString() != "")
                revstack.Push(stack.Pop());
        }



        private void checkingIfThereOtherOperator()
        {
            while (revstack.Peek().ToString() == "+" ||
               revstack.Peek().ToString() == "-" ||
               revstack.Peek().ToString() == "*" ||
               revstack.Peek().ToString() == "/")
                stack.Push(revstack.Pop());
        }



        private void check(char c)
        {
            switch (c)
            {
                case '+':
                    while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/" ||
                         revstack.Peek().ToString() == "+" || revstack.Peek().ToString() == "-")
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
                case '-':
                    while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/" ||
                         revstack.Peek().ToString() == "+" || revstack.Peek().ToString() == "-")
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
                case '*':
                    while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
                case '/':
                    while (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
            }
        }
    }
}

while (revstack.Peek().ToString() == "+" ||
revstack.Peek().ToString() == "-" ||
revstack.Peek().ToString() == "*" ||
revstack.Peek().ToString() == "/")
stack.Push(revstack.Pop());

I am still with your first code: if revstack pops it can find empty and you get an error.
You cannot pop anything from an empty stack.

yea but with the while condition the stack will only pop when it have data

IMHO why use a loop here? Better use an if I think. Check with the Empty method if your stack is empty or not!

You can't peek if you have an empty stack. (Can you? Oh god please tell me it doesn't return null.)

mmm right ,,, so any doese anybody have ideas to pass this problem ???

Why don't you try reasoning about what your code is doing and figure it out yourself.

the problem is i dont have stack.Isempty() in my compiler

Oh, but you do have stack.Count. I hope.

yea , i figured it out
but i still have some problems with the output resulttt ,, anyway looking forward to fix it

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace postcalc
{
    public partial class Form1 : Form
    {
        Stack stack = new Stack();
        Stack revstack = new Stack();
       

        string lastnum;
        char[] ramy;
        bool empty = true;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ramy = textBox1.Text.ToCharArray();

            foreach (char c in ramy)
            {
                if (char.IsDigit(c))
                    lastnum = lastnum + c.ToString();
                else
                {
                    stack.Push(lastnum);
                    
                    if (empty)
                    {
                        revstack.Push(c);
                        empty = false;
                    }
                    else
                        check(c);
                 
                }

            }

            //stack.Push(lastnum);

            checkingIfThereOtherOperator(); // checking if there any other operations


            reversestack(); // reverse the stack


            // displaynig the answer

            while (revstack.Count != 0)
                textBox2.Text = textBox2.Text + revstack.Pop().ToString();

            


        }
        


        private void reversestack()
        {
            while (stack.Count != 0)
                revstack.Push(stack.Pop());
        }



        private void checkingIfThereOtherOperator()
        {
            while (revstack.Count != 0)
            {
                if (revstack.Peek().ToString() == "+" ||
               revstack.Peek().ToString() == "-" ||
               revstack.Peek().ToString() == "*" ||
               revstack.Peek().ToString() == "/")
                    stack.Push(revstack.Pop());
            }
            
        }



        private void check(char c)
        {
            switch (c)
            {
                case '+':
                  
                        while (revstack.Count != 0)
                            stack.Push(revstack.Pop());

                        revstack.Push(c);
                    
                    break;
                case '-':
                    while (revstack.Count != 0)
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
                case '*':
                    while (revstack.Count != 0)
                    {
                        if (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                            stack.Push(revstack.Pop());
                    }

                    revstack.Push(c);
                    break;
                case '/':
                    while (revstack.Count !=0)
                    {
                        if (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                            stack.Push(revstack.Pop());
                    }

                    revstack.Push(c);
                    break;
            }
        }
    }
}

well i have fixed the problem and now it gives the result thanx evrybody

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace postcalc
{
    public partial class Form1 : Form
    {
        Stack stack = new Stack();
        Stack revstack = new Stack();
       

        string lastnum;
        char[] ramy;
        bool empty = true;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ramy = textBox1.Text.ToCharArray();

            foreach (char c in ramy)
            {
                if (char.IsDigit(c))
                    lastnum = lastnum + c.ToString();
                else
                {
                   stack.Push(lastnum);
                   lastnum = "";
                    
                    if (empty)
                    {
                        revstack.Push(c);
                        empty = false;
                    }
                    else
                        check(c);
                 
                }

            }

            stack.Push(lastnum);
            lastnum = "";

            checkingIfThereOtherOperator(); // checking if there any other operations


            reversestack(); // reverse the stack


            // displaynig the answer
            textBox2.Text = "";
            while (revstack.Count != 0)
                textBox2.Text = textBox2.Text + revstack.Pop().ToString()+" ";

            


        }
        


        private void reversestack()
        {
            while (stack.Count != 0)
                revstack.Push(stack.Pop());
        }



        private void checkingIfThereOtherOperator()
        {
            while (revstack.Count != 0)
                    stack.Push(revstack.Pop());
        }



        private void check(char c)
        {
            switch (c)
            {
                case '+':
                  
                        while (revstack.Count != 0)
                            stack.Push(revstack.Pop());

                        revstack.Push(c);
                    
                    break;
                case '-':
                    while (revstack.Count != 0)
                        stack.Push(revstack.Pop());

                    revstack.Push(c);
                    break;
                case '*':

                    while (revstack.Count != 0)
                        if (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                            stack.Push(revstack.Pop());
                        else
                            break;

                     revstack.Push(c);
                    break;
                case '/':
                    while (revstack.Count != 0)
                        if (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/")
                            stack.Push(revstack.Pop());
                        else
                            break;

                    revstack.Push(c);
                    break;
            }
        }
    }
}

infix to postfix conversion
(A*B*C)+D/F-G*(H-I*J)

infix to postfix conversion
(A*B*C)+D/F-G*(H-I*J)

Is this a question? demand? requisition? ultimatum?
Please post this in a new thread.

i want to display result in new textbox can anyone help me?

Hi Ahmed_9, welcome to Daniweb.
Please post your question in a new thread.
A TextBox has a Text property, if you set it to the string you want, it will display that string.

i am already used the above code but i want also to get the calculation result in anther textbox on the same windows form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.