Solved

applying operator to 2 numbers when operator is stored as character

Posted on 1998-10-26
7
201 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

746 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

7 Experts available now in Live!

Get 1:1 Help Now