Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

applying operator to 2 numbers when operator is stored as character

Posted on 1998-10-26
7
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

670 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