Das verrückte Rekursive Programm



  • Es ist mittwoch und meine kumpels sind nciht daheim, mir ist totlangweilig,
    soviel zu meiner Situation woraus dieses Programm entstand:

    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    char* itoa(int val, int base){	
       static char buf[32] = {0};	
       int i = 30;	
       for(; val && i ; --i, val /= base)	
          buf[i] = "0123456789abcdef"[val % base];	
       return &buf[i+1];
    }
    
    int main(int argc, char *argv[]) {
    
       int zahl = 0,zahl2 = 0;
       char string1[255] = "./hard ";
    
       //Werte überprüfen
       if(argc < 1)
          return 0;
       zahl = atoi(argv[1]);
       if(zahl == 1) {
          cout << zahl<< endl;                   //hier ist die zahl noch 1           
          return zahl;                           //<- hier wird die Zahl losgeschickt über return zum system aufruf
       }
    
       //String vorbereiten
       char *string2 = itoa(zahl-1,10);
       int max = strlen(string2);
       for(int x = 0; x <= max;x++) {
          string1[7+x] = string2[x];
       }
       cout << string1 << endl;
    
       //Rekursion aufrufen
       zahl2 = system(string1);            //<- hier wird die zahl2 empfangen und
       cout << zahl2 << endl;              //<- ausgegeben jetzt ist sie 256
       //cout << zahl*zahl2 << endl;
       return zahl*zahl2;
    
    }
    

    Das Programm soll die Fakultät einer Zahl erechnen indem es sich selbst,
    in einer Rekusion per system() wieder aufruft. Die zahl wird dabei per Kommandozeile übergeben.
    Es ist nciht der schönste code aber er funktioniert so weit ganz gut.

    Nun zum Problem:
    Statt der Zahl die das Programm zurückgeben soll erhalte ich 256 von system zurück.
    Ganz leicht nachzuprüfen indem man das Programm übersetzt

    g++ rekur.cpp -o hard
    

    und ausführt

    storm@localhost ~/C-C++/Aufgaben/Kommandozeile $ ./hard 2
    ./hard 1
    1
    256
    

    Wie man sieht funktioniert die Rekursion, und das Programm gibt 1 zurück,
    plötzlich wird aber aus dem 1 eine 256, warum?



  • Der Returncode von System kapselt ein zwei Byte Feld, wobei das linke Byte den Returncode enthält, und das rechte Byte Informationen über die Art der Beendigung (Normal, Crash, usw).

    In Deinem Fall ist der Returncode 0x0100, also 1 als Returncode und 0 als Indikator für "aus eigener Kraft beendet".

    Unter Linux gibt es dafür auch die Makros WEXITSTATUS(x) und WTERMSIG(x)...

    Da in meiner Manpage steht "Conforming to ANSI C, POSIX", weiss ich leider nicht welche Teile meiner Erläuterung Implementierungsspezifisch sind 😞



  • Es funktioniert:
    hier das endgültige Programm:

    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    char* itoa(int val, int base){	
       static char buf[32] = {0};	
       int i = 30;	
       for(; val && i ; --i, val /= base)	
          buf[i] = "0123456789abcdef"[val % base];	
       return &buf[i+1];
    }
    
    int main(int argc, char *argv[]) {
    
       int zahl = 0,zahl2 = 0;
       char string1[255] = "./hard ";
    
       //Werte überprüfen
       if(argc < 1)
          return 0;
       zahl = atoi(argv[1]);
       if(zahl == 1) {
          cout << zahl<< endl;
          return zahl;
       }
    
       //String vorbereiten
       char *string2 = itoa(zahl-1,10);
       int max = strlen(string2);
       for(int x = 0; x <= max;x++) {
          string1[7+x] = string2[x];
       }
       cout << string1 << endl;
    
       //Rekursion aufrufen
       zahl2 = system(string1);
       zahl2 = zahl2 >> 8;
       cout << zahl*zahl2 << endl;
       return zahl*zahl2;
    
    }
    

    Thx @ LordJaxom


Anmelden zum Antworten