Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ input operator overload ">>"
    primarykey
    data
    text
    <p>I have a rational number class is made up of two integers: <code>num</code>, the nominator, and <code>den</code>, the denominator.</p> <p>The following operator is supposed to read the rational number from a stream.</p> <pre><code>istream&amp; operator &gt;&gt; (istream&amp; Is, rational&amp; r) { char c; //Test char. double n; //Could be either the numerator of the fraction or the antiperiod of the repeating decimal number. Is &gt;&gt; n; int i = 0; for (; n*10-pow(10, i+1) &lt; 1 &amp;&amp; int(n) != 0; i++) { n *= 10; } for (; int(n*10) % 10; i++) { n *= 10; } n /= pow(10, i); if (i == 0) { r.num = n; Is &gt;&gt; ws; c = Is.peek(); if (c == '/') { c = Is.get(); Is &gt;&gt; r.den; } else { r.den = 1; } r.normalize(); //This function normalizes the fraction. } else { Is &gt;&gt; ws; c = Is.peek(); if (c == 'p' || c == 'P') { int p; //Period of the repeating decimal number. c = Is.get(); Is &gt;&gt; p; vector&lt;int&gt; a = genFrac(n, p); //This function returns the fraction which express the decimal repeating number. It returns a "vector&lt;int&gt;" with the nominator at index 1 e denominator at index 0. r.num = a[1]; r.den = a[0]; } else { i = 0; for (; n*10-pow(10, i+1) &lt; 1 &amp;&amp; int(n) != 0; i++) { n *= 10; } for (; int(n*10) % 10 != 0; i++) { n *= 10; } int pot10 = pow(10, i); r.num = n; r.den = pot10; } r.normalize(); } return Is; } </code></pre> <p>I wrote this code to implement the input of my "rational" class. I modified it from the one written in my C++ book in order to make possible the input of decimal numbers, including repeating ones.</p> <p>It should be able to handle these types of input:</p> <ul> <li>9/8</li> <li>9</li> <li>9.87</li> <li>1.p3 (= 1.3333333333)</li> </ul> <p>But it doesn't work, not even the part I copied from the book.</p> <p>Can anyone help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload