Solved

applying operator to 2 numbers when operator is stored as character

Posted on 1998-10-26
7
204 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 300 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

777 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