我正在做一个分数分配,我提示用户输入两个分数,然后我加、减、乘和除这两个分数。我知道我仍然需要减少分数,但是我不知道为什么分母要乘以 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;
}