jeudi 5 octobre 2017

stack implementation in c++ CALCULATOR program

I am new in c++. I am trying to implement a stack-based CALCULATOR which would be able to take input as follow:

Input: A fully parenthesized expression E, where each operand is a single digit in {0, 1, …, 9}, and three operators are + (binary addition), * (binary multiplication), and ^ (binary power). You can assume that input is correct and fully parenthesized

?:

Output: Equivalent postfix expression to E, and the result of evaluating E in modulo 10.

Example input: (((2+(5^2))+7)

Answer: 252^+7+ 4 in modulo 10

I am trying to implement using following sample codes but I don't know what is the next to complete this program:

#include <iostream>
#include <string.h>

using namespace std;
class STACK 
{
private:
  char *s; int N;
public:
  STACK(int maxN)
    { s = new char[maxN]; N = 0; }
  int empty() const
    { return N == 0; }
  void push(char item)
    { s[N++] = item; }
  char pop()
    { return s[--N]; }
};

int main() {

 char a[]; //read from the input
 int N = strlen(a);
 STACK ops(N);
 for (int i = 0; i < N; i++)
  {
    if (a[i] == ')')
      cout << ops.pop() << " ";
    if ((a[i] == '+') || (a[i] == '*')) 
      ops.push(a[i]);
    if ((a[i] >= '0') && (a[i] <= '9')) 
      cout << a[i] << " ";
  } 

  cout << endl;
 } 

It would be very helpful if you help me to implement this program.

Aucun commentaire:

Enregistrer un commentaire