gpt4 book ai didi

c++ - 重载运算符 C++ : Why is denominator being multiplied by 10

转载 作者:太空宇宙 更新时间:2023-11-04 16:24:56 26 4
gpt4 key购买 nike

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

关于c++ - 重载运算符 C++ : Why is denominator being multiplied by 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com