Προς το περιεχόμενο

Βοήθεια για multiple cpp αρχεία


JohnnysR

Προτεινόμενες αναρτήσεις

Δημοσ.

Οχι, και δεν έχω καν χρησιμοποιήσει ακόμη τα άλλα αρχεία..Αυτό είναι που με παραξενεύει...Ενώ θα έπρεπε να δουλεύει κανονικά δεν το κάνει..Προσπαθώ να το κάνω πρώτα να δουλέψει με το ένα αρχείο (1.cpp) αλλά δε βλέπω να βγαίνει άκρη...!

Δημοσ.

>
//data_structures.cpp
#include <iostream>
#include "01_if2pf.hpp"

using namespace std;

int main()
{
   menu();
return 0;
}

>
//01_if2pf.hpp
#include "01_if2pf.cpp"
int menu();

>
//01_if2pf.cpp
#include <iostream>
#include <string>

using namespace std;

class myStack{
   public:
       char* sp;
       int maxSize;
       int topp;
   myStack(int s){
       maxSize=s;
       sp=new char[maxSize+1];
       if (sp == NULL)
           cout<<"Memory problem";
       topp=0;
   }
   ~myStack(){
       delete sp;
   }
   void push(char value){
       if (topp < maxSize){
           sp[++topp]=value;
       }
       else
           cout<<"Stack Overflow";
   }
   char top(){
       if (topp>0)
           return sp[topp];
       else
           cout<<"Top when empty!"<<endl;
   }
   bool empty(){
       return (bool) (topp==0);
   }
    void pop(){
       if ( topp>0 )
           topp--;
       else
           cout<<"Oveflow"<<endl;
   }
};

int protereotita (char ch);
int telestis (char ch);
int telesteos(char ch);

int menu()
{
while(1){
   int i=0;
   char ch;

   string infix,postfix;
   myStack stack1(10);
   cout <<"Infix Expression:";
   cin >> infix;

   while(ch=infix[i++]){
       if ( telesteos(ch) )
           postfix.append(1, ch);
       else
           if ( telestis(ch) && !stack1.empty() ){
               char ch1=stack1.top();
               if ( protereotita(ch1) > protereotita(ch) ){ //a*b+c
                   stack1.pop();
                   if (ch1 != '(')
                       postfix.append(1,ch1);
                   stack1.push(ch);
               }
               if ( protereotita(ch1) == protereotita(ch) ){ //a+b+c
                   stack1.pop();
                   postfix.append(1,ch1);
                   stack1.push(ch);
               }
               if ( protereotita(ch1) < protereotita(ch) ) { //a+b*c
                   if ( ch!= ')' )
                       stack1.push(ch);
                   else{
                       postfix.append(1,stack1.top());
                       stack1.pop();
                   }
               }
           }
           else
               if ( telestis(ch) && stack1.empty() ){
                   stack1.push(ch);
               }
               else{
                   cout<<"Something is wrong with the input";
                   exit(-1);
               }
   }
   while ( !stack1.empty() ){
       postfix.append(1,stack1.top());
       stack1.pop();
   }
   cout<<"Postfix Expression:"<<postfix<<endl;
}
}
int protereotita (char ch)
{
   if ( ch == '^' )
       return 0;
   if ( (ch=='+') || (ch=='-') )
       return 1;
   if ( (ch=='*') || (ch=='/') )
       return 2;
   if (ch=='~')
       return 3;
   if ( (ch == '(') || ( ch == ')' ) )
       return 4;
}
int telestis (char ch)
{
   if ( (ch=='+') || (ch=='-') || (ch=='*') || (ch=='/') || (ch == '~') || (ch == '^')
                     || (ch == '(') || (ch == ')') )
       return 1;
   else
       return 0;
}
int telesteos(char ch)
{
   if ( (ch<= 'Z') && (ch>= 'A') )
       return 1;
   else
       return 0;
}

Δημοσ.

Χίλια συγγνώμη, στο hpp δεν πρέπει να κάνεις include το αντίστοιχο cpp. Επίσης, στο cpp φαίνεται πως παίζει ρόλο η σχετική θέση του κώδικα των συναρτήσεων, πρέπει η menu να μπει τελευταία:

 

>//01_if2pf.hpp
int protereotita(char);
int telestis(char);
int telesteos(char);
int menu(void);
class myStack;

 

>//01_if2pf.cpp
#include <iostream>
#include <string>

using namespace std;

class myStack{
   public:
       char* sp;
       int maxSize;
       int topp;
   myStack(int s){
       maxSize=s;
       sp=new char[maxSize+1];
       if (sp == NULL)
           cout<<"Memory problem";
       topp=0;
   }
   ~myStack(){
       delete sp;
   }
   void push(char value){
       if (topp < maxSize){
           sp[++topp]=value;
       }
       else
           cout<<"Stack Overflow";
   }
   char top(){
       if (topp>0)
           return sp[topp];
       else
           cout<<"Top when empty!"<<endl;
   }
   bool empty(){
       return (bool) (topp==0);
   }
    void pop(){
       if ( topp>0 )
           topp--;
       else
           cout<<"Oveflow"<<endl;
   }
};

int protereotita(char ch)
{
   if ( ch == '^' )
       return 0;
   if ( (ch=='+') || (ch=='-') )
       return 1;
   if ( (ch=='*') || (ch=='/') )
       return 2;
   if (ch=='~')
       return 3;
   if ( (ch == '(') || ( ch == ')' ) )
       return 4;
}
int telestis(char ch)
{
   if ( (ch=='+') || (ch=='-') || (ch=='*') || (ch=='/') || (ch == '~') || (ch == '^')
                     || (ch == '(') || (ch == ')') )
       return 1;
   else
       return 0;
}
int telesteos(char ch)
{
   if ( (ch<= 'Z') && (ch>= 'A') )
       return 1;
   else
       return 0;
}

int menu(void)
{
while(1){
   int i=0;
   char ch;

   string infix,postfix;
   myStack stack1(10);
   cout <<"Infix Expression:";
   cin >> infix;

   while(ch=infix[i++]){
       if ( telesteos(ch) )
           postfix.append(1, ch);
       else
           if ( telestis(ch) && !stack1.empty() ){
               char ch1=stack1.top();
               if ( protereotita(ch1) > protereotita(ch) ){ //a*b+c
                   stack1.pop();
                   if (ch1 != '(')
                       postfix.append(1,ch1);
                   stack1.push(ch);
               }
               if ( protereotita(ch1) == protereotita(ch) ){ //a+b+c
                   stack1.pop();
                   postfix.append(1,ch1);
                   stack1.push(ch);
               }
               if ( protereotita(ch1) < protereotita(ch) ) { //a+b*c
                   if ( ch!= ')' )
                       stack1.push(ch);
                   else{
                       postfix.append(1,stack1.top());
                       stack1.pop();
                   }
               }
           }
           else
               if ( telestis(ch) && stack1.empty() ){
                   stack1.push(ch);
               }
               else{
                   cout<<"Something is wrong with the input";
                   exit(-1);
               }
   }
   while ( !stack1.empty() ){
       postfix.append(1,stack1.top());
       stack1.pop();
   }
   cout<<"Postfix Expression:"<<postfix<<endl;
}
}

 

 

Ενδιαφέρον φαίνεται πάντως, τί είναι; Κάποιου είδους συντακτικός αναλυτής;

Δημοσ.

Είσαι αρχηγός!

Φίλε μου ευχαριστώ πολύ για τον χρόνο σου, δουλεύει μια χαρούλα :)

Είναι ο υπολογισμός της επιθεματικής μορφής μιας ενδοθεματικής έκφρασης...(Από infix σε postfix)

 

και πάλι thanks! Καλο βράδυ!

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...