Solved

Code for calculator

Posted on 2007-11-26
7
754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 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