Parsing Rational Numbers

  1. Enhance your class of rational numbers, so that in addition to the constructor CRational(int numerator, int denominator), it supports another constructor which takes a string in the format like "3/4".
  2. This new constructor will automatically parse the string and find out the numerator portion and the denominator portion, and assign the corresponding values to data members.
  3. You may test your class with the following main program:
    #include <iostream>
    #include "rational.h"
    using std::cout;
    
    int main() {
        CRational a(2, 8);
        CRational b("3/4");
        CRational d("2");
        cout << a << endl;
        cout << a + b << endl;
        cout << d - a << endl;
        return 0;
    }  
  4. The result should be
    1/4
    1
    7/4
  5. Now, design your main program so that it will read an infinite number of lines from the user. The format of the line is an expression of addition for two rational numbers (e.g. "1/4 + 3/4"). For simplicity, you may assume that the addition operator is surrounded by a space from both sides.
  6. Your program should be able to perform a series of addition operations and print out the result.
  7. When there is no further input, the user press Ctrl+D to end the program.
  8. You may use the member functions find() and substr() in a string for parsing.
  9. To convert a string to an integer, you may use the stoi() function.
  10. For simplicity, let us assume that there is no illegal input in this exercise. That is, the formats of all input expressions are correct.
  11. Note that you should reduce a rational number before it is printed out. Therefore, for the input
    1/4 + 1/4
    3 + 6/2
    6/8 + 6/24  
    the running result should look like
    1/2
    6
    1  
  12. Submit both your "rational.h" and "main.cpp".