<?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[Code-Verkürzung]]></title><description><![CDATA[<p>Hi Leute</p>
<p>ich bin ein absoluter C++ Neuling. Ich kann ein klein wenig programmieren, aber nur kleine sachen und ich bräuchte bei zwei Codeabschnitten bitte eure Hilfe um diese zu verkürzen:</p>
<pre><code class="language-cpp">if ((a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (b+1)%2==0 &amp;&amp; (c+1)%2==0) ||
    (d!=0 &amp;&amp; e!=0 &amp;&amp; f!=0 &amp;&amp; (d+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (f+1)%2==0) ||
    (g!=0 &amp;&amp; h!=0 &amp;&amp; i!=0 &amp;&amp; (g+1)%2==0 &amp;&amp; (h+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (a!=0 &amp;&amp; d!=0 &amp;&amp; g!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (d+1)%2==0 &amp;&amp; (g+1)%2==0) ||
    (b!=0 &amp;&amp; e!=0 &amp;&amp; h!=0 &amp;&amp; (b+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (h+1)%2==0) ||
    (c!=0 &amp;&amp; f!=0 &amp;&amp; i!=0 &amp;&amp; (c+1)%2==0 &amp;&amp; (f+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (a!=0 &amp;&amp; e!=0 &amp;&amp; i!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (c!=0 &amp;&amp; e!=0 &amp;&amp; g!=0 &amp;&amp; (c+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (g+1)%2==0)){
        return 1;}
else if ((a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; a%2==0 &amp;&amp; b%2==0 &amp;&amp; c%2==0) ||
    (d!=0 &amp;&amp; e!=0 &amp;&amp; f!=0 &amp;&amp; d%2==0 &amp;&amp; e%2==0 &amp;&amp; f%2==0) ||
    (g!=0 &amp;&amp; h!=0 &amp;&amp; i!=0 &amp;&amp; g%2==0 &amp;&amp; h%2==0 &amp;&amp; i%2==0) ||
    (a!=0 &amp;&amp; d!=0 &amp;&amp; g!=0 &amp;&amp; a%2==0 &amp;&amp; d%2==0 &amp;&amp; g%2==0) ||
    (b!=0 &amp;&amp; e!=0 &amp;&amp; h!=0 &amp;&amp; b%2==0 &amp;&amp; e%2==0 &amp;&amp; h%2==0) ||
    (c!=0 &amp;&amp; f!=0 &amp;&amp; i!=0 &amp;&amp; c%2==0 &amp;&amp; f%2==0 &amp;&amp; i%2==0) ||
    (a!=0 &amp;&amp; e!=0 &amp;&amp; i!=0 &amp;&amp; a%2==0 &amp;&amp; e%2==0 &amp;&amp; i%2==0) ||
    (c!=0 &amp;&amp; e!=0 &amp;&amp; g!=0 &amp;&amp; c%2==0 &amp;&amp; e%2==0 &amp;&amp; g%2==0)){
        return 2;}
</code></pre>
<p>ich bin mir fast sicher, dass es bei einer derartig guten Programmiersprache wie C++ eine einfache Möglichkeit gibt diesen Code zu verkürzen.</p>
<p>ganau so sieht es hier aus:</p>
<pre><code class="language-cpp">if (x==1 and y==1){
        if (a==0){
            a=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==1 and y==2){
        if (d==0){
            d=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==1 and y==3){
        if (g==0){
            g=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==1){
        if (b==0){
            b=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==2){
        if (e==0){
            e=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==3){
        if (h==0){
            h=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==1){
        if (c==0){
            c=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==2){
        if (f==0){
            f=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==3){
        if (i==0){
            i=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
</code></pre>
<p>Danke schonmal im voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/234242/code-verkürzung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 11:41:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/234242.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 15 Feb 2009 12:44:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:50:28 GMT]]></title><description><![CDATA[<p>Hi Leute</p>
<p>ich bin ein absoluter C++ Neuling. Ich kann ein klein wenig programmieren, aber nur kleine sachen und ich bräuchte bei zwei Codeabschnitten bitte eure Hilfe um diese zu verkürzen:</p>
<pre><code class="language-cpp">if ((a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (b+1)%2==0 &amp;&amp; (c+1)%2==0) ||
    (d!=0 &amp;&amp; e!=0 &amp;&amp; f!=0 &amp;&amp; (d+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (f+1)%2==0) ||
    (g!=0 &amp;&amp; h!=0 &amp;&amp; i!=0 &amp;&amp; (g+1)%2==0 &amp;&amp; (h+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (a!=0 &amp;&amp; d!=0 &amp;&amp; g!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (d+1)%2==0 &amp;&amp; (g+1)%2==0) ||
    (b!=0 &amp;&amp; e!=0 &amp;&amp; h!=0 &amp;&amp; (b+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (h+1)%2==0) ||
    (c!=0 &amp;&amp; f!=0 &amp;&amp; i!=0 &amp;&amp; (c+1)%2==0 &amp;&amp; (f+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (a!=0 &amp;&amp; e!=0 &amp;&amp; i!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (i+1)%2==0) ||
    (c!=0 &amp;&amp; e!=0 &amp;&amp; g!=0 &amp;&amp; (c+1)%2==0 &amp;&amp; (e+1)%2==0 &amp;&amp; (g+1)%2==0)){
        return 1;}
else if ((a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; a%2==0 &amp;&amp; b%2==0 &amp;&amp; c%2==0) ||
    (d!=0 &amp;&amp; e!=0 &amp;&amp; f!=0 &amp;&amp; d%2==0 &amp;&amp; e%2==0 &amp;&amp; f%2==0) ||
    (g!=0 &amp;&amp; h!=0 &amp;&amp; i!=0 &amp;&amp; g%2==0 &amp;&amp; h%2==0 &amp;&amp; i%2==0) ||
    (a!=0 &amp;&amp; d!=0 &amp;&amp; g!=0 &amp;&amp; a%2==0 &amp;&amp; d%2==0 &amp;&amp; g%2==0) ||
    (b!=0 &amp;&amp; e!=0 &amp;&amp; h!=0 &amp;&amp; b%2==0 &amp;&amp; e%2==0 &amp;&amp; h%2==0) ||
    (c!=0 &amp;&amp; f!=0 &amp;&amp; i!=0 &amp;&amp; c%2==0 &amp;&amp; f%2==0 &amp;&amp; i%2==0) ||
    (a!=0 &amp;&amp; e!=0 &amp;&amp; i!=0 &amp;&amp; a%2==0 &amp;&amp; e%2==0 &amp;&amp; i%2==0) ||
    (c!=0 &amp;&amp; e!=0 &amp;&amp; g!=0 &amp;&amp; c%2==0 &amp;&amp; e%2==0 &amp;&amp; g%2==0)){
        return 2;}
</code></pre>
<p>ich bin mir fast sicher, dass es bei einer derartig guten Programmiersprache wie C++ eine einfache Möglichkeit gibt diesen Code zu verkürzen.</p>
<p>ganau so sieht es hier aus:</p>
<pre><code class="language-cpp">if (x==1 and y==1){
        if (a==0){
            a=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==1 and y==2){
        if (d==0){
            d=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==1 and y==3){
        if (g==0){
            g=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==1){
        if (b==0){
            b=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==2){
        if (e==0){
            e=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==2 and y==3){
        if (h==0){
            h=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==1){
        if (c==0){
            c=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==2){
        if (f==0){
            f=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
    else if (x==3 and y==3){
        if (i==0){
            i=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
</code></pre>
<p>Danke schonmal im voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664147</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664147</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:50:28 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:50:25 GMT]]></title><description><![CDATA[<p>Was dir sicher sehr helfen wird, sind Funktionen. Damit kannst du dir die lästige Codeduplizierung sparen.</p>
<p>Zum Beispiel für diese Zeile</p>
<pre><code class="language-cpp">a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; (a+1)%2==0 &amp;&amp; (b+1)%2==0 &amp;&amp; (c+1)%2==0
</code></pre>
<p>kannst du dir eine Funktion schreiben, die die einzelnen Variablen als Argumente nimmt und einen <code>bool</code> zurückgibt.</p>
<p>Für die <code>if</code> - <code>else</code> -Abfragen gilt das Gleiche: Der immer wiederkehrende Teil</p>
<pre><code class="language-cpp">if (b==0){
            b=spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            anzahl++;
            spieler--;}}
</code></pre>
<p>kann in eine Funktion ausgelagert werden, die die zu verändernden Variablen als Referenz nimmt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664152</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:50:25 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:51:55 GMT]]></title><description><![CDATA[<p>wie würde dass dann in etwas ausehen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664154</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:51:55 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:56:24 GMT]]></title><description><![CDATA[<p>(a+1)%2==0 fragt ob a ungerade ist. Das kann man auch als a%2==1 schreiben. Wenn das zutrifft, dann ist a sicher != 0. Ansonsten solltest du überlegen ob die anderen Abfragen etwas bedeuten und ggf. logisch umformbar sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664160</guid><dc:creator><![CDATA[hajb]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:56:24 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:57:37 GMT]]></title><description><![CDATA[<p>Schleifen wirken auch Wunder.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664161</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664161</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:57:37 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 12:59:14 GMT]]></title><description><![CDATA[<p>martin.bens schrieb:</p>
<blockquote>
<p>wie würde dass dann in etwas ausehen</p>
</blockquote>
<p>Du solltest unbedingt ein richtiges Buch oder wenigstens ein Tutorial anschauen. Such mal hier im Forum, es werden sehr oft Vorschläge gebracht. Es hat nämlich keinen Wert, wenn ich den Quellcode poste, da du ihn dann mit Sicherheit nicht verstehst.</p>
<p>Funktionen sind etwas vom Grundlegendsten in C++, falls du also ernsthaft programmieren willst, kommst du sowieso nicht darum herum, diese genau anzuschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664162</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 12:59:14 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:20:23 GMT]]></title><description><![CDATA[<p>das mit den Funktionen wussete ich wie das geht. Ich habs auch hinbekommen. War ehrlich gesagt nur zu Faul. Bevor ich mit C++ angefagen hab hatte ich 1,5 Jahre Python. Das ist ne ordentliche Ungewöhnung. Vor allem von der Syntax her.</p>
<p>habs jetz soweit verkürzt:</p>
<pre><code class="language-cpp">if (DreiInReihe1(a,b,c)==true || DreiInReihe1(d,e,f)==true || DreiInReihe1(g,h,i)==true
    || DreiInReihe1(a,d,g)==true || DreiInReihe1(b,e,h)==true || DreiInReihe1(c,f,i)==true
    || DreiInReihe1(a,e,i)==true || DreiInReihe1(c,e,g)==true){
        return 1;}
    else if (DreiInReihe2(a,b,c)==true || DreiInReihe2(d,e,f)==true || DreiInReihe2(g,h,i)==true
    || DreiInReihe2(a,d,g)==true || DreiInReihe2(b,e,h)==true || DreiInReihe2(c,f,i)==true
    || DreiInReihe2(a,e,i)==true || DreiInReihe2(c,e,g)==true){
</code></pre>
<pre><code class="language-cpp">bool DreiInReihe1(int a, int b, int c){
    if (a%2==1 &amp;&amp; b%2==1 &amp;&amp; c%2==1){
        return true;}
    else{
        return false;}}

bool DreiInReihe2(int a, int b, int c){
    if (a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; a%2==0 &amp;&amp; b%2==0 &amp;&amp; c%2==0){
        return true;}
    else{
        return false;}}
</code></pre>
<p>aber bei dem anderen codeabschnitt komm ich nicht drauf</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664173</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:20:23 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:19:15 GMT]]></title><description><![CDATA[<p>Und jetzt packst du das ganze noch irgendwie in eine Schleife und das sind dann noch ein paar Zeilen. a,b,c usw. musst du halt am besten in ein Array oder Container packen, damit der Zugriff in einer Schleife möglich ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664175</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:19:15 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:21:47 GMT]]></title><description><![CDATA[<p>Ok mit array und container kann ich jetz wirklich nichts anfangen. Und was für ne Art von Schleifen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664176</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664176</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:21:47 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:25:26 GMT]]></title><description><![CDATA[<p>Ein Vorteil hat es ja, dass du von Python kommst: Du rückst wenigstens schön ein. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Aber wovon ich unbedingt abrate, ist das da:</p>
<pre><code class="language-cpp">if (...){
        return true;}
    else{
        return false;}}
</code></pre>
<p>Also erstens ist es sehr wichtig, dass du die Klammern einheitlich setzt. Besonders bei grösseren Blöcken siehst du gleich, wo diese beginnen und enden. Aber einfacher kannst du auch gerade schreiben:</p>
<pre><code class="language-cpp">return a%2==1 &amp;&amp; b%2==1 &amp;&amp; c%2==1;
</code></pre>
<p>Das <code>==true</code> in deinem oberen Code brauchst du im Übrigen auch nicht, da der Wert automatisch als <code>bool</code> (also <code>true</code> oder <code>false</code> ) interpretiert wird. Schleifen sind hier nicht unbedingt vereinfachend, da du ja kreuz und quer vergleichst. Container sind eher für die Datenspeicherung, nicht die Codeübersicht von Bedeutung.</p>
<p>Aber dein Code sieht doch schon viel besser aus, wenn du die <code>==true</code> noch entfernst, oder? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664177</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664177</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:25:26 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:33:23 GMT]]></title><description><![CDATA[<p>stimmt habsch jetz auch.<br />
Aber wäre es auch noch möglich das irgendwie zu schreiben, dass der vielzählige Aufruf der Funktionen kürzer geschrieben werden kann.</p>
<p>Und das andere Codesegment krig ich nicht in ne Funktion gepackt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664180</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664180</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:33:23 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:35:23 GMT]]></title><description><![CDATA[<p>Also ich bin immernoch für ne Schleife. :p</p>
<p>Vor allem das, wo du a,b,c dann d,e,f und g,h,i verlgleichst geht ganz gut in eine Schleife rein. bei den anderen kommt es drauf an, ob es ein bestimmtes Muster gibt. Das musst du aber selbst wissen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664183</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664183</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:35:23 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 13:38:03 GMT]]></title><description><![CDATA[<p>martin.bens schrieb:</p>
<blockquote>
<p>Und das andere Codesegment krig ich nicht in ne Funktion gepackt.</p>
</blockquote>
<p>Welches Codesegment? Und wo liegt das Problem genau?</p>
<p>drakon schrieb:</p>
<blockquote>
<p>Also ich bin immernoch für ne Schleife. :p</p>
</blockquote>
<p>Ja, kommt halt drauf an. Nur wenn man dann in der Schleife mehr Code hat als wenn man die Funktionen direkt hinschreiben würde, muss man sich fragen, ob sich der Aufwand lohnt. Grundsätzlich ist es aber nie schlecht, Schleifen zu berücksichtigen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664186</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 13:38:03 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:00:47 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">if (x==1 &amp;&amp; y==1){
        a=feldveraenderung(a, spieler);
        anzahl=anzahlveraenderung(a, anzahl);
        spieler=spielerveraenderung(a, spieler);}
    else if(x==2 &amp;&amp; y==1){
        b=feldveraenderung(b, spieler);
        anzahl=anzahlveraenderung(b, anzahl);
        spieler=spielerveraenderung(b, spieler);}
    else if(x==3 &amp;&amp; y==1){
        c=feldveraenderung(c, spieler);
        anzahl=anzahlveraenderung(c, anzahl);
        spieler=spielerveraenderung(c, spieler);}
    else if(x==1 &amp;&amp; y==2){
        d=feldveraenderung(d, spieler);
        anzahl=anzahlveraenderung(d, anzahl);
        spieler=spielerveraenderung(d, spieler);}
    else if(x==2 &amp;&amp; y==2){
        e=feldveraenderung(e, spieler);
        anzahl=anzahlveraenderung(e, anzahl);
        spieler=spielerveraenderung(e, spieler);}
    else if(x==3 &amp;&amp; y==2){
        f=feldveraenderung(f, spieler);
        anzahl=anzahlveraenderung(f, anzahl);
        spieler=spielerveraenderung(f, spieler);}
    else if(x==1 &amp;&amp; y==3){
        g=feldveraenderung(g, spieler);
        anzahl=anzahlveraenderung(g, anzahl);
        spieler=spielerveraenderung(g, spieler);}
    else if(x==2 &amp;&amp; y==3){
        h=feldveraenderung(h, spieler);
        anzahl=anzahlveraenderung(h, anzahl);
        spieler=spielerveraenderung(h, spieler);}
    else if(x==3 &amp;&amp; y==3){
        i=feldveraenderung(i, spieler);
        anzahl=anzahlveraenderung(i, anzahl);
        spieler=spielerveraenderung(i, spieler);}
</code></pre>
<pre><code class="language-cpp">int feldveraenderung(int a, int spieler){
    if (a==0){
            return spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            return 0;
        }}

int anzahlveraenderung(int a, int anzahl){
    if (a!=0){
        return anzahl++;}}

int spielerveraenderung(int a, int spieler){
    if (a!=0){
        return spieler--;}}
</code></pre>
<p>könnte man das noch kürzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664198</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664198</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:00:47 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:03:16 GMT]]></title><description><![CDATA[<p>Klar geht das. Probier einfach das ganze zu abstrahieren, wo du jetzt eine Zahl einsetzt. Gibt es da ein Muster? - Wenn ja dann setz es in Programmcode um und dann kannst du das ganze wieder in eine Schleife packen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664200</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664200</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:03:16 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:06:03 GMT]]></title><description><![CDATA[<p>Das erste sehr wohl. Du machst zwei verschachtelte Schleifen, eine für das <code>x</code> und eine für das <code>y</code> .</p>
<p>Beim zweiten könntest du eventuell den ternären <code>operator?:</code> einsetzen, aber wichtiger ist, dass du die Klammern sauber setzt. Glaub mir, du wirst später froh sein, wenn du dir jetzt schon einen guten Stil aneignest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664202</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664202</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:06:03 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:10:34 GMT]]></title><description><![CDATA[<p>Hier mal der ganze Code. Das sollte für mich zur Übung ein Tic-Tac-ToE Spiel werden:</p>
<pre><code class="language-cpp"># include &lt;iostream&gt;
using namespace std;

int y_koordinate(){
    int y;
    cout&lt;&lt;&quot;y-Koordinate: &quot;;
    cin&gt;&gt;y;
    cout&lt;&lt;&quot;\n&quot;;
    return y;
}

int x_koordinate(){
    int x;
    cout&lt;&lt;&quot;x-Koordinate: &quot;;
    cin&gt;&gt;x;
    cout&lt;&lt;&quot;\n&quot;;
    return x;
}

bool DreiInReihe1(int a, int b, int c){
    return a%2==1 &amp;&amp; b%2==1 &amp;&amp; c%2==1;}

bool DreiInReihe2(int a, int b, int c){
    return a!=0 &amp;&amp; b!=0 &amp;&amp; c!=0 &amp;&amp; a%2==0 &amp;&amp; b%2==0 &amp;&amp; c%2==0;}

int feldveraenderung(int a, int spieler){
    if (a==0){
            return spieler;}
        else{
            cout&lt;&lt;&quot;Feld bereits besetzt.\n&quot;;
            return 0;
        }}

int anzahlveraenderung(int a, int anzahl){
    if (a!=0){
        return anzahl++;}}

int spielerveraenderung(int a, int spieler){
    if (a!=0){
        spieler--;
        return spieler;}}

int feld(string spielername1, string spielername2){
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0, spieler=1, anzahl=8;
    while (anzahl&lt;=8){
    if (spieler%2==1){
        cout&lt;&lt;spielername1&lt;&lt;&quot; : \n&quot;;}
    else if (spieler%2==0){
        cout&lt;&lt;spielername2&lt;&lt;&quot; : \n&quot;;}
    int x=x_koordinate();
    int y=y_koordinate();
    if (x==1 &amp;&amp; y==1){
        a=feldveraenderung(a, spieler);
        anzahl=anzahlveraenderung(a, anzahl);
        spieler=spielerveraenderung(a, spieler);}
    else if(x==2 &amp;&amp; y==1){
        b=feldveraenderung(b, spieler);
        anzahl=anzahlveraenderung(b, anzahl);
        spieler=spielerveraenderung(b, spieler);}
    else if(x==3 &amp;&amp; y==1){
        c=feldveraenderung(c, spieler);
        anzahl=anzahlveraenderung(c, anzahl);
        spieler=spielerveraenderung(c, spieler);}
    else if(x==1 &amp;&amp; y==2){
        d=feldveraenderung(d, spieler);
        anzahl=anzahlveraenderung(d, anzahl);
        spieler=spielerveraenderung(d, spieler);}
    else if(x==2 &amp;&amp; y==2){
        e=feldveraenderung(e, spieler);
        anzahl=anzahlveraenderung(e, anzahl);
        spieler=spielerveraenderung(e, spieler);}
    else if(x==3 &amp;&amp; y==2){
        f=feldveraenderung(f, spieler);
        anzahl=anzahlveraenderung(f, anzahl);
        spieler=spielerveraenderung(f, spieler);}
    else if(x==1 &amp;&amp; y==3){
        g=feldveraenderung(g, spieler);
        anzahl=anzahlveraenderung(g, anzahl);
        spieler=spielerveraenderung(g, spieler);}
    else if(x==2 &amp;&amp; y==3){
        h=feldveraenderung(h, spieler);
        anzahl=anzahlveraenderung(h, anzahl);
        spieler=spielerveraenderung(h, spieler);}
    else if(x==3 &amp;&amp; y==3){
        i=feldveraenderung(i, spieler);
        anzahl=anzahlveraenderung(i, anzahl);
        spieler=spielerveraenderung(i, spieler);}
    else{
        cout&lt;&lt;&quot;Falsche Eingabe\n&quot;;
        anzahl++;
        spieler--;}

    if (DreiInReihe1(a,b,c) || DreiInReihe1(d,e,f) || DreiInReihe1(g,h,i)
    || DreiInReihe1(a,d,g) || DreiInReihe1(b,e,h) || DreiInReihe1(c,f,i)
    || DreiInReihe1(a,e,i) || DreiInReihe1(c,e,g)){
        return 1;}
    else if (DreiInReihe2(a,b,c) || DreiInReihe2(d,e,f) || DreiInReihe2(g,h,i)
    || DreiInReihe2(a,d,g) || DreiInReihe2(b,e,h) || DreiInReihe2(c,f,i)
    || DreiInReihe2(a,e,i) || DreiInReihe2(c,e,g)){
        return 2;}
        anzahl--;
        spieler++;
        }
}

int main(){
    cout&lt;&lt;&quot;Tic - Tac - To\n&quot;;
    cout&lt;&lt;&quot;Welches Zeichen moechten sie?\n&quot;;
    cout&lt;&lt;&quot;Kreuz --&gt; 1 und Kreis --&gt;2  &quot;;
    int spieler1=0, spieler2=0;
    cin&gt;&gt;spieler1;
    cout&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;&quot;Spieler 1 geben sie ihren Namen ein! &quot;;
    string spielername1;
    cin&gt;&gt;spielername1;
    cout&lt;&lt;&quot;Spieler 2 geben sie ihren Namen ein! &quot;;
    string spielername2;
    cin&gt;&gt;spielername2;
    if (spieler1==1){
        cout&lt;&lt;spielername1&lt;&lt;&quot; hat Kreuz und &quot;&lt;&lt;spielername2&lt;&lt;&quot; hat Kreis\n&quot;;}
    else if (spieler1==2){
        cout&lt;&lt;spielername2&lt;&lt;&quot; hat Kreuz und &quot;&lt;&lt;spielername1&lt;&lt;&quot; hat Kreis\n&quot;;}
    if (spieler1==1){
        spieler2=2;}
    else if(spieler1==2){
        spieler2=1;}
    int gewonnen;
    gewonnen=feld(spielername1, spielername2);
    if (gewonnen==spieler1){
        cout&lt;&lt;spielername1&lt;&lt;&quot; hat gewonnen\n&quot;;}
    else if(gewonnen==spieler2){
        cout&lt;&lt;spielername2&lt;&lt;&quot; hat gewonnen\n&quot;;}
    system(&quot;Pause&quot;);
    return 0;
}
</code></pre>
<p>könntet ihr mir noch mögliche Verkürzungen sagen. Das war eben so einfache programmierung und dann wollt ich mir Rat einholen wie ichs besser machen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664206</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:10:34 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:16:13 GMT]]></title><description><![CDATA[<p>Wir haben dir ja bereits Ratschläge gebracht...?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664211</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664211</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:16:13 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:18:40 GMT]]></title><description><![CDATA[<p>die meisten versteh ich leider nicht.<br />
einmal das mit den Schleifen und das mit dem ternären operator?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664214</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664214</guid><dc:creator><![CDATA[SpiGAndromeda]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:18:40 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:20:05 GMT]]></title><description><![CDATA[<p>Wie wärs, wenn du dich zusätzlich zu unseren Vorschlägen noch ein wenig selbst erkunden würdest?</p>
<p>Zu den Schleifen haben wir glaube ich genügend Hinweise gegeben. Schau doch, wo du wiederkommende Strukturen erkennst, und versuche, eine Regelmässigkeit darin zu finden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664216</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:20:05 GMT</pubDate></item><item><title><![CDATA[Reply to Code-Verkürzung on Sun, 15 Feb 2009 14:22:36 GMT]]></title><description><![CDATA[<p>Kleine Merkregel, wenn du weisst, was du noch verkürzen kannst:</p>
<p>Sobald du Code per Copy/Paste kopierst und einfügst, dann ist das ein Fall von unnötiger Redundanz. (Klar gibt es Ausnahmen, aber bei einem Anfänger greift die Regel sicher recht gut.)</p>
<p>Ich sehe bei dir so ein Schema:</p>
<pre><code class="language-cpp">for (int y = 0; y &lt; 3; y++)
   for (int x = 0; x &lt; 3; x++)
   {
       bar (array[y*3 + x]);
       foobar (array[y*3 + x]);
       foo (x,y);
   }
</code></pre>
<p>Wo das ist und wie du das bei dir umsetzen kannst solltest du selbst hinkriegen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664218</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 15 Feb 2009 14:22:36 GMT</pubDate></item></channel></rss>