メンバー関数を使用して、2 つの有理数 (分数) 数 (4 つの値) を入力し、メンバー関数で 2 つの変数のみを使用できるようにしようとしています。分子と分母。これを機能させることはできません。
以下のコードを使用すると、次のように表示されます: 1/2 と 1/4 を入力
し、「2」を選択して追加すると、分子 = 1 分母 = 1 r.分子 = 1 r.分母 = 2 を受け取ります。
そのため、入力関数で変数を出力すると、1/2 と 1/4 が取得されます。そのため、1/2 がロードされて Add に渡され、1/4 には渡されない理由がわかりません。
どうぞよろしくお願いいたします。
ありがとうございました!
以下は未完成の状態のコードですが、コンパイルして実行します。
#include <iostream>
using namespace std;
#include <conio.h>
class Rational
{
public:
Rational();
Rational(int numerator, int denominator);
Rational Input();
void Add(Rational r);
void Subtract(Rational r);
void Multiply(Rational r);
void Divide(Rational r);
void printRational();
void printRationalAsDouble();
Rational Reduction();
private:
int numerator, denominator;
};
Rational::Rational()
{
numerator = 1;
denominator = 1;
}
Rational Rational::Input()
{
Rational r;
cout<<"Enter two fractions. The denominator cannot be 0."<<endl;
cout<<"Enter first numerator: "<<endl; cin >> numerator;
cout<<"Enter first denominator: " <<endl; cin >> denominator;
if (denominator == 0)
{
cout<<"Please enter a valid denominator: "<<endl; cin >> denominator;
}
cout<<"Enter second numerator: "<<endl; cin >> r.numerator;
cout<<"Enter second denominator: "<<endl; cin >> r.denominator;
if (r.denominator == 0)
{
cout<<"Please enter a valid denominator: "<<endl; cin >> r.denominator;
}
cout<<endl;
cout << numerator << "/" << denominator << " " << r.numerator
<< "/" << r.denominator <<endl;
cout<<endl;
return(r);
}
void Rational::Add(Rational r)
{
Rational Result, x;
Result.numerator = (numerator * r.denominator) + (r.numerator * denominator);
Result.denominator = denominator * r.denominator;
cout << "numerator:" << numerator << endl;
cout << "denominator:" << denominator << endl;
cout << "r.nuerator:" << r.numerator << endl;
cout << "r.denominator:" << r.denominator << endl;
Result.Reduction();
Result.printRational();
Result.printRationalAsDouble();
}
void Rational::Subtract(Rational r)
{
Rational Result;
Result.numerator = (numerator * r.numerator) - (r.numerator * denominator);
Result.denominator = denominator * r.denominator;
Result.Reduction();
Result.printRational();
Result.printRationalAsDouble();
}
void Rational::Multiply(Rational r)
{
Rational Result;
Result.numerator = numerator * r.numerator;
Result.denominator = denominator * r.denominator;
Result.Reduction();
Result.printRational();
Result.printRationalAsDouble();
}
void Rational::Divide(Rational r)
{
Rational Result;
Result.numerator = numerator * r.denominator;
Result.denominator = denominator * r.numerator;
Result.Reduction();
Result.printRational();
Result.printRationalAsDouble();
}
void Rational::printRational()
{
cout << numerator << "/" << denominator << " Decimal:";
}
void Rational::printRationalAsDouble()
{
cout << (double) numerator / denominator << endl;
}
Rational Rational::Reduction()
{
Rational Result;
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 1;
for (int i = 2; i <= largest; i++)
if(numerator % i == 0 && denominator % i == 0)
gcd = i;
if(gcd != 0)
{
Result.numerator = (numerator /= gcd);
Result.denominator = (denominator /= gcd);
}
return(Result);
}
int main()
{
cout <<"*****************************************************************************"
<< endl;
cout <<"* This program will add two fractions, subract two fractions, *"
<< endl; //Intro message to the user
cout <<"* multiply two imputed fractions, or divide two fractions. *"
<< endl;
cout <<"*****************************************************************************"
<< endl;
int choice = 0;
Rational x, A, S, M, D;
while(choice != 6)
{
cout << "1. Enter two fractions." <<endl;
cout << "2. Add and display result." <<endl;
cout << "3. Subtract and display result." <<endl;
cout << "4. Multiply and display result." <<endl;
cout << "5. Divide and display result." <<endl;
cout << "6. Quit " <<endl;
cout <<"Selection choice: "<<endl;
cin >> choice;
if(choice == 1)
{
x.Input();
}
else if(choice == 2)
{
A.Add(x);
}
else if(choice == 3)
{
cout << "#3";
}
else if(choice == 4)
{
cout << "#4";
}
else if(choice == 5)
{
cout << "#5";
}
}
return(0);
}