<?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[Polynomaddition mit verketteter Liste]]></title><description><![CDATA[<p>Hallo zusammen!<br />
Ich habe ein Problem mit meinem Algorithmus zu einer Polynomaddition. Man soll zwei Polynome beliebigen Grades nehmen und mittels verketteten Listen addieren. Klingt einfach, dachte ich auch.</p>
<p>Der Code ist auch an sich kein Problem - er ist noch etwas redundant (das ist mir bewusst) und einige Bedingungen fehlen noch, aber das ist auch gar nicht das Problem. Wenn ich ihn kompiliere und ausführe, meine beiden Polynome eingebe, sieht das ganze wie folgt aus:</p>
<p><a href="http://i.imagebanana.com/img/aesw2nz5/polynom.png" rel="nofollow">http://i.imagebanana.com/img/aesw2nz5/polynom.png</a></p>
<p>Jeder Schritt wird einzeln ausgedruckt, obwohl ich nur den untersten haben möchte. Das ist mein Problem - ich sitze jetzt seit mehreren Stunden dran und bekomme meinen Fehler nicht raus. Vielleicht könnt ihr mir helfen <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>
<pre><code>#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;sstream&gt;
#include &lt;iterator&gt;
using namespace std;

//Funktionen//

std::list&lt;string&gt; AddPolynomes(std::list&lt;string&gt; l1, std::list&lt;string&gt; l2) {

    std::list&lt;string&gt; polynome;
    string temp1, temp2, pol;
    stringstream ss1, ss2, ss3;
    int coef1, coef2, coef3, expo1, expo2;

    for (std::list&lt;string&gt;::iterator i=l1.begin(); i!=l1.end(); ++i) {
        temp1 = &quot;&quot;;
        ss1.clear();

        temp1 = *i;
        ss1 &lt;&lt; temp1;
        ss1 &gt;&gt; coef1 &gt;&gt; expo1;

        for(std::list&lt;string&gt;::iterator j=l2.begin(); j!=l2.end(); ++j) {
            temp2 = &quot;&quot;;
            ss2.clear();

            temp2 = *j;
            ss2 &lt;&lt; temp2;
            ss2 &gt;&gt; coef2 &gt;&gt; expo2;

            if (expo1 == expo2) {
                coef3 = coef1 + coef2;
                pol = &quot;&quot;;
                ss3.clear();

                if (coef3 &lt; 0) {
                    ss3 &lt;&lt; coef3 &lt;&lt; &quot;x^&quot; &lt;&lt; expo2;
                    pol = ss3.str();
                    polynome.push_back(pol);
                }
                else {
                    ss3 &lt;&lt; coef3 &lt;&lt; &quot;x^&quot; &lt;&lt; expo2 &lt;&lt; &quot;+&quot;;
                    pol = ss3.str();
                    polynome.push_back(pol);
                }
            }
        }
    }
    return polynome;
}

void PrintList(std::list&lt;string&gt; l) {
    std::copy(l.begin(), l.end(), std::ostream_iterator&lt;string&gt;(std::cout, &quot;\n&quot;));
}

//Main//

int main() {
    int degree, coef, expo;
    string s;
    ostringstream os;
    std::list&lt;string&gt; list1, list2, polynome;

    /* First polynome */
    cout &lt;&lt; &quot;Enter the degree of polynomial: &quot;;
    cin &gt;&gt; degree;

    while (expo != degree) {
        s = &quot;&quot;;
        os.str(&quot;&quot;);

        cout &lt;&lt; &quot;Enter coef, expo: &quot;;
        cin &gt;&gt; coef &gt;&gt; expo;
        os &lt;&lt; coef &lt;&lt; &quot; &quot; &lt;&lt; expo;
        s = os.str();

        list1.push_back(s);
    }

    degree = 0;
    expo = 0;
    coef = 0;

    /* second polynome */
    cout &lt;&lt; &quot;Enter the degree of polynomial: &quot;;
    cin &gt;&gt; degree;

    while (expo != degree) {
        s = &quot;&quot;;
        os.str(&quot;&quot;);

        cout &lt;&lt; &quot;Enter coef, expo: &quot;;
        cin &gt;&gt; coef &gt;&gt; expo;
        os &lt;&lt; coef &lt;&lt; &quot; &quot; &lt;&lt; expo;
        s = os.str();

        list2.push_back(s);
    }

    /*Addition and print of new polynome*/
    polynome = AddPolynomes(list1, list2);

    PrintList(polynome);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/311096/polynomaddition-mit-verketteter-liste</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 00:32:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/311096.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Nov 2012 21:06:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Polynomaddition mit verketteter Liste on Mon, 26 Nov 2012 21:06:27 GMT]]></title><description><![CDATA[<p>Hallo zusammen!<br />
Ich habe ein Problem mit meinem Algorithmus zu einer Polynomaddition. Man soll zwei Polynome beliebigen Grades nehmen und mittels verketteten Listen addieren. Klingt einfach, dachte ich auch.</p>
<p>Der Code ist auch an sich kein Problem - er ist noch etwas redundant (das ist mir bewusst) und einige Bedingungen fehlen noch, aber das ist auch gar nicht das Problem. Wenn ich ihn kompiliere und ausführe, meine beiden Polynome eingebe, sieht das ganze wie folgt aus:</p>
<p><a href="http://i.imagebanana.com/img/aesw2nz5/polynom.png" rel="nofollow">http://i.imagebanana.com/img/aesw2nz5/polynom.png</a></p>
<p>Jeder Schritt wird einzeln ausgedruckt, obwohl ich nur den untersten haben möchte. Das ist mein Problem - ich sitze jetzt seit mehreren Stunden dran und bekomme meinen Fehler nicht raus. Vielleicht könnt ihr mir helfen <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>
<pre><code>#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;sstream&gt;
#include &lt;iterator&gt;
using namespace std;

//Funktionen//

std::list&lt;string&gt; AddPolynomes(std::list&lt;string&gt; l1, std::list&lt;string&gt; l2) {

    std::list&lt;string&gt; polynome;
    string temp1, temp2, pol;
    stringstream ss1, ss2, ss3;
    int coef1, coef2, coef3, expo1, expo2;

    for (std::list&lt;string&gt;::iterator i=l1.begin(); i!=l1.end(); ++i) {
        temp1 = &quot;&quot;;
        ss1.clear();

        temp1 = *i;
        ss1 &lt;&lt; temp1;
        ss1 &gt;&gt; coef1 &gt;&gt; expo1;

        for(std::list&lt;string&gt;::iterator j=l2.begin(); j!=l2.end(); ++j) {
            temp2 = &quot;&quot;;
            ss2.clear();

            temp2 = *j;
            ss2 &lt;&lt; temp2;
            ss2 &gt;&gt; coef2 &gt;&gt; expo2;

            if (expo1 == expo2) {
                coef3 = coef1 + coef2;
                pol = &quot;&quot;;
                ss3.clear();

                if (coef3 &lt; 0) {
                    ss3 &lt;&lt; coef3 &lt;&lt; &quot;x^&quot; &lt;&lt; expo2;
                    pol = ss3.str();
                    polynome.push_back(pol);
                }
                else {
                    ss3 &lt;&lt; coef3 &lt;&lt; &quot;x^&quot; &lt;&lt; expo2 &lt;&lt; &quot;+&quot;;
                    pol = ss3.str();
                    polynome.push_back(pol);
                }
            }
        }
    }
    return polynome;
}

void PrintList(std::list&lt;string&gt; l) {
    std::copy(l.begin(), l.end(), std::ostream_iterator&lt;string&gt;(std::cout, &quot;\n&quot;));
}

//Main//

int main() {
    int degree, coef, expo;
    string s;
    ostringstream os;
    std::list&lt;string&gt; list1, list2, polynome;

    /* First polynome */
    cout &lt;&lt; &quot;Enter the degree of polynomial: &quot;;
    cin &gt;&gt; degree;

    while (expo != degree) {
        s = &quot;&quot;;
        os.str(&quot;&quot;);

        cout &lt;&lt; &quot;Enter coef, expo: &quot;;
        cin &gt;&gt; coef &gt;&gt; expo;
        os &lt;&lt; coef &lt;&lt; &quot; &quot; &lt;&lt; expo;
        s = os.str();

        list1.push_back(s);
    }

    degree = 0;
    expo = 0;
    coef = 0;

    /* second polynome */
    cout &lt;&lt; &quot;Enter the degree of polynomial: &quot;;
    cin &gt;&gt; degree;

    while (expo != degree) {
        s = &quot;&quot;;
        os.str(&quot;&quot;);

        cout &lt;&lt; &quot;Enter coef, expo: &quot;;
        cin &gt;&gt; coef &gt;&gt; expo;
        os &lt;&lt; coef &lt;&lt; &quot; &quot; &lt;&lt; expo;
        s = os.str();

        list2.push_back(s);
    }

    /*Addition and print of new polynome*/
    polynome = AddPolynomes(list1, list2);

    PrintList(polynome);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2274935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2274935</guid><dc:creator><![CDATA[Verzweiflung]]></dc:creator><pubDate>Mon, 26 Nov 2012 21:06:27 GMT</pubDate></item><item><title><![CDATA[Reply to Polynomaddition mit verketteter Liste on Mon, 26 Nov 2012 21:46:46 GMT]]></title><description><![CDATA[<p>was erwartest du was passiert, wenn du die Ausgabe in eine doppelt verschachtelte Schleife packst?</p>
<p>Dr COde ist auch total wirr. Strukturier ihn mal so um:</p>
<p>1. addier die beiden Polynome und speicher das Ergebnis in einer dritten Liste<br />
2. gib diese dritte Liste als Polynom aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2274944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2274944</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Mon, 26 Nov 2012 21:46:46 GMT</pubDate></item><item><title><![CDATA[Reply to Polynomaddition mit verketteter Liste on Mon, 26 Nov 2012 23:34:45 GMT]]></title><description><![CDATA[<p>Die methode clear() beim stringstream macht warscheinlich nicht was Du denkst.<br />
In deiner Print Funktion reicht es dadurch nur das Element der Liste auszugeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2274985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2274985</guid><dc:creator><![CDATA[maxmax]]></dc:creator><pubDate>Mon, 26 Nov 2012 23:34:45 GMT</pubDate></item><item><title><![CDATA[Reply to Polynomaddition mit verketteter Liste on Tue, 27 Nov 2012 08:05:10 GMT]]></title><description><![CDATA[<p>Hallo Verzweifelter,</p>
<p>ist in der Liste 'l1' ein Polynom mit einem Exponenten vorhanden, der in 'l2' nicht vorkommt, so ist der Ausdruck in Zeile 33 nie 'true'. Also wird dieses Element auch nie zur Summe hinzugefügt. Aus diesem Grund fehlt auch der Term '5x^5' im Ergebnis.</p>
<p>Tipps:<br />
- Deklariere Variablen erst dann, wenn Du sie benötigst. Das schafft Überblick.<br />
- Die Zeilen 18, 26 und 35 kannst Du löschen.<br />
- speichere die Elemente nicht als strings, sondern so, dass Du mit ihnen direkt rechnen kannst (als Zahlen)<br />
- Mache aus dem Polynom eine Klasse, die einen Container enthält (vector&lt;&gt; oder list&lt;&gt;). Damit kapselst Du den Zugriff, und kannst z.B. festlegen, das der Exponent implizit über die Stellung des Elements im Container festgelegt ist.<br />
- Die Ausgabe in Zeile 44 passt nicht zu der Abfrage in Zeile 38.<br />
- kommen in der Liste 'l2' zwei Elemente mit dem gleichen Exponenten vor, so wird der zugehörige Wert aus 'l1' zweimal addiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2275010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2275010</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 27 Nov 2012 08:05:10 GMT</pubDate></item></channel></rss>