我正在进行分数分配,在该提示下,提示用户输入两个分数,然后对两个分数进行加,减,乘和除。我知道我仍然需要减少分数,但是我无法弄清楚为什么将分母乘以10。如果我为frac1输入1 1/2,为frac2输入2 2/5,则分母为100而不是10所以很奇怪..任何指导表示赞赏! :)

// fraction9.cpp : main project file.
#include "stdafx.h"
#include<conio.h>
#include<iostream>
 #include<string>
using namespace std;


class Fraction
{
private:
   int whole;
   int numerator;       // may be any integer
   int denominator;     // should always be positive
public:
 Fraction();    // Set numerator = 0, denominator = 1.
 Fraction(int w, int n, int d=1);   // constructor with parameters
            //  acts as conversion constructor

 // operator overload as member
 Fraction operator+ (const Fraction& f) const;
 Fraction operator- (const Fraction& f) const;
 Fraction operator* (const Fraction& f) const;
 Fraction operator/ (const Fraction& f) const;

 // standard input/output routines
 void Input();      // input a fraction from keyboard.
 void Show() const;     // Display a fraction on screen

 // accessors
 int GetWhole() const;
 int GetNumerator() const;
 int GetDenominator() const;

 // mutator
   bool SetValue(int w, int n, int d=1); // set the fraction's value through parameters
   //double Evaluate() const;   // Return the decimal value of a fraction


};

Fraction Fraction::operator+(const Fraction& f) const
// operator overload for + written as member function
{
   Fraction r;      // result
   r.numerator = (denominator * whole + numerator) * f.denominator + (f.denominator *     f.whole + f.numerator) * denominator;
   r.denominator = (denominator * f.denominator);
   return r;
}

Fraction Fraction::operator-(const Fraction& f) const
// operator overload for - written as member function
{
   Fraction r;      // result
   r.numerator = (denominator * whole + numerator) * f.denominator - (f.denominator *     f.whole + f.numerator) * denominator;
   r.denominator = (denominator * f.denominator);
   return r;
}

// operator overload for * written as member function
Fraction Fraction::operator*(const Fraction& f) const
{
   Fraction r;      // result
   r.numerator = ((denominator * whole + numerator) * (f.denominator * f.whole +     f.numerator));
   r.denominator = (denominator * f.denominator);
   return r;
}

// operator overload for / written as member function
Fraction Fraction::operator/(const Fraction& f) const
{
   Fraction r;      // result
   r.numerator = ((denominator * whole + numerator) * f.denominator);
   r.denominator = (denominator * (f.denominator * f.whole + f.numerator));
   return r;
}

Fraction::Fraction()
// Default constructor.  Initializes fraction to 0/1
{
   whole = 0;
   numerator = 0;
   denominator = 1;
}

Fraction::Fraction(int w, int n, int d)
// initializes fraction to n/d (defaults to 0/1 if invalid data)
{
   if (SetValue(w, n, d) == false)
  SetValue(0, 0, 1);
}

void Fraction::Input()
{
    cout << "Enter whole number ";
  cin >> whole;
  cout << "Enter numerator " ;
  cin >> numerator;
  cout << "Enter denominator ";
  cin >> denominator;
      if (denominator <= 0)
  cout << "*** Denominator must be positive.  Try again: ";
  while (denominator <= 0)
    {
        cout << "Please re-enter the denominator of the fraction, it cannot be 0" <<     endl;
        cin >> denominator;
    };
}

void Fraction::Show() const
// Display a fraction
{
   cout << whole << " " << numerator << '/' << denominator;
}

int Fraction::GetWhole() const
{
    return whole;
}

int Fraction::GetNumerator() const
{
   return numerator;
}

int Fraction::GetDenominator() const
{
   return denominator;
}

bool Fraction::SetValue(int w, int n, int d)
// sets fraction to n/d and returns true for success (good data)
// returns false and leaves fraction alone if bad data
{
   if (d <= 0)
    return false;

   numerator = n;
   denominator = d;
   return true;
}

// Driver routine to test the Fraction class

int main()
{

Fraction f1, f2;
cout << "Enter first fraction: ";
f1.Input();
cout << "\n You entered ";
f1.Show();
cout << "\n Enter second fraction: ";
f2.Input();
cout << "You entered ";
f2.Show();
cout << "\n The sum of the first and second fraction is ";
Fraction Aresult;
Aresult = f1 + f2;
Fraction Sresult;
Sresult = f1 - f2;
Fraction Mresult;
Mresult = f1 * f2;
Fraction Dresult;
Dresult = f1 / f2;
Aresult.Show();
Sresult.Show();
Mresult.Show();
Dresult.Show();
cout << '\n';
_getch();
return 0;

}

最佳答案

这些额外的零不会被附加到分母上。它们是您要串在一起的混合分数的全部。

结果实际上是

0 39/10
0 -9/10
0 36/10
0 15/24


但您将它们打印在一行上:

0 39/100 -9/100 36/100 15/24

09-27 06:59