no cmq il problema lo dà su tutte le funzioni membro
stack.h
codice:
#include <iostream>
#include <cstdlib>
using namespace std;
template <class item>
class stack
{
private:
item *s;
int n;
int max;
public:
// ---------------------------------------------------------
// Costruttore della classe
// Parametro => Capienza massima dello stack (unità)
// ---------------------------------------------------------
stack (int);
// ---------------------------------------------------------
// Verifica se lo stack è vuoto
// Valori restituiti => 0 (non vuoto) , 1 (vuoto)
// ---------------------------------------------------------
bool empty() const;
// ---------------------------------------------------------
// Inserisce in cima allo stack
// Parametro => Oggetto da inserire
// Valore restituito => 1 (push eseguito) , 0 (stack pieno)
// ---------------------------------------------------------
bool push (item obj);
// ---------------------------------------------------------
// Preleva dallo stack
// Valore restituito => Oggetto prelevato
// ---------------------------------------------------------
item pop();
};
stack.cpp
codice:
#include "stack.h"
#include <iostream>
#include <cstdlib>
using namespace std;
template <class item>
stack<item>::stack (int num)
{
s = new item[num];
n = 0;
max = num;
}
template <class item>
bool stack<item>::empty() const
{
return n == 0;
}
template <class item>
bool stack<item>::push (item obj)
{
if (n == max)
return false;
s[n] = obj;
n++;
return true;
}
template <class item>
item stack<item>::pop()
{
if (n == 0)
return 0;
n--;
return s[n];
}
main.cpp
codice:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "stack.h"
using namespace std;
int valPost (char* exp);
int main(int argc, char *argv[])
{
char str[100];
cin.getline (str,100,'\n');
cout << valPost (str);
system("PAUSE");
return EXIT_SUCCESS;
}
int valPost (char* a)
{
stack<int> sp(strlen(a));
for (int i = 0; i < strlen(a); i++)
{
if (a[i] == '+')
sp.push (sp.pop() + sp.pop());
else if (a[i] == '*')
sp.push (sp.pop() * sp.pop());
else if (a[i] == '-')
sp.push (-(sp.pop() - sp.pop()));
else if (a[i] == '/')
{
int d2 = sp.pop();
int d1 = sp.pop();
sp.push (d1 / d2);
}
else if (a[i] == '%')
{
int d2 = sp.pop();
int d1 = sp.pop();
sp.push (d1 % d2);
}
else if (a[i] == '^')
{
int p2 = sp.pop();
int p1 = sp.pop();
sp.push ((int)pow((float)p1,(int)p2));
}
else if (a[i] >= '0' && a[i] <= '9')
{
sp.push (0);
while (a[i] >= '0' && a[i] <= '9')
{
sp.push (10*sp.pop() + (a[i++]-'0'));
}
}
}
return sp.pop();
}