gpt4 book ai didi

c++ - 如何修复分数类的重载 + 运算符的输出?

转载 作者:行者123 更新时间:2023-11-30 05:24:43 24 4
gpt4 key购买 nike

我对我的 fractionType 类有疑问。我正在为赋值编码,赋值要求我重载基本算术运算符(+、-、/、*)、逻辑运算符(==、<、>、=<、=>)和系统运算符(<< , >>)到目前为止,我有系统运算符在工作,以及乘法和除法运算符。当我测试 + 和 - 运算符时,我的问题出现了。我的输入被硬编码为 2/4 和 1/8,但我的输出是 3/8。这是不对的。

我的代码来自 fractionType.cpp

#include <iostream>
#include "fractionType.h"

fractionType::fractionType(){
}
fractionType::fractionType(int a, int b) {
numerator = a;
denominator = b;
}
fractionType fractionType::operator+(fractionType fraction) {
fractionType anotherFraction;
if (denominator = fraction.denominator) {

anotherFraction.numerator = numerator + fraction.numerator;

anotherFraction.denominator = denominator;
}
if (denominator != fraction.denominator) {
anotherFraction.numerator = ((numerator) * (fraction.denominator)) + ((fraction.numerator) * (denominator));

anotherFraction.denominator = denominator * fraction.denominator;
}
return anotherFraction;
}
fractionType fractionType::operator-(fractionType fraction) {

fractionType anotherFraction;

if (denominator = fraction.denominator) {

anotherFraction.numerator = numerator - fraction.numerator;

anotherFraction.denominator = denominator;
}
else {
anotherFraction.numerator = (numerator * fraction.denominator) - (fraction.numerator * fraction.denominator);

anotherFraction.denominator = denominator * fraction.denominator;
}

return anotherFraction;


}

我的代码来自 fractionType.h

#include <iostream>

class fractionType {
public:
fractionType(int a, int b);
fractionType();
fractionType operator*(fractionType fraction);
fractionType operator/(fractionType fraction);
fractionType operator+(fractionType fraction);
fractionType operator-(fractionType fraction);
friend std::ostream& operator<<(std::ostream& out, const fractionType &fraction) {
out << fraction.numerator << '/' << fraction.denominator;
return out;
}
friend std::istream& operator>> (std::istream& in, fractionType &fraction) {
char c;
in >> fraction.numerator >> c >> fraction.denominator;
return in;
}
private:
int numerator, denominator;
};
#endif FRACTIONTYPE_H

和我的 source.cpp

#include <iostream>
#include "fractionType.h"

int main() {
fractionType aFraction(2, 4);
fractionType bFraction(1, 8);
fractionType cFraction;
cFraction = aFraction + bFraction;
std::cout << cFraction << std::endl;
system("PAUSE");
}

我删除了我的 operator* 和 operator/,因为它们可以正常工作。我不知道我做错了什么,在我发布这个问题之前我已经一遍又一遍地复习数学。是因为我没有使用指针吗?常量?或者它有什么不同?在此先感谢您的帮助!

最佳答案

这一行是错误的:

if (denominator = fraction.denominator) {

必须是

if (denominator == fraction.denominator) {

如果启用警告,clang 和 gcc 会告诉您,例如:

main.cpp:32:19: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]

if (denominator = fraction.denominator) {

~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

live example

如果您更正了上述错误,您的代码就可以工作了:

输出

20/32

live example

关于c++ - 如何修复分数类的重载 + 运算符的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560233/

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