<?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[Klasse für rationale Zahlen (Brüche)]]></title><description><![CDATA[<p>Hallo!<br />
Ich bin gerade dabei eine Klasse zu programmieren, die mit rationalen Zahlen rechnen soll.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std ;

class Ratio {
      public:
             void ein(int z,int n);
             void aus();
             void gleitkomma();

             Ratio operator*(Ratio &amp;r);
             Ratio operator/(Ratio &amp;r);
             Ratio operator+(Ratio &amp;r);
             Ratio operator-();
             Ratio operator-(Ratio &amp;r);

      private:
              int zaehler;
              int nenner;
              void kuerzen();
};

void Ratio::ein(int z,int n) {
     zaehler=z;
     nenner=n;
}

void Ratio::kuerzen() {
     int a;
     if (abs(zaehler)&gt;abs(nenner)) { a=abs(nenner)+1; } else { a=abs(zaehler)+1; }
     do {
     a--;
     } while ((zaehler%a!=0) || (nenner%a!=0)); 

     zaehler=zaehler/a;
     nenner=nenner/a;

}

void Ratio::aus() {
     kuerzen();
     cout &lt;&lt; zaehler &lt;&lt; &quot;/&quot; &lt;&lt; nenner &lt;&lt; endl;
}

void Ratio::gleitkomma() {
     cout &lt;&lt; (float)zaehler/(float)nenner &lt;&lt; endl;     
}

Ratio Ratio::operator*(Ratio &amp;r) {
      Ratio erg;
      erg.ein(zaehler*r.zaehler,nenner*r.nenner);     
      return(erg);
}

Ratio Ratio::operator/(Ratio &amp;r) {
      Ratio erg;
      erg.ein(zaehler*r.nenner,nenner*r.zaehler);     
      return(erg);
}

Ratio Ratio::operator+(Ratio &amp;r) {
      int n=nenner*r.nenner;
      Ratio erg;
      erg.ein(zaehler*n/nenner+r.zaehler*n/r.nenner,n);     
      return(erg);
}

Ratio Ratio::operator-() {
      Ratio erg;
      erg.ein(-zaehler,nenner);
      return(erg);
      }

Ratio Ratio::operator-(Ratio &amp;r) {
      int n=nenner*r.nenner;
      Ratio erg;
      erg.ein(zaehler*n/nenner-r.zaehler*n/r.nenner,n);     
      return(erg);
}

int main()
{
    Ratio r1,r2,r3,r4,r5,erg,erg2;
    r1.ein(2,25);
    r2.ein(7,5);
    r3.ein(2,5);
    r4.ein(1,4);
    r5.ein(1,3);

    erg=r1*(r2+r3);
    erg.aus();
    erg.gleitkomma();

    system(&quot;Pause&quot;);
    return(0);
}
</code></pre>
<p>Nachdem ich das Problem mit einem Minus als Vorzeichen gelöst habe, ist jetzt noch das Problem der Klammern offen.<br />
Wie kann ich der Klasse klar machen, dass sie zuerst die Klammer berechnen soll und dann die Multiplikation?<br />
Zurzeit habe ich noch folgende Fehlermeldung:</p>
<p><code>rational.cc: In function</code>int main()':</p>
<p><a href="http://rational.cc:103" rel="nofollow">rational.cc:103</a>: error: no match for 'operator*' in 'r1 * (&amp;r2)-&gt;Ratio::operator+(((Ratio&amp;)(&amp;r3)))'</p>
<p><a href="http://rational.cc:55" rel="nofollow">rational.cc:55</a>: note: candidates are: Ratio Ratio::operator*(Ratio&amp;)`</p>
<p>Über Infos zu diesem Problem würde ich mich freuen,</p>
<p>Thomas</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/257361/klasse-für-rationale-zahlen-brüche</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 11:48:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/257361.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 Dec 2009 13:36:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klasse für rationale Zahlen (Brüche) on Sun, 27 Dec 2009 13:36:19 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich bin gerade dabei eine Klasse zu programmieren, die mit rationalen Zahlen rechnen soll.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std ;

class Ratio {
      public:
             void ein(int z,int n);
             void aus();
             void gleitkomma();

             Ratio operator*(Ratio &amp;r);
             Ratio operator/(Ratio &amp;r);
             Ratio operator+(Ratio &amp;r);
             Ratio operator-();
             Ratio operator-(Ratio &amp;r);

      private:
              int zaehler;
              int nenner;
              void kuerzen();
};

void Ratio::ein(int z,int n) {
     zaehler=z;
     nenner=n;
}

void Ratio::kuerzen() {
     int a;
     if (abs(zaehler)&gt;abs(nenner)) { a=abs(nenner)+1; } else { a=abs(zaehler)+1; }
     do {
     a--;
     } while ((zaehler%a!=0) || (nenner%a!=0)); 

     zaehler=zaehler/a;
     nenner=nenner/a;

}

void Ratio::aus() {
     kuerzen();
     cout &lt;&lt; zaehler &lt;&lt; &quot;/&quot; &lt;&lt; nenner &lt;&lt; endl;
}

void Ratio::gleitkomma() {
     cout &lt;&lt; (float)zaehler/(float)nenner &lt;&lt; endl;     
}

Ratio Ratio::operator*(Ratio &amp;r) {
      Ratio erg;
      erg.ein(zaehler*r.zaehler,nenner*r.nenner);     
      return(erg);
}

Ratio Ratio::operator/(Ratio &amp;r) {
      Ratio erg;
      erg.ein(zaehler*r.nenner,nenner*r.zaehler);     
      return(erg);
}

Ratio Ratio::operator+(Ratio &amp;r) {
      int n=nenner*r.nenner;
      Ratio erg;
      erg.ein(zaehler*n/nenner+r.zaehler*n/r.nenner,n);     
      return(erg);
}

Ratio Ratio::operator-() {
      Ratio erg;
      erg.ein(-zaehler,nenner);
      return(erg);
      }

Ratio Ratio::operator-(Ratio &amp;r) {
      int n=nenner*r.nenner;
      Ratio erg;
      erg.ein(zaehler*n/nenner-r.zaehler*n/r.nenner,n);     
      return(erg);
}

int main()
{
    Ratio r1,r2,r3,r4,r5,erg,erg2;
    r1.ein(2,25);
    r2.ein(7,5);
    r3.ein(2,5);
    r4.ein(1,4);
    r5.ein(1,3);

    erg=r1*(r2+r3);
    erg.aus();
    erg.gleitkomma();

    system(&quot;Pause&quot;);
    return(0);
}
</code></pre>
<p>Nachdem ich das Problem mit einem Minus als Vorzeichen gelöst habe, ist jetzt noch das Problem der Klammern offen.<br />
Wie kann ich der Klasse klar machen, dass sie zuerst die Klammer berechnen soll und dann die Multiplikation?<br />
Zurzeit habe ich noch folgende Fehlermeldung:</p>
<p><code>rational.cc: In function</code>int main()':</p>
<p><a href="http://rational.cc:103" rel="nofollow">rational.cc:103</a>: error: no match for 'operator*' in 'r1 * (&amp;r2)-&gt;Ratio::operator+(((Ratio&amp;)(&amp;r3)))'</p>
<p><a href="http://rational.cc:55" rel="nofollow">rational.cc:55</a>: note: candidates are: Ratio Ratio::operator*(Ratio&amp;)`</p>
<p>Über Infos zu diesem Problem würde ich mich freuen,</p>
<p>Thomas</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828440</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828440</guid><dc:creator><![CDATA[m-r-x]]></dc:creator><pubDate>Sun, 27 Dec 2009 13:36:19 GMT</pubDate></item><item><title><![CDATA[Reply to Klasse für rationale Zahlen (Brüche) on Sun, 27 Dec 2009 13:55:46 GMT]]></title><description><![CDATA[<p>Was die Operatorüberladung angeht, findest du in <a href="http://magazin.c-plusplus.net/artikel/%DCberladung%20von%20Operatoren%20in%20CPlusPlus%20%28Teil%201%29" rel="nofollow">diesem Artikel</a> sehr viele nützliche Tipps und gängige Implementierungen. Zum Beispiel sollten die Operatoren <code>+</code> , <code>-</code> , <code>*</code> , <code>/</code> (jeweils mit zwei Operanden) global deklariert werden und ihre Argumente als Const-Referenz nehmen. Dazu sollten entsprechende arithmetische Zuweisungsoperatoren in der Klasse vorhanden sein ( <code>+=</code> , <code>-=</code> , <code>*=</code> , <code>/=</code> ).</p>
<p>Im Weiteren würde ich dir raten, statt der Methode <code>ein()</code> einen angemessenen Konstruktor bereitzustellen, der gleich Zähler und Nenner initialisiert. Vielleicht so:</p>
<pre><code class="language-cpp">Ratio(int zaehler = 0, int nenner = 1);
</code></pre>
<p>Die Klammern sollten automatisch dafür sorgen, dass der innere Ausdruck zuerst ausgewertet wird und erst dessen Ergebnis multipliziert wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828456</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 27 Dec 2009 13:55:46 GMT</pubDate></item><item><title><![CDATA[Reply to Klasse für rationale Zahlen (Brüche) on Sun, 27 Dec 2009 14:04:02 GMT]]></title><description><![CDATA[<p>Hi,<br />
danke für die Antwort, Konstruktoren haben wir in der Vorlesung noch nicht behandelt und ansonsten waren nur die Operatoren +,-,+ und / verlangt, von daher kann ich mir die anderen (+=,...) sparen, aber trotzdem danke für diese Hinweise.</p>
<p>Das mit den Klammern funktioniert aber nicht automatisch, warum, weiß ich auch nicht, die Addition an sich sowie die Multiplikation funktionieren:</p>
<pre><code class="language-cpp">erg=r2+r3;
    erg.aus();
    erg.gleitkomma();

    erg2=r1*erg;
    erg2.aus();
    erg2.gleitkomma();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1828463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828463</guid><dc:creator><![CDATA[m-r-x]]></dc:creator><pubDate>Sun, 27 Dec 2009 14:04:02 GMT</pubDate></item><item><title><![CDATA[Reply to Klasse für rationale Zahlen (Brüche) on Sun, 27 Dec 2009 15:43:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">Ratio Ratio::operator*(Ratio &amp;r)
</code></pre>
<p>--&gt;</p>
<pre><code class="language-cpp">Ratio Ratio::operator*(const Ratio &amp;r)
</code></pre>
<p>(Das Gleiche bei den anderen Operatoren).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828524</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sun, 27 Dec 2009 15:43:05 GMT</pubDate></item><item><title><![CDATA[Reply to Klasse für rationale Zahlen (Brüche) on Sun, 27 Dec 2009 16:11:47 GMT]]></title><description><![CDATA[<p>Besser</p>
<pre><code class="language-cpp">Ratio Ratio::operator*(Ratio &amp;r);
</code></pre>
<p>--&gt;</p>
<pre><code class="language-cpp">Ratio Ratio::operator*(const Ratio &amp;r);
</code></pre>
<p>--&gt;</p>
<pre><code class="language-cpp">Ratio operator*(const Ratio&amp; l, const Ratio&amp; r);
</code></pre>
<p>Warum, steht im Artikel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1828545</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1828545</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 27 Dec 2009 16:11:47 GMT</pubDate></item></channel></rss>