Solved

applying operator to 2 numbers when operator is stored as character

Posted on 1998-10-26
7
205 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 75
How to convert MFC APP to Win32 APP. 19 98
Better understanding on C++ Class serialization and formats 9 80
Safe conversion? 4 72
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

820 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