<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Das verrückte Rekursive Programm]]></title><description><![CDATA[<p>Es ist mittwoch und meine kumpels sind nciht daheim, mir ist totlangweilig,<br />
soviel zu meiner Situation woraus dieses Programm entstand:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;cstring&gt;

using namespace std;

char* itoa(int val, int base){	
   static char buf[32] = {0};	
   int i = 30;	
   for(; val &amp;&amp; i ; --i, val /= base)	
      buf[i] = &quot;0123456789abcdef&quot;[val % base];	
   return &amp;buf[i+1];
}

int main(int argc, char *argv[]) {

   int zahl = 0,zahl2 = 0;
   char string1[255] = &quot;./hard &quot;;

   //Werte überprüfen
   if(argc &lt; 1)
      return 0;
   zahl = atoi(argv[1]);
   if(zahl == 1) {
      cout &lt;&lt; zahl&lt;&lt; endl;                   //hier ist die zahl noch 1           
      return zahl;                           //&lt;- 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 &lt;= max;x++) {
      string1[7+x] = string2[x];
   }
   cout &lt;&lt; string1 &lt;&lt; endl;

   //Rekursion aufrufen
   zahl2 = system(string1);            //&lt;- hier wird die zahl2 empfangen und
   cout &lt;&lt; zahl2 &lt;&lt; endl;              //&lt;- ausgegeben jetzt ist sie 256
   //cout &lt;&lt; zahl*zahl2 &lt;&lt; endl;
   return zahl*zahl2;

}
</code></pre>
<p>Das Programm soll die Fakultät einer Zahl erechnen indem es sich selbst,<br />
in einer Rekusion per system() wieder aufruft. Die zahl wird dabei per Kommandozeile übergeben.<br />
Es ist nciht der schönste code aber er funktioniert so weit ganz gut.</p>
<p>Nun zum Problem:<br />
Statt der Zahl die das Programm zurückgeben soll erhalte ich 256 von system zurück.<br />
Ganz leicht nachzuprüfen indem man das Programm übersetzt</p>
<pre><code>g++ rekur.cpp -o hard
</code></pre>
<p>und ausführt</p>
<pre><code>storm@localhost ~/C-C++/Aufgaben/Kommandozeile $ ./hard 2
./hard 1
1
256
</code></pre>
<p>Wie man sieht funktioniert die Rekursion, und das Programm gibt 1 zurück,<br />
plötzlich wird aber aus dem 1 eine 256, warum?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/144575/das-verrückte-rekursive-programm</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 16:20:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/144575.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 19 Apr 2006 14:50:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Das verrückte Rekursive Programm on Wed, 19 Apr 2006 14:52:36 GMT]]></title><description><![CDATA[<p>Es ist mittwoch und meine kumpels sind nciht daheim, mir ist totlangweilig,<br />
soviel zu meiner Situation woraus dieses Programm entstand:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;cstring&gt;

using namespace std;

char* itoa(int val, int base){	
   static char buf[32] = {0};	
   int i = 30;	
   for(; val &amp;&amp; i ; --i, val /= base)	
      buf[i] = &quot;0123456789abcdef&quot;[val % base];	
   return &amp;buf[i+1];
}

int main(int argc, char *argv[]) {

   int zahl = 0,zahl2 = 0;
   char string1[255] = &quot;./hard &quot;;

   //Werte überprüfen
   if(argc &lt; 1)
      return 0;
   zahl = atoi(argv[1]);
   if(zahl == 1) {
      cout &lt;&lt; zahl&lt;&lt; endl;                   //hier ist die zahl noch 1           
      return zahl;                           //&lt;- 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 &lt;= max;x++) {
      string1[7+x] = string2[x];
   }
   cout &lt;&lt; string1 &lt;&lt; endl;

   //Rekursion aufrufen
   zahl2 = system(string1);            //&lt;- hier wird die zahl2 empfangen und
   cout &lt;&lt; zahl2 &lt;&lt; endl;              //&lt;- ausgegeben jetzt ist sie 256
   //cout &lt;&lt; zahl*zahl2 &lt;&lt; endl;
   return zahl*zahl2;

}
</code></pre>
<p>Das Programm soll die Fakultät einer Zahl erechnen indem es sich selbst,<br />
in einer Rekusion per system() wieder aufruft. Die zahl wird dabei per Kommandozeile übergeben.<br />
Es ist nciht der schönste code aber er funktioniert so weit ganz gut.</p>
<p>Nun zum Problem:<br />
Statt der Zahl die das Programm zurückgeben soll erhalte ich 256 von system zurück.<br />
Ganz leicht nachzuprüfen indem man das Programm übersetzt</p>
<pre><code>g++ rekur.cpp -o hard
</code></pre>
<p>und ausführt</p>
<pre><code>storm@localhost ~/C-C++/Aufgaben/Kommandozeile $ ./hard 2
./hard 1
1
256
</code></pre>
<p>Wie man sieht funktioniert die Rekursion, und das Programm gibt 1 zurück,<br />
plötzlich wird aber aus dem 1 eine 256, warum?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1040631</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1040631</guid><dc:creator><![CDATA[Mario Sandler]]></dc:creator><pubDate>Wed, 19 Apr 2006 14:52:36 GMT</pubDate></item><item><title><![CDATA[Reply to Das verrückte Rekursive Programm on Wed, 19 Apr 2006 15:08:10 GMT]]></title><description><![CDATA[<p>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).</p>
<p>In Deinem Fall ist der Returncode 0x0100, also 1 als Returncode und 0 als Indikator für &quot;aus eigener Kraft beendet&quot;.</p>
<p>Unter Linux gibt es dafür auch die Makros WEXITSTATUS(x) und WTERMSIG(x)...</p>
<p>Da in meiner Manpage steht &quot;Conforming to ANSI C, POSIX&quot;, weiss ich leider nicht welche Teile meiner Erläuterung Implementierungsspezifisch sind <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1040650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1040650</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 19 Apr 2006 15:08:10 GMT</pubDate></item><item><title><![CDATA[Reply to Das verrückte Rekursive Programm on Wed, 19 Apr 2006 15:22:56 GMT]]></title><description><![CDATA[<p>Es funktioniert:<br />
hier das endgültige Programm:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;cstring&gt;

using namespace std;

char* itoa(int val, int base){	
   static char buf[32] = {0};	
   int i = 30;	
   for(; val &amp;&amp; i ; --i, val /= base)	
      buf[i] = &quot;0123456789abcdef&quot;[val % base];	
   return &amp;buf[i+1];
}

int main(int argc, char *argv[]) {

   int zahl = 0,zahl2 = 0;
   char string1[255] = &quot;./hard &quot;;

   //Werte überprüfen
   if(argc &lt; 1)
      return 0;
   zahl = atoi(argv[1]);
   if(zahl == 1) {
      cout &lt;&lt; zahl&lt;&lt; endl;
      return zahl;
   }

   //String vorbereiten
   char *string2 = itoa(zahl-1,10);
   int max = strlen(string2);
   for(int x = 0; x &lt;= max;x++) {
      string1[7+x] = string2[x];
   }
   cout &lt;&lt; string1 &lt;&lt; endl;

   //Rekursion aufrufen
   zahl2 = system(string1);
   zahl2 = zahl2 &gt;&gt; 8;
   cout &lt;&lt; zahl*zahl2 &lt;&lt; endl;
   return zahl*zahl2;

}
</code></pre>
<p>Thx @ LordJaxom</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1040658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1040658</guid><dc:creator><![CDATA[Mario Sandler]]></dc:creator><pubDate>Wed, 19 Apr 2006 15:22:56 GMT</pubDate></item></channel></rss>