Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

applying operator to 2 numbers when operator is stored as character

Posted on 1998-10-26
7
Medium Priority
?
214 Views
Last Modified: 2010-04-02
The following is code for a class assignment. I get the Fully Parenthesised Expression in string FPE with no problems.  The second section of assignment is to utilize the following algorithm to evaluate the expression.  
1. if it is an operand, push on to the stack.
2. if it is an operator, pop two values from the stack and apply operator and push the value.

I don't see how it is possible to apply the operator which is a character, to the 2 operands that I have popped off the stack.  Any feedback is greatly appreciated.
#include <iostream.h>
#include <string.h>
#include "A:\class\bstack.h"
#include <ctype.h>
typedef char string[40];

int main()
{
      string expression, FPE;
      unsigned i = 0, j = 0;
      int x = 0, y = 0, z = 0;
      char temp;
      cout << "Please enter a Fully Parenthesised Expression. \n";
      cout << "For example, ((2 + 4 ) * 3). \n\n";
      cin.getline(expression, 39);
      IntStack stk;
      
      while (i < strlen(expression))
      {
            if ((expression[i] == '+' ) || (expression[i] == '-' ) || (expression[i] == '/' )||
                  (expression[i] == '*'))
            {
                  stk.Push(expression[i]);            
                  
            }
      
            else if (isdigit(expression[i]) != 0 )
            {
                  FPE[j] = expression[i];
                  
                  ++j;
            }
            else if (expression[i] == ')')
            {
                  expression[i] = stk.Top();
                  
                  FPE[j] = expression[i];
                  ++j;
                  stk.Pop();
            }
      ++i;
      }

cout << " \nThe Fully Parenthisised Expression is:   " << FPE << "\n\n" ;
j = 0;
while (j < strlen(FPE))            // FPE is my Fully Parenthesised Expression
{
      if(isdigit(FPE[j]) != 0)
      {
            stk.Push(FPE[j]);  // pushing the operands here
            
      }
      else
      {
            x = char (stk.Top());
            cout << x << endl;
            stk.Pop();
            y = char(stk.Top());
            cout << y << endl;
            stk.Pop();
            temp = (FPE[j]);
            z = x temp y;
            cout << z << '\n';

            cout << temp;
            
            stk.Push(z);
      }
      ++j;
      }
      return 0;
}


0
Comment
Question by:strev
  • 5
  • 2
7 Comments
 

Author Comment

by:strev
ID: 1176158
Adjusted points to 300
0
 
LVL 22

Accepted Solution

by:
nietod earned 1200 total points
ID: 1176159
>> don't see how it is possible to apply the operator which is a
>>character, to the 2 operands that I have popped off the stack.

Use a switch statement.  Details follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176160
switch (operator)
{
    case '+' :
      result = op1 + op2;
      break;
    case '-' :
      result = op1 - op2;
      break;
    case '*' :
      result = op1 * op2;
      break;
    case '/' :
      result = op1 / op2;
      break;
}

stk.Push(result)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1176161
>>  1. if it is an operand, push on to the stack.
>>   2. if it is an operator, pop two values from the stack and apply
>>   operator and push the value

I beleive this is the algorithm for a post fix expresion.  Not an infix expression.
An infix expression would have the operator in between the operands, like
 
4+5

Your algorithm won't work with that expresion.  It works with a postfix one, where the operator comes after the two operands.  like

4 5 +

I think you are missing a step in the project.  You probably are supposed to convert infix to postfix before trying to calculate.  Look over your assignment again.  If you have questions, let me know.
0
 

Author Comment

by:strev
ID: 1176162
thank you for your response, however, having applied your suggestion, the result is as follows:

(2 + 3 )     //user entered expression

result = 101

which are the ascii conversions, anyway you know to accomplish conversion so that result is the integer 5?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176163
I'm not sure what you asking about.  I think I need to see your code.  Can you e-mail it to me?  my address is nietod@theshop.net.  Also how soon until this is due?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176164
Your problem is sort of two fold.  First of all you can easily convert a digit character (like '5') to a number,  You just subtract the value of a zero digit ('0' not 0) from it like

char Ch5 = '5'
int Int5 = Ch5 - '0';  // This integer will be 5.

You could apply that logic to your program.  That would work in your current program and in the examples you tested.  However your program (and that method) won't work for numbers that are multiple characters.  I assume you need to be able to handle expressions like

123 + 56

right?  Your current logic won't handle that at all.  when you come to a digit in your processing, you probably should use atoi() to convert the entire number (maybe several digits) to an itneger and then work with the integer.  If you need to convert back to a string again, you can use
itoa() to convert back.

let me know iof this helps.  If you need to send me another co;y make sure to send your bstack files..  Taht way I can compile and test your code.
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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

824 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