如何在同一行上接受两个有理数输入?

主要目标是让用户输入一行并进行算术和比较。函数相等必须比较两个有理数,看看它们是否相等。但是,我不确定如何同时输入两个数字。我考虑过要使用istream进行操作,但是我不确定如何执行此操作。

例如:输入两个要比较的基本原理: 1/4 3/5

他们不相等!

感谢您的帮助。

// Add appropriate headers

#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include <fstream>

using namespace std;


/*  KEEP THIS COMMENT
* class Rational
*    represents a Rational number. Remember rational means ratio-nal
*    which means there is a numerator and denominator having
*    integer values. Using good ADT techniques,we have made member
*    variable private (also known as instance variables) and made member
*    functions public.
*/
class Rational
{
 private:   
  int numerator;
  int denominator;
  static int counter;
  //int gcd (int a,int b);



 public:
    /*
    // ToDo: Default Constructor
    Rational();

    // ToDo: Constructor that takes int numerator
    Rational(int num);

    // ToDo: Constructor that takes int numerator and int denominator
    Rational(int num,int den);
    */
    // Three constructors to one 
    Rational(int num = 0,int den = 1);


    // ToDo: Member function to read a rational in the form: n/d
    void input();

    // ToDo: Member function to write a rational as n/d
    void output()const;

    // ToDo: declare an accessor function to get the numerator
    int getNumerator()const;

    // ToDo: declare an accessor function to get the denominator
    int getDenominator()const;

    // object counter 'global'
    static int getObjectCounter();

    // ToDo: delcare a function called Sum that takes two rational objects
    // sets the current object to the sum of the given objects using the
    // formula: a/b + c/d = ( a*d + b*c)/(b*sd)
    friend Rational sum(const Rational &a,const Rational &b);

    friend Rational product(const Rational &a,const Rational &b);

    void set(int num,int den);

    //void simplify(int num,int den);

    friend bool isEqual(const Rational &a,const Rational &b);



};
// Add appropriate headers

#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
#include <fstream>

using namespace std;



// ToDO: Implement your class member functions below.

/*
Rational::Rational()
{
   numerator = 0;
   denominator = 1;
}

Rational::Rational(int num)
{
   numerator = num;
   denominator = 1;
}

Rational::Rational(int num,int den)
{
   Rational r; 
   numerator = num;
   denominator = den;
}
*/

int Rational::counter = 0;

//Three constructors to one 
Rational::Rational(int num,int den)
{
    numerator = num;
    denominator = den;
}

Rational sum(const Rational &a,const Rational &b)
//void Rational::sum(const Rational &a,const Rational &b)
{
   Rational temp;
   int num = 0;
   int den = 0;

   //num = (a.getNumerator()*b.getDenominator() + a.getDenominator()*b.getNumerator());
   //den = (a.getDenominator()*b.getDenominator());
   num = (a.numerator*b.denominator + a.denominator*b.numerator);
   den = (a.denominator*b.denominator);

   temp.numerator = num;
   temp.denominator = den;

   return temp;

}

//void Rational::product(const Rational &a,const Rational &b)
Rational product(const Rational &a,const Rational &b)
{
   Rational temp;

   //int num = (a.getNumerator()*b.getNumerator());
   //int den = (a.getDenominator()*b.getDenominator());
   int num = (a.numerator*b.numerator);
   int den = (a.denominator*b.denominator);
   temp.numerator = num;
   temp.denominator = den;

   return temp;
}
void Rational::input()
{
   string in;
   getline(cin,in);
   //cin >> in;
   // find the index position of /
   int indx = in.find("/");
   // seperate the numerator
   numerator = atoi(in.substr(0,indx).c_str());
   // seperate the denominator
   denominator = atoi(in.substr(indx+1,in.length()).c_str());
   //if(denominator != 0)
   Rational(numerator,denominator);

}

void Rational::output()const
{
   cout << numerator << "/" << denominator;
}


void Rational::set(int num,int den)
{
    numerator = num;
    denominator = den;
}



// Two getter functions
int Rational::getNumerator()const
{
   return numerator;
}

int Rational::getDenominator()const
{
   return denominator;
}

/*
void Rational::simplify(int num,int den)
{
    gcd(num,den);
}



int Rational::gcd (int a,int b)
{
   while (a!=0 && b!=0)
   {
      a = a % b;
      if (a!=0)
         b = b % a;
   }
   if (a==0)
      return b;
   if (b==0)
      return a;
}
*/


bool isEqual(const Rational &a,const Rational &b)
{
  if (a.numerator * b.denominator == a.denominator * b.numerator)
  {
    return true;
  }
  else
  {
    return false;
  }
}


int Rational::getObjectCounter()
{
    return counter;
}
// Add appropriate headers

#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
#include <fstream>

using namespace std;


int main()
{
    // ToDo: declare three rational objects using the default constructor
    Rational a,b,c;
    int length;
    char answer='Y';

    // Main loop to read in rationals and compute the sum
    do {
        cout << "Enter the number of fractions to add/multiply: ";
        cin >> length;
        Rational *fractions = new Rational[length];

        for (int i = 0; i < length; i++)
        {
          fractions[i].set (1,i+1);
          fractions[i].output();
          cout << " " << endl;
        }



        Rational r_sum,r_product(1);
        //tempsum = fractions[0];
        //tempproduct = fractions[0];
        for (int i = 0; i < length; i++)
        {
          r_sum = sum(r_sum,fractions[i]); // test r.sum
          r_product = product(r_product,fractions[i]); // test
        }
        //simplify


         cout << "Sum of fractions: "; 
         r_sum.output();
         cout << " " << endl;
         cout << "Prod of fractions: "; 
         r_product.output();
         cout << " " << endl;
        // ToDo: use your input member function to read the first rational

        ifstream input1;
        string one;
        string two;
        cout << "Enter two rationals to compare: ";
        input1 >> one >> two;

        //a.input();
        //b.input();
        a.output(); // Debug line
        cout << " " << endl; // Debug line
        b.output(); // Debug line
        /*
        while (a.getDenominator() == 0 || b.getDenominator() == 0)
        {
            cout << "wrong input. \n";
            cout << "Enter a rational (p/q): \n";
            a.input();
            b.input();
        }s
        */

        if (isEqual(a,b) == true)
        {
            a.output();
            cout << " is equal to ";
            b.output();
        }
        else
        {
            a.output();
            cout << " is not equal to ";
            b.output();
        }
    //  cout << "Total number of objects created = " << << endl;

        cout << "\nTry again (Y/N)?\n";
        cin >> answer;
        cin.ignore(256,'\n');

    } while (answer == 'y' || answer == 'Y');



    return 0;
}


wangkai5260929 回答:如何在同一行上接受两个有理数输入?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3131427.html

大家都在问