samedi 25 avril 2015

Stack based console application


in my console application I would check if the parenthesis balance of the equation in input is ok, but i get this error: "expected '}' at end of input" reffered to line 28 of the main(is the last line of the main).

this is code:

MAIN.CPP
#include <iostream> 
#include "Stack.h"
using namespace std;

int main() {
    char input, output;
    Stack s;
    while(input!=';'){
        cin>>input;
        if(input=='(' || input=='[' || input=='{')
            s.Push(input);
            if(input==')' || input==']' || input=='}'){
                output=s.Pop();
                        if(input==')' && output=='(' || input==']' && output==']' || input=='}' && output=='{')
                        cout<<"l'equazione è corretta";
                        else
                            cout<<"l'equazione è errata";
                }
    return 0;
    }


STACK.H
#ifndef STACK_H_
#define STACK_H_

namespace std {

class Stack
{
private:

    public:
            struct node {
                struct node *next ; /* puntatore nodo successivo */
                char data ; /* Dati contenuti nel nodo */
            };
        node *successivo;
        node *top;
        node *temp;

    Stack();
    void Push(char n);  //inserimento
    char Pop();  //estrazione
};

#endif /* STACK_H_ */

STACK.CPP
#include "stack.h"
#include <stddef.h>

using namespace std;

Stack::Stack(){
top = NULL;
successivo = NULL;
temp = NULL;
}

void Stack::Push(char n){
node *elem;
elem->data = n;
elem->next = top;
top = elem;
}

char Stack::Pop(){
char returned;
if(top != NULL){
    returned = top->data;
    temp = top->next;
    delete top;
    top = temp;
    }
    else
    returned = -1;
return returned;
}

Can anyone help me? Thanks in advance


Aucun commentaire:

Enregistrer un commentaire