0

私は、ユーザーに2つの分数の入力を求め、次に2つの分数を加算、減算、乗算、除算する分数の割り当てに取り組んでいます。分数を減らす必要があることはわかっていますが、分母が10倍になっている理由がわかりません。frac1に1 1/2を入力し、frac2に2 2/5を入力すると、分母は10ではなく100になります。 。とても奇妙な..どんなガイダンスも大歓迎です!:)

// 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;

}
4

1 に答える 1

5

これらの余分なゼロは分母に追加されていません。それらは、あなたがつなぎ合わせている混合分数の全体です。

結果は実際には

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

しかし、あなたはそれらを一行に印刷しています:

0 39/100 -9/100 36/100 15/24
于 2012-10-21T05:55:52.227 に答える