Solved

Code for calculator

Posted on 2007-11-26
7
749 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maze travler 6 47
Problem to ASCII 1 148
How to convert MFC APP to Win32 APP. 19 55
Eclipse IDE - Cannot copy/paste from console output 8 71
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

9 Experts available now in Live!

Get 1:1 Help Now