logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Błędy kompilacji w programie kalkulatora - analiza kodu C++

kolahawk 10 Sty 2010 12:42 1596 4
REKLAMA
  • #1 7513797
    kolahawk
    Poziom 10  
    Posty: 23
    Ocena: 2
    Witam,
    Mam taki program, niestety nie chce mi się skompilować i nie wiem czemu...:-( Może ktoś wie gdzie są jakieś błędy i mi pomoże :-) Z góry dziękuję :-)
    Oto kod do tego programu:
    
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    
    using namespace std;
    
    static int count_number  (0);   //do przydzielenia tablicy liczb  na stos
    static int count_char    (0);     //do przydzielenia tablicy znakow na stos
    static int open_brackets (0);
    
    typedef long double typ_danej;      //typ danych dla liczb
    
    #define PI 3.1415926535897932385
    
    static int error (0);
    
    int priorytet (char wejscie, char stos) { //# - dno stosu
     
      if (stos=='#' || stos=='(') return 1;
      
      if (wejscie==')' || wejscie=='(') return 1;
      
      switch (wejscie) {
       case '+':       
       case '-': return 0;  
       case '*':
       case '/': return (stos=='+' || stos=='-') ? 1 : 0;  
       case '^': return (stos=='+' || stos=='-' || stos=='*' || stos=='/') ? 1 : 0;
       case 'C':  //cosinus
       case 'S':  //sinus   
       case 'T':  //tanges  
       case 'N':  //negacja
       case 'l':  //logarytm o podstawie 10
       case 'L':  //logarytm naturalny       
       case '%': return 1;          
      }       
      return 0;  
    } 
    
    bool przelicz (char znak, typ_danej *stos_liczba, int &pos_stos_liczba) {
     
     switch (znak) {
        case '+':
          cout << *(stos_liczba-2) << "+" << *(stos_liczba-1);   
          *(stos_liczba-2) += *(stos_liczba-1); 
          cout << "=" << *(stos_liczba-2) << endl;        
          pos_stos_liczba--;
          break;
        case '-':
          cout << *(stos_liczba-2) << "-" << *(stos_liczba-1);   
          *(stos_liczba-2) -= *(stos_liczba-1); 
          cout << "=" << *(stos_liczba-2) << endl;      
          pos_stos_liczba--;
          break; 
        case '*':
          cout << *(stos_liczba-2) << "*" << *(stos_liczba-1);   
          *(stos_liczba-2) *= *(stos_liczba-1); 
          cout << "=" << *(stos_liczba-2) << endl;   
          pos_stos_liczba--;
          break; 
        case '/':
          cout << *(stos_liczba-2) << "/" << *(stos_liczba-1);
          if (*(stos_liczba-1)!=0) {   
            *(stos_liczba-2) /= *(stos_liczba-1); 
            cout << "=" << *(stos_liczba-2) << endl;
          } else {
            cout << "= blad: dzielenie przez zero!!!" << endl; 
            error = 1;        
            return false;
          }  
          pos_stos_liczba--;
          break;
        case '^':
          cout << *(stos_liczba-2) << "^" << *(stos_liczba-1);   
          *(stos_liczba-2) = pow(*(stos_liczba-2),*(stos_liczba-1)); 
          cout << "=" << *(stos_liczba-2) << endl;   
          pos_stos_liczba--;
          break;        
        case 'N':   
          *(stos_liczba-1) *= -1;       
          break; 
        case '%':
          *(stos_liczba-1) /= 100;       
          break;
        case 'S':
          *(stos_liczba-1) = sin(*(stos_liczba-1) * PI / 180);       
          break;  
        case 'C':
          *(stos_liczba-1) = cos(*(stos_liczba-1) * PI / 180);       
          break; 
        case 'T':
          *(stos_liczba-1) = tan(*(stos_liczba-1) * PI / 180);       
          break;    
        case 'l':
          if (*(stos_liczba-1)>0) {    
            *(stos_liczba-1) = log10(*(stos_liczba-1));       
          } else {
            cout << "= blad: liczba logarytmowana musi byc wieksza od zera!!!" << endl; 
            error = 2;        
            return false;
          }       
          break;    
        case 'L':
          if (*(stos_liczba-1)>0) {      
            *(stos_liczba-1) = log(*(stos_liczba-1));       
          } else {
            cout << "= blad: liczba logarytmowana musi byc wieksza od zera!!!" << endl;         
            error = 2;
            return false;
          }      
          break;         
       }    
       return true;  
         
    }
                
    typ_danej oblicz(string &s) {
     
     
     typ_danej liczba (0);
     
     char stos_char[count_char];
     char last_;
     int pos_stos_char (0);
     
     typ_danej stos_liczba[count_char * 2];
     int pos_stos_liczba (0);
     
     while (s.length()>0) {
                
       if (isdigit(s[0]) || s[0]=='P') {
         
         if (s[0]=='P') {
           liczba = PI;  
           s.erase(0,1);           
         } else {
           liczba = atof(s.c_str());
           while (isdigit(s[0]) || s[0]=='.') s.erase(0,1);  
         }
         stos_liczba[pos_stos_liczba] = liczba;
         pos_stos_liczba++;
                      
       } else {
         
         if (s[0]!=')') {
           if (pos_stos_char>0) {
             last_ = stos_char[pos_stos_char - 1];                   
           } else last_ = '#';
           } else last_ = ')';    
         
         while(priorytet(s[0],last_)==0) {
           
           if (!przelicz(last_,&stos_liczba[pos_stos_liczba],pos_stos_liczba)) {
             return 0;
           }                                         
           pos_stos_char--;
            
           if (pos_stos_char>0) {
             last_ = stos_char[pos_stos_char - 1];                   
           } else last_ = '#';                               
         }                                                  
         
         if (last_!=')') {
           stos_char[pos_stos_char] = s[0];
           pos_stos_char++;
         } else {
                
          while (stos_char[pos_stos_char-1]!='(') {
                
            if (!przelicz(stos_char[pos_stos_char-1],&stos_liczba[pos_stos_liczba],pos_stos_liczba)) {
              return 0;
            }                                                                                             
            pos_stos_char--; 
                 
          }
          pos_stos_char--;
         }       
              
         s.erase(0,1);      
       
       }             
     }
     
    
     while (pos_stos_char>0) {
     
       if (!przelicz(stos_char[pos_stos_char-1],&stos_liczba[pos_stos_liczba],pos_stos_liczba)) {
         return 0;
       }                                                                                                
       pos_stos_char--;
           
     }
    
     return stos_liczba[pos_stos_liczba-1];
    }          
                
    int weryfikacja (string &s) {
    
      char last_ (0); 
      
      int cn (0);
     
      for (int i = 0, c = s.length(); i < c; i++)
      {
        cn ++;  
        if (!isdigit(s[i])) {
                            
          switch(s[i]) { 
            case 'c':
            case 'C':
                 if (i+5>c) return i;
                 if (toupper(s[i+1])=='O' && toupper(s[i+2])=='S' && toupper(s[i+3])=='(') {
                   s[i]='C'; s.erase(i+1,2); c-=2;                      
                                          
                 } else return i;
                 break; 
            case 's':
            case 'S':
                 if (i+5>c) return i;
                 if (toupper(s[i+1])=='I' && toupper(s[i+2])=='N' && toupper(s[i+3])=='(') {
                   s[i]='S'; s.erase(i+1,2); c-=2;                      
                                          
                 } else return i;
                 break;
            case 't':
            case 'T':
                 if (i+5>c) return i;
                 if (toupper(s[i+1])=='A' && toupper(s[i+2])=='N' && toupper(s[i+3])=='(') {
                   s[i]='T'; s.erase(i+1,2); c-=2;                      
                                          
                 } else return i;
                 break;                           
            case 'l':
            case 'L':
                 if (i+3>=c) return i;
                 if (toupper(s[i+1])=='N' && s[i+2]=='(') {               
                  s[i]='L'; s.erase(i+1,1);  c--;                      
                 } else if (toupper(s[i+1])=='O' && toupper(s[i+2])=='G' && toupper(s[i+3])=='(') {
                          s[i]='l'; s.erase(i+1,2);  c-=2;        
                        } else return i;
                 break; 
            case 'p':
            case 'P':
                 if (i+1>c) return i;
                 if (toupper(s[i+1])=='I' && !isdigit(s[i+2])) {
                  s[i]='P'; s.erase(i+1,1);  c--;                         
                 } else return i;     
                 break;                                     
            case '%':
                 if (i==0) return i;
                 if (!isdigit(s[i-1]) && isdigit(s[i+1])) return i;
                 break;                           
            case ')':
                 open_brackets--;
                 if (i<c-1) {
                   if ((isdigit(s[i+1])) || toupper(s[i+1])=='S' || toupper(s[i+1])=='C' || toupper(s[i+1])=='T' || toupper(s[i+1])=='P') {
                     s.insert(i+1,"*");
                     count_char++;
                     i++; c++;                     
                   } 
                 } 
                 break;           
            case '(':
                 open_brackets++;
                 if (i>0)
                   if (isdigit(s[i-1])) {
                     s.insert(i,"*");
                     count_char++;
                     i++; c++;                 
                   }       
                 count_char++;                
                 break; 
            case '^':
                 if (i>0 && i<c-1) {
                   if (!isdigit(last_) && last_!=')' && !isdigit(s[i+1]) && s[i+1]!='(') return i;     
                 } else return i;
                 break; 
            case ',':
                 s[i] = '.';                   
            case '.':
                 if (i>0 && i<c-1) {
                   if (!isdigit(last_) && !isdigit(s[i+1])) return i;     
                 } else return i;
                 break;
            case '-': 
                 if (last_=='(' || i==0) {
                   s[i] = 'N';
                   count_char++;
                   break;
                 }
            case '/':     
            case '*':
            case '+':
                 if (!isdigit(last_) && last_!=')' && last_!='%' && last_!='P') return i; else count_char++;        
                 break;
            default:    
                 return i;                
          }     
                     
        }                    
       
        last_ = s[i];
      } 
      return -1;     
    }    
    
    
    int main(int argc, char *argv[])
    {
        
        cout << " cos(x)   - cosinus z liczby x" << endl;
        cout << " sin(x)   - sinus z liczby x" << endl;
        cout << " tan(x)   - tanges z liczby x" << endl;
        cout << " log(x)   - logarytm o podstawie 10 z liczby x" << endl;
        cout << " ln(x)    - logarytm naturalny z liczby x" << endl;
        cout << "            ln(x)/ln(y) = logarytm o podstawie y z liczby x" << endl;
        cout << " pi       - stala pi" << endl;
        cout << " x%       - procent danej liczby x (x * 1/100)" << endl;
        
        cout << endl;        
        string s;
        
        cout << "podaj rownanie: ";
        cin >> s;
        
        if (s=="q") return 0;
        
        int i = weryfikacja(s);
        if (i>-1) {
          cout << "Blad skladni w pozycji " << i+1 << " .Nieprawdlowy znak, lub jego pozycja: " << s[i] << endl;       
        } else { 
         if (open_brackets<0) cout << "brak nawiasu otwierajacego" << endl; else                
         if (open_brackets>0) cout << "brak nawiasu zamykajacego" << endl; 
         else {
               cout << endl;
               typ_danej wynik = oblicz(s);
               if (error==0) cout << "wynik: " << wynik << "\n\n"; else cout << "\n\n";
              }
        }
        
        
               
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
  • REKLAMA
  • #2 7513844
    Loker
    Poziom 39  
    Posty: 3368
    Pomógł: 575
    Ocena: 720
    A co "mówi" kompilator?
  • REKLAMA
  • #3 7513906
    kolahawk
    Poziom 10  
    Posty: 23
    Ocena: 2
    Podkreśla mi tą linię:
       cout << *(stos_liczba-2) << "+" << *(stos_liczba-1);   


    A to pokazuje kompilator:

     In function `bool przelicz(char, typ_danej*, int&)': 
    44  `cout' undeclared (first use this function) 
      (Each undeclared identifier is reported only once for each function it appears in.) 
    46  `endl' undeclared (first use this function) 
    46  At global scope: 
    117  `string' was not declared in this scope 
    117  `s' was not declared in this scope 
    117  expected `,' or `;' before '{' token 
    197  `string' was not declared in this scope 
    197  `s' was not declared in this scope 
    197  expected `,' or `;' before '{' token 
     In function `int main(int, char**)': 
    311  `cout' undeclared (first use this function) 
    311  `endl' undeclared (first use this function) 
    321  `string' undeclared (first use this function) 
    321  expected `;' before "s" 
    324  `cin' undeclared (first use this function) 
    324  `s' undeclared (first use this function) 
    328  `weryfikacja' cannot be used as a function 
    336  `oblicz' cannot be used as a function 
    343  `system' undeclared (first use this function) 
    344 D:\Dev-Cpp\zaliczenie kalkulator.cpp `EXIT_SUCCESS' undeclared (first use this function) 
    
  • REKLAMA
  • #5 7514228
    kolahawk
    Poziom 10  
    Posty: 23
    Ocena: 2
    haha...wogóle zapomniałem o tym xP
    dzięki wielkie :D Już działa :-)
REKLAMA