i ve programmed a postfix calculator it woks fine until if make new calculation
e.g:
3+4*2
=11
new try
3+4*2
i get stack empty error
does anyboy knows why
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();
float result;
float first,second;
string temp;
string lastnum;
char[] ramy;
//char[] calc;
bool empty = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
stack.Clear();
revstack.Clear();
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 postfix
textBox2.Text = "";
while (revstack.Count != 0)
{
temp = revstack.Pop().ToString();
textBox2.Text = textBox2.Text + temp + " ";
stack.Push(temp);
}
//Reversing the stack again
while (stack.Count != 0)
revstack.Push(stack.Pop());
//calculate
calculate();
}
private void calculate()
{
while (revstack.Count != 0)
{
lastnum = revstack.Pop().ToString();
SeeWhatCanUDo();
}
textBox3.Text = stack.Pop().ToString();
}
private void SeeWhatCanUDo()
{
lastnum.ToCharArray();
// MessageBox.Show(lastnum[0].ToString());
if(char.IsDigit(lastnum[0]))
{
lastnum.ToString();
stack.Push(lastnum);
lastnum = "";
}
else
{
lastnum.ToString();
result = claculate();
stack.Push(result);
}
}
private float claculate()
{
second = float.Parse(stack.Pop().ToString());
first = float.Parse(stack.Pop().ToString());
switch (lastnum)
{
case "+":
result = first + second;
break;
case "-":
result = first - second;
break;
case "*":
result = first * second;
break;
case "/":
result = first / second;
break;
}
return result;
}
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;
}
}
}
} Your problem is that you use variables global to your class instead of passing them from function to function. The result is that it's easy for previous states of your program to leak all over the place.
Set your lastnum into null once you wanna try again.
<anyway thanks for the program> i copied a part of it for my assignment.
cheers...