Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I'd write this somewhat differently<sup>1</sup>. </p> <p>Unless you really need to do otherwise, I'd start by reading an entire "chunk" of input (i.e., all the characters up to the next white space), then sort out how that's supposed to represent a number, and call a separate function for each possible representation:</p> <pre><code>std::istream &amp;operator&gt;&gt;(std::istream &amp;is, rational &amp;r) { std::string temp; Is &gt;&gt; temp; if (temp.find('/') != std::string::npos) r = cvt_fraction(temp, Is); else if (temp.find_first_of("pP") != std::string::npos) r = cvt_repeating(temp, Is); else if (temp.find('.') != std::string::npos) r = cvt_float(temp, Is); else r = cvt_int(temp, Is); return Is; } </code></pre> <p>I've passed the istream to each for two reasons: first, so if they find garbage in the input, they can set the stream's fail bit. Second, so <em>if</em> they really do need to read more input, they can (but I'd be a little surprised if that's ever really needed).</p> <p>It seems to me that each of those conversion functions should be fairly trivial: if I'm starting from the fact that a string should be <code>digits "/" digits</code> or `digits "p" digits", doing a conversion is generally going to be pretty simple -- specifically, simple enough that I think just about anybody can probably glance at the code and sort out what each piece is supposed to do.</p> <hr> <ol> <li>I honestly don't mean to be nasty, but if I was maintaining code, and ran across your <code>operator&gt;&gt;</code>, I would have one of two possible reactions: if it apparently had a bug, replace it immediately. Otherwise, put it on the "technical debt" list, and replace it as soon as possible. The simple fact is that as it stands right now, it takes a fair amount of study to even be sure what input formats are supposed to be supported, not to mention which part of the code handles each, or how the whole thing is supposed to work together to produce a meaningful result.</li> </ol>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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