Solved

Code for calculator

Posted on 2007-11-26
7
750 Views
Last Modified: 2010-08-05
Hello,
I need code for a c++ calculator that is based on a stack data structure. It must be able to evaluate expressions like :

( 3 * 4) + ( 6 + 2)
3 + 4
(6 *9) / (2 * 4)

Thank you
0
Comment
Question by:kk_hutton
7 Comments
 
LVL 5

Expert Comment

by:abith
ID: 20348925
refer the link http://www.programmersheaven.com/download/16076/download.aspx
it uses infix. if you are familer with tree traversal methods, then you can try with prefix and post fix aswell
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20350155
In the 'Annotated C++ Reference' of Bjarne Stroustrup there was a calculator sample. It was 15 years ago where I enhanced it to a class based sample and add some more functions and operand handling. But the basic sample was rather complete and it should not be difficult to google for it.

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20350185
BTW, is it a homework assignment? EE experts may not post full code samples for homework. So, if it is homework I only could help if you show some attempts of you to solve the problem yourself.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kk_hutton
ID: 20352119
based on my other post you can probably see i attempted  and I couldnt get it to work. thats why im paying to get some help.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 20352818
>>>> thats why im paying to get some help.
Nevertheless there are the EE rules:

   - no full code to homework questions.
   - a maximum of 500 points for one question

As experts don't get anything from your money, you hardly can buy anyone over ;-)

I looked into the other thread. Where did you get the starter code from? The problem with code that wasn't designed by yourself is, that you hardly can get it corrected.

>>>> In addition,  this program must use a stack to implement the calculation
You could do it without explicit stack by using recursive calls. That way C++ makes the stack (call stack). Is the Stack class you used from a previous project? If you have to use that class you nevertheless should adopt it to your purposes. It shouldn't collect char strings but 'tokens'. A 'token' can be any operator or operand or delimiter. You best make a enum to 'classify' your tokens:

  enum CalcToken { INITIAL, PAR_OPEN, PAR_CLOSE, PLUS, MINUS, STAR, SLASH, NUMBER, ...., MAX_TOKEN };

with the 'MAX_TOKEN' which must be last, you have an automatic 'count' of all tokens which can be used like

  char* szToken[MAX_TOKEN] = { "", "(", ")", "+", "-", "*", "/", .... };

I made it strings rather than single chars in case you will ahve operators with more than one character.

You then can make

struct Token
{
      CalcToken tokentype;
      double value;
};

Normally I would derive Operand, Operator, Delimiter from struct Token, but I don't want to confuse you. You could use the struct Token as data for the Stack. Whenever you parsed a token (by strtok) you decide

  - whether to add it to a new Stack
  - whether you can calculate teh current Stack,
     close it and put the result as Operand token
     to the previous Stack
  - whether to add it to the current Stack

E. g.  

    3 * 66 + (2 + 5)

1. should create a new Stack and add NUMBER token

  S[0]: { NUMBER 3 }

2. Add  STAR token
 
  S[0]: { NUMBER 3 }   { STAR }

3. Add NUMBER token

  S[0]: { NUMBER 3 }   { STAR }  { NUMBER 66}

4. When recognizing the '+'

4a)  evaluate Stack 0: "3 * 66" by popping the tokens from the stack and
       use a switch statement to do the appropriate action

    double result = 0;
    CalcToken oper = INITIAL;
    switch (token.tokentype)
    {
        case NUMBER:
        {
              switch (oper)
              {
                  case INITIAL: result = token.value; break;
                  ...
                  case STAR:    result *= token.value; break;
              }
        }
        break;  
        case PLUS:    oper = PLUS;  break;
        case STAR:    oper = STAR;  break;
        ...
   }    

Finally the current Stack can be deleted and the result must be added to the previous stack. If there is no previous stack, create a new stack and put the result as initial token ...

In case of a parantheses you open a new stack and wait for the closing parantheses ... and so on. Finally the result of out last stack is the final result.

Regards, Alex

 
0
 

Author Comment

by:kk_hutton
ID: 20353236
Actually I wrote the starter code from scratch and on my own. Believe it or not,  Im not trying to "buy" anyone over, Im trying to get help. I asked for help on the parenthesis part, and people suggested I wrote it using different methods I don't know how to write. Please don't make assumptions. Thanks for your help
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20376857
Alex is right!

If this is homework then we are not allowed to post full code examples. That's EE rules, we don't make them but also we don't break them. Look at the code you already have, identify what you don't understand and ask specific questions here. You'll find EE experts will be only too willing to assist with specific problems but not one of them will post you full code -- no  matter how many times you say you've tried but can't do it! If you are that stuck you'll need to refer back to your professor.

-Rx.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now