hey i guys iam trying to make a postfix calculator it works very well with the number but when u tried to add parantheses the code doesnt seem to work well
/*Programmer : Ramy Selim
* E-mail:themurder2005@hotmail.com
* postfix calculator
*/
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
{
//Defining two stack
Stack stack = new Stack();
Stack revstack = new Stack();
/*my Variables*/
float result;
float first,second;
string temp;
string lastnum;
char[] ramy;
bool empty = true;
/*end variables*/
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Rese();
Start();
checkingIfThereOtherOperator(); // checking if there any other operations
reversestack(); // reverse the stack
Display(); // displaynig the postfix
//Reversing the stack again
while (stack.Count != 0)
revstack.Push(stack.Pop());
calculate(); //calculate
}
/*Reseting all variable*/
private void Rese()
{
stack.Clear();
revstack.Clear();
result = 0;
first= second =0;
temp = "";
lastnum ="";
empty = true;
}
//Displaying the postfix expression
private void Display()
{
textBox2.Text = "";
while (revstack.Count != 0)
{
temp = revstack.Pop().ToString();
textBox2.Text = textBox2.Text + temp + " ";
stack.Push(temp);
}
}
//start the main
private void Start()
{
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 = "";
}
//claculate the infix
private void calculate()
{
try
{
while (revstack.Count != 0)
{
lastnum = revstack.Pop().ToString();
SeeWhatCanUDo();
}
textBox3.Text = stack.Pop().ToString();
}
catch (Exception e)
{ MessageBox.Show("Sign error","Romz"); }
}
private void SeeWhatCanUDo()
{
try
{
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);
}
}
catch (Exception e)
{
//MessageBox.Show("Sign error", "Romz");
}
}
//complete calculate the infix
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;
case '(':
revstack.Push(c);
break;
case ')':
while (revstack.Count != 0)
{
if (revstack.Peek().ToString() == "*" || revstack.Peek().ToString() == "/" ||
revstack.Peek().ToString() == "+" || revstack.Peek().ToString() == "-")
stack.Push(revstack.Pop());
else
break;
}
revstack.Pop();
break;
}
}
}
}