# mdas calculator

Page 1 of 1

## 2 Replies - 12751 Views - Last Post: 17 December 2008 - 06:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=76960&amp;s=dcfed7280106f059a31bab2bc4edf98a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cs_starter

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

# mdas calculator

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);
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

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• 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?

### #3 cs_starter

Reputation: 0
• 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 =(