Stacks C++

This is a discussion on Stacks C++ within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream>#include <stack> #include <string> using namespace std; void printMenu(); int evaluate(string exp); int main() { char choice; int ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Stacks C++

    Code:
    #include <iostream>#include <stack>
    #include <string>
    
    
    using namespace std;
    
    
    void printMenu();
    int evaluate(string exp);
    
    
    int main()
    {
           char choice;
           int result;
           string line;
    
    
           printMenu();
    
    
           choice = 'Z';
    
    
           do
           {
            cout << "What action would you like to perform?\n";
    
    
            cin.get(choice);
            cin.ignore(20, '\n'); //to flush '\n'
            choice = toupper(choice);
    
    
            switch(choice)
             {
              case 'E':
                   cout << "Enter an arithmetic expression:" << endl;
                   getline(cin, line);
                   result = evaluate(line);
                   cout << "The evaluated value is: " << result << endl;
                   break;
              case 'Q':   //Quit
                   break;
              case '?':   //Display Menu
                   printMenu();
                   break;
              default:
                   cout << "Unknown action\n";
                   break;
             }
            } while (choice != 'Q');
           return 0;
    }
    
    
    //Print the menu to a user
    void printMenu()
     {
            cout << "Choice\t\tAction\n";
            cout << "------\t\t------\n";
            cout << "E\t\tEnter an arithmetic expression to evaluate\n";
            cout << "Q\t\tQuit\n";
            cout << "?\t\tDisplay Help\n\n";
    
    
            return;
      }
    
    
    //The evaluate function takes a string containing an arithmetic expression,
    //evaluates it,and returns its result
    
    int evaluate(string exp)
     {
     stack<char> parStack;
     stack<int> numStack;
     stack<char> opStack;
    
    
     int j = exp.size();
     int i=0;
     char x;
    
    
     
     while (i<j)
     {
         if(exp[i] == '(')
         {
             parStack.push(exp[i]);
             cout << exp[i] << endl;  
         }
         if((exp[i]='0') || (exp[i]='1') || (exp[i]='2') || (exp[i]='3') || (exp[i]='4') || (exp[i]='5') || (exp[i]='6') || (exp[i]='7') || (exp[i]='8') || (exp[i]='9'))  
         {
             numStack.push(exp[i]);
         }
         if((exp[i] = '+') || (exp[i] = '-') || (exp[i] = '*') || (exp[i] = '/') || (exp[i] = '%'))
         {
             opStack.push(exp[i]);
         }
         i++;
     }
    I need to finish "int evaluate(string exp)" What am I missing to return the proper results? For example I need (((1+2)/3)*(6-4)) to return 2.



  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > if((exp[i]='0')
    Well you got == in the first one, thereafter, you used = where you should be using ==

    And also look up the isdigit() function in ctype.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Ok switched the = to == But still unsure of how to return my results.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Well the first thing to do is work out what to do with ')'

    Shunting-yard algorithm - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    You must first understand why the code you have is pushing things onto stacks, because although what you need to write might involve doing that, you're not doing it in any kind of overly purposeful manner.
    What I see so far is like someone wanting to make a car who thinks "hey cars have wheels, and wheels are round, I know I'll make a bunch of round things". I'm sorry, but that's truthfully just the feeling I get from looking at this code.

    You also need to post more information about your requirements. Do you only ever need to deal with single digit numbers, for example?
    Do you need to worry about operator precedence?
    Plus about another 20 questions that I'm not going to bother writing out, because hopefully you will just post the description of what you are meant to do. One example does not constitute a complete set of requirements.

    Have you looked up isdigit, and can you then see where it might be useful in place of something you have already?

    It could help to do some research about parsing, so that you get a feeel for what you need to do, and learn any relevant terminology that you don't know of yet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stacks?
    By Cloud6 in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2007, 02:47 PM
  2. Stacks
    By one dude in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 01:27 AM
  3. 'pop' stacks
    By student2005 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2004, 10:33 AM
  4. Stacks
    By hermit in forum C Programming
    Replies: 8
    Last Post: 06-17-2003, 07:23 PM
  5. Stacks
    By Kunzy in forum C++ Programming
    Replies: 7
    Last Post: 01-28-2003, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21