mdas calculator

mdas stack calculator

Page 1 of 1

2 Replies - 7132 Views - Last Post: 17 December 2008 - 06:17 AM Rate Topic: -----

#1 cs_starter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 08

mdas calculator

Post icon  Posted 15 December 2008 - 10:13 PM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace stack_calc
{
   public partial class Form1 : Form
   {
	  public Form1()
	  {
		 InitializeComponent();
	  }

	  public void button1_Click(object sender, EventArgs e)
	  {

		 string inputText = Input.Text; //get the user input and assign it to inputText variable
		 Stack stack = new Stack(); //preparation for storage of numbers or operand
		 char[] chars = inputText.ToCharArray(); //convert user input tp char arrays
		 string lastNumber = string.Empty;


		 //to store the number and operands inside a stack object
		 try
		 {
			foreach (char c in chars)
			{
			   if (char.IsDigit(c))
			   {
				  lastNumber = lastNumber + c.ToString();
			   }
			   else
			   {

				  stack.Push(lastNumber);
				  stack.Push(c);
				  lastNumber = string.Empty;
			   }

			}

			stack.Push(lastNumber);

			Stack revStack = new Stack();

			// reverse the stack! it's upside down
			while (stack.Count > 0)
			{
			   revStack.Push(stack.Pop());
			}



			// revStack is now in left to right textbox order,
			// top of stack to bottom. assign it to stack!
			stack = revStack;


			


			//calculate

			double prevNum, currNum = 0;
			string operand;

		   
		   
			   prevNum = int.Parse(stack.Pop().ToString());
			
			

			do
			{
			   operand = stack.Pop().ToString();

			   if (operand == "+")
			   {
				  string strPopped = stack.Pop().ToString();
				  currNum = int.Parse(strPopped);
				  prevNum = prevNum + currNum;

			   }

			   if (operand == "-")
			   {
				  string strPopped = stack.Pop().ToString();
				  currNum = int.Parse(strPopped);
				  prevNum = prevNum - currNum;

			   }

			   if (operand == "/")
			   {
				  string strPopped = stack.Pop().ToString();
				  currNum = int.Parse(strPopped);
				  prevNum = prevNum / currNum;

			   }

			   if (operand == "*")
			   {
				  string strPopped = stack.Pop().ToString();
				  currNum = int.Parse(strPopped);
				  prevNum = prevNum * currNum;

			   }
			} while (stack.Count > 0);
			answer.Text = prevNum.ToString();
			Input.Text = "";

		 }
		 catch (StackOverflowException)
		 {
			MessageBox.Show("Invalid Input");
		 }
		 catch (FormatException)
		 {
			MessageBox.Show("Invalid Input");
		 }

		 

	  }

   }


}





I now am finished w/ the stack calculator thnks to the help of the ones who answered my qeuries. my problem now is it does not have mdas rule(multiplication, div, add and subtraction)..it just calculate whatever the last operator placed in the stack..been working on this program( i know simple for others) and i apologigze for being stupid on this matter..i need some idea on how i can insert it to my code for operator precendence method to determine the sequence of operator processing..thnx!

Is This A Good Question/Topic? 0
  • +

Replies To: mdas calculator

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: mdas calculator

Posted 16 December 2008 - 07:31 PM

What is the format of the arithmetic sequence the user enters into the input box?
Was This Post Helpful? 0
  • +
  • -

#3 cs_starter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 08

Re: mdas calculator

Posted 17 December 2008 - 06:17 AM

ex. 6+3*5-1/2 (my calculator) = 22
when it should be (using MDAS rule) = 12.5..thnx btw, i have started making it but still would not run..or give me correct answer =(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1