<?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[Rational Struct]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>ich habe mir einen struct gebastelt (klassen kenne ich noch nicht, dass kommt später noch), der den Zahlenkörper <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mrow><mi mathvariant="double-struck">Q</mi></mrow></mrow><annotation encoding="application/x-tex">\mathbb{Q}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68889em;"></span><span class="strut bottom" style="height:0.85556em;vertical-align:-0.16667em;"></span><span class="base textstyle uncramped"><span class="mord textstyle uncramped"><span class="mord mathbb">Q</span></span></span></span></span> darstellen soll, also die rationalen Zahlen.</p>
<p>Dazu habe ich auch einige Operatoren implementiert. der code sieht wie folgt aus:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;

// Define the new type Rational

struct Rational{
  int n;
  int d; // INV: d!=0
};

// Grundoperatoren

// PRE:  Two value of type Rational
// POST: return value is a Rational representing the summation

Rational operator+ (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d+y.n*x.d;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational 
// POST: return value is a Rational representing the subtraction

Rational operator- (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d-y.n*x.d;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational 
// POST: return value is a Rational representing the product

Rational operator* (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.n;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational, y!=0 
// POST: return value is a Rational representing the quotient

Rational operator/ (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d;
  result.d=x.d*y.n;
  return result;
} 

// Booloperatoren

// PRE:  Two value of type Rational 
// POST: return value is true if a==b

bool operator== (Rational x, Rational y){
  return x.n*y.d==y.n*x.d ;
} 

// PRE:  Two value of type Rational 
// POST: return value is true if a==b

bool operator!= (Rational x, Rational y){
  return !(x.n*y.d==y.n*x.d) ;
}; 

//Rational operator&lt; (Rational x, Rational y){
//  return !(x.n*y.d==y.n*x.d) ;
//} 

// PRE:  A Rational
// POST: Rational has been written on outstream

std::ostream&amp; operator&lt;&lt; (std::ostream&amp; o, Rational x){
  return o &lt;&lt; x.n &lt;&lt; &quot;/&quot; &lt;&lt; x.d;
};

std::istream&amp; operator&gt;&gt; (std::istream&amp; i, Rational x){
  char c; // For the / in input
  return i &gt;&gt; x.n &gt;&gt; c &gt;&gt; x.d;
};

int main ()
{

  // Input

  std::cout &lt;&lt; &quot;Your first rational number r:\n&quot;;
  Rational r;
  std::cin &gt;&gt; r;

  std::cout &lt;&lt; &quot;Your second rational number s:\n&quot;;
  Rational s;
  std::cin &gt;&gt; s;

  // Computation and Output

  std::cout &lt;&lt; &quot; The sum of 2 Rationals is         &quot; &lt;&lt; r+s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The subtraction of 2 Rationals is &quot; &lt;&lt; r-s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The product of 2 Rationals is     &quot; &lt;&lt; r*s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The division of 2 Rationals is    &quot; &lt;&lt; r/s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The equality of 2 Rationals is    &quot; &lt;&lt;(r==s)  &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The inequalitz of 2 Rationals is  &quot; &lt;&lt;(r!=s)  &lt;&lt; &quot;.\n&quot;;

 return 0;
}
</code></pre>
<p>Eigentlich glaubte ich, dass dies klappen sollte, wenn ich aber zum bsp.</p>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>r</mi><mo>=</mo><mfrac><mrow><mn>2</mn></mrow><mrow><mn>3</mn></mrow></mfrac></mrow><annotation encoding="application/x-tex">r=\frac{2}{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.190108em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle cramped"><span class="mord scriptstyle cramped"><span class="mord mathrm">3</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathrm">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span> und <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>s</mi><mo>=</mo><mfrac><mrow><mn>5</mn></mrow><mrow><mn>6</mn></mrow></mfrac></mrow><annotation encoding="application/x-tex">s=\frac{5}{6}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.190108em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord mathit">s</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle cramped"><span class="mord scriptstyle cramped"><span class="mord mathrm">6</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathrm">5</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span> eingebe, bekomme ich als output:</p>
<p>The sum of 2 Rationals is -760672256/0.<br />
The subtraction of 2 Rationals is -760672256/0.<br />
The product of 2 Rationals is 470474752/0.<br />
The division of 2 Rationals is -760672256/0.<br />
The equality of 2 Rationals is 0.<br />
The inequalitz of 2 Rationals is 1.</p>
<p>da läuft doch irgendwas schief?</p>
<p>Merci für die Hilfe,</p>
<p>euer Hugo</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/204744/rational-struct</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 18:03:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/204744.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 Feb 2008 17:08:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Rational Struct on Wed, 06 Feb 2008 17:08:36 GMT]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>ich habe mir einen struct gebastelt (klassen kenne ich noch nicht, dass kommt später noch), der den Zahlenkörper <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mrow><mi mathvariant="double-struck">Q</mi></mrow></mrow><annotation encoding="application/x-tex">\mathbb{Q}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.68889em;"></span><span class="strut bottom" style="height:0.85556em;vertical-align:-0.16667em;"></span><span class="base textstyle uncramped"><span class="mord textstyle uncramped"><span class="mord mathbb">Q</span></span></span></span></span> darstellen soll, also die rationalen Zahlen.</p>
<p>Dazu habe ich auch einige Operatoren implementiert. der code sieht wie folgt aus:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;

// Define the new type Rational

struct Rational{
  int n;
  int d; // INV: d!=0
};

// Grundoperatoren

// PRE:  Two value of type Rational
// POST: return value is a Rational representing the summation

Rational operator+ (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d+y.n*x.d;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational 
// POST: return value is a Rational representing the subtraction

Rational operator- (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d-y.n*x.d;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational 
// POST: return value is a Rational representing the product

Rational operator* (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.n;
  result.d=x.d*y.d;
  return result;
} 

// PRE:  Two value of type Rational, y!=0 
// POST: return value is a Rational representing the quotient

Rational operator/ (Rational x, Rational y){
  Rational result;
  result.n=x.n*y.d;
  result.d=x.d*y.n;
  return result;
} 

// Booloperatoren

// PRE:  Two value of type Rational 
// POST: return value is true if a==b

bool operator== (Rational x, Rational y){
  return x.n*y.d==y.n*x.d ;
} 

// PRE:  Two value of type Rational 
// POST: return value is true if a==b

bool operator!= (Rational x, Rational y){
  return !(x.n*y.d==y.n*x.d) ;
}; 

//Rational operator&lt; (Rational x, Rational y){
//  return !(x.n*y.d==y.n*x.d) ;
//} 

// PRE:  A Rational
// POST: Rational has been written on outstream

std::ostream&amp; operator&lt;&lt; (std::ostream&amp; o, Rational x){
  return o &lt;&lt; x.n &lt;&lt; &quot;/&quot; &lt;&lt; x.d;
};

std::istream&amp; operator&gt;&gt; (std::istream&amp; i, Rational x){
  char c; // For the / in input
  return i &gt;&gt; x.n &gt;&gt; c &gt;&gt; x.d;
};

int main ()
{

  // Input

  std::cout &lt;&lt; &quot;Your first rational number r:\n&quot;;
  Rational r;
  std::cin &gt;&gt; r;

  std::cout &lt;&lt; &quot;Your second rational number s:\n&quot;;
  Rational s;
  std::cin &gt;&gt; s;

  // Computation and Output

  std::cout &lt;&lt; &quot; The sum of 2 Rationals is         &quot; &lt;&lt; r+s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The subtraction of 2 Rationals is &quot; &lt;&lt; r-s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The product of 2 Rationals is     &quot; &lt;&lt; r*s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The division of 2 Rationals is    &quot; &lt;&lt; r/s    &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The equality of 2 Rationals is    &quot; &lt;&lt;(r==s)  &lt;&lt; &quot;.\n&quot;;
  std::cout &lt;&lt; &quot; The inequalitz of 2 Rationals is  &quot; &lt;&lt;(r!=s)  &lt;&lt; &quot;.\n&quot;;

 return 0;
}
</code></pre>
<p>Eigentlich glaubte ich, dass dies klappen sollte, wenn ich aber zum bsp.</p>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>r</mi><mo>=</mo><mfrac><mrow><mn>2</mn></mrow><mrow><mn>3</mn></mrow></mfrac></mrow><annotation encoding="application/x-tex">r=\frac{2}{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.190108em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle cramped"><span class="mord scriptstyle cramped"><span class="mord mathrm">3</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathrm">2</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span> und <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>s</mi><mo>=</mo><mfrac><mrow><mn>5</mn></mrow><mrow><mn>6</mn></mrow></mfrac></mrow><annotation encoding="application/x-tex">s=\frac{5}{6}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.845108em;"></span><span class="strut bottom" style="height:1.190108em;vertical-align:-0.345em;"></span><span class="base textstyle uncramped"><span class="mord mathit">s</span><span class="mrel">=</span><span class="mord reset-textstyle textstyle uncramped"><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle cramped"><span class="mord scriptstyle cramped"><span class="mord mathrm">6</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathrm">5</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span> eingebe, bekomme ich als output:</p>
<p>The sum of 2 Rationals is -760672256/0.<br />
The subtraction of 2 Rationals is -760672256/0.<br />
The product of 2 Rationals is 470474752/0.<br />
The division of 2 Rationals is -760672256/0.<br />
The equality of 2 Rationals is 0.<br />
The inequalitz of 2 Rationals is 1.</p>
<p>da läuft doch irgendwas schief?</p>
<p>Merci für die Hilfe,</p>
<p>euer Hugo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1450717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1450717</guid><dc:creator><![CDATA[Hugo B]]></dc:creator><pubDate>Wed, 06 Feb 2008 17:08:36 GMT</pubDate></item><item><title><![CDATA[Reply to Rational Struct on Wed, 06 Feb 2008 17:30:11 GMT]]></title><description><![CDATA[<p>Du übergibst jeweils Kopien der Objekte, anstatt einer Referenz auf diese.<br />
Bei den Rechenoperatoren verschwendest du dadurch lediglich CPU-Zyklen, aber bei</p>
<pre><code class="language-cpp">std::istream&amp; operator&gt;&gt; (std::istream&amp; i, Rational x){
  char c; // For the / in input
  return i &gt;&gt; x.n &gt;&gt; c &gt;&gt; x.d;
}
</code></pre>
<p>werden die Werte in die Kopie geschrieben, welche gleich wieder zerstört wird.<br />
Also &quot;Rational x&quot; jeweils durch &quot;Rational&amp; x&quot; bzw. &quot;const Rational&amp; x&quot; ersetzen und es sollte gehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1450726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1450726</guid><dc:creator><![CDATA[Nanyuki]]></dc:creator><pubDate>Wed, 06 Feb 2008 17:30:11 GMT</pubDate></item><item><title><![CDATA[Reply to Rational Struct on Wed, 06 Feb 2008 17:47:23 GMT]]></title><description><![CDATA[<p>Bingo, habs geändert und siehe da, es läuft merci</p>
<p>Hugo B</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1450736</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1450736</guid><dc:creator><![CDATA[Hugo B]]></dc:creator><pubDate>Wed, 06 Feb 2008 17:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to Rational Struct on Wed, 06 Feb 2008 22:36:27 GMT]]></title><description><![CDATA[<p>Und bitte beachten, dass struct und class in C++ identisch sind (außer default-access: struct =&gt; public; class =&gt; private)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1450925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1450925</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 06 Feb 2008 22:36:27 GMT</pubDate></item></channel></rss>