this is suppose to be infix to postfix converter
ramyking 0 Newbie Poster
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();
}
}
}
Rashakil Fol 978 Super Senior Demiposter Team Colleague
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.
ramyking 0 Newbie Poster
yea ,, but why it keep saying the stack is emptyy when it have data in it ???
Rashakil Fol 978 Super Senior Demiposter Team Colleague
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.
ramyking 0 Newbie Poster
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;
}
}
}
}
ddanbe 2,724 Professional Procrastinator Featured Poster
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.
ramyking 0 Newbie Poster
yea but with the while condition the stack will only pop when it have data
ddanbe 2,724 Professional Procrastinator Featured Poster
IMHO why use a loop here? Better use an if I think. Check with the Empty method if your stack is empty or not!
Rashakil Fol 978 Super Senior Demiposter Team Colleague
You can't peek if you have an empty stack. (Can you? Oh god please tell me it doesn't return null.)
ramyking 0 Newbie Poster
mmm right ,,, so any doese anybody have ideas to pass this problem ???
Rashakil Fol 978 Super Senior Demiposter Team Colleague
Why don't you try reasoning about what your code is doing and figure it out yourself.
ramyking 0 Newbie Poster
the problem is i dont have stack.Isempty() in my compiler
Rashakil Fol 978 Super Senior Demiposter Team Colleague
Oh, but you do have stack.Count. I hope.
ramyking 0 Newbie Poster
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;
}
}
}
}
ramyking 0 Newbie Poster
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;
}
}
}
}
harishan 0 Newbie Poster
infix to postfix conversion
(A*B*C)+D/F-G*(H-I*J)
ddanbe 2,724 Professional Procrastinator Featured Poster
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.
أحمد_9 0 Newbie Poster
i want to display result in new textbox can anyone help me?
ddanbe 2,724 Professional Procrastinator Featured Poster
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.
أحمد_9 0 Newbie Poster
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.