关于重载运算符的课后作业
本帖最后由 1955207586 于 2019-5-14 17:49 编辑// // test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Math
{
public:
Math();
int son,mom;
Num(int a,int b);
void print();
Math operator+(Math &c);
Math operator-(Math &c);
Math operator*(Math &c);
Math operator/(Math &c);
};
Math::Math()
{
son=0;
mom=1;
}
Math::Num(int a,int b)
{
son=a;
mom=b;
}
void Math::print()
{
cout<<son<<"/"<<mom<<endl;
}
Math Math::operator +(Math &c)
{
return Math::Num(son*c.mom+mom*c.son,mom*c.mom);
}
Math Math::operator -(Math &c)
{
return Math::Num(son*c.mom-mom*c.son,mom*c.mom);
}
Math Math::operator *(Math &c)
{
return Math::Num(son*c.son,mom*c.mom);
}
Math Math::operator /(Math &c)
{
return Math::Num(son*c.mom,mom*c.son);
}
int main(int argc, char* argv[])
{
Math a,b,c;
/*
c=a+b;
c.print();
c=a-b;
c.print();
c=a*b;
c.print();
c=a/b;
c.print();*/
return 0;
}
结果报错D:\C_test\test\test.cpp(35) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
Reason: cannot convert from 'int' to 'const class Math'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(35) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(39) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
Reason: cannot convert from 'int' to 'const class Math'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(39) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(43) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
Reason: cannot convert from 'int' to 'const class Math'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(43) : error C2553: no legal conversion of return value to return type 'class Math *'
D:\C_test\test\test.cpp(47) : error C2664: '__thiscall Math::Math(const class Math &)' : cannot convert parameter 1 from 'int' to 'const class Math &'
Reason: cannot convert from 'int' to 'const class Math'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C_test\test\test.cpp(47) : error C2553: no legal conversion of return value to return type 'class Math *'
执行 cl.exe 时出错.
求大神解答。 本帖最后由 Croper 于 2019-5-14 18:06 编辑
第一个问题、
你”>>"反了,是“<<”好吧,
void Math::print()
{
std::cout<<son<<"/"<<mom<<std::endl;
}
第二个问题、原来的Math()是构造函数
你没头没尾的Num()是什么鬼,系统当然没法识别
第三:作为一个分数,你似乎应该给它加上自动化简
第四:建议养成使用const的习惯,Math Math::operator +(const Math &c) const
{
return Math(son*c.mom+mom*c.son,mom*c.mom);
}
不然你这种表达式Math a,b,c;
Math d=a+(b+c);
将会报错
第五、(建议)构造函数加上分母不能为0的检测代码
本帖最后由 Croper 于 2019-5-14 18:15 编辑
这是我以前写的分数类,你可以参考下//========================================
//fraction.h
//Created by Croper
//last modified in Dec.15.2018
//实现分数类,并通过重载,实现分数类的四则运算以及类型转换
//=========================================================
#pragma once
#include <iostream>
#include <stack>
double fraction_accuracy = 0.000001; //当与实数进行转换时允许的最大误差
class fraction
{
friend const fraction& operator+(const fraction, constfraction);
friend const fraction& operator-(const fraction, constfraction);
friend const fraction& operator*(const fraction, constfraction);
friend const fraction& operator/(const fraction, constfraction);
friend bool operator==(const fraction, constfraction);
friend bool operator!=(const fraction, constfraction);
friend std::ostream &operator<<(std::ostream&, fraction);
private:
long num; //分子
long den; //分母
void Reduce(); //约分
public:
fraction();
fraction(int);
fraction(int,int);
fraction(double);
fraction(const fraction&);
explicit operator int() const; //向下取整
explicit operator long() const;
explicit operator double() const;
explicit operator float() const;
fraction& operator+=(const fraction);
fraction& operator-=(const fraction);
fraction& operator*=(const fraction);
fraction& operator/=(const fraction);
};
void fraction::Reduce() //约分
{
long a;
long p, q;
if (den < 0) {
num = 0 - num;
den = 0 - den;
}
p = num;
q = den;
do //辗转相除求最大公约数
{
a = p % q;
p = q;
q = a;
} while (a != 0);
num/= p;
den/= p;
}
fraction::fraction()
{
num = 0;
den = 1;
}
fraction::fraction(int a)
{
num = a;
den = 1;
}
fraction::fraction(int a, int b)
{
if (b == 0) throw "除数不能为 0" ;
num = a;
den = b;
Reduce();
}
#if 0==1
fraction::fraction(double d)
{
static fraction fsum;
double e;
fsum = (long)floor(d + 0.5);
e = abs((double)fsum - d);
while (abs(e) > abs(fraction_accuracy))
{
double x = 1. / e;
double y = x + 0.5;
int z = (int)floor(y);
fsum += fraction(1,z);
e = d-(double)fsum;
}
num = fsum.num;
den = fsum.den;
}
#endif
//连分数法浮点数化分数
fraction::fraction(double d)
{
std::stack<int> stk,stk1;
static fraction fsum;
long n;
double e,err;
n = (long)floor(d + 0.5);
stk.push(n);
e = d-(double)n;
err = abs(e);
while (err > fraction_accuracy)
{
e = 1. / e;
n = (long)floor(e + 0.5);
e = e - (double)n;
stk.push(n);
stk1 = stk;
fsum = 0;
while (!stk1.empty())
{
n = stk1.top();
stk1.pop();
fsum = 1 / (fsum+n);
}
fsum = 1 / fsum;
err = abs((double)fsum - d);
}
num = fsum.num;
den = fsum.den;
}
fraction::fraction(const fraction& f)
{
num = f.num;
den = f.den;
}
fraction& fraction::operator+=(const fraction f)
{
num = num*f.den + f.num*den;
den = den * f.den;
Reduce();
return *this;
}
fraction& fraction::operator-=(const fraction f)
{
num = num * f.den - f.num*den;
den = den * f.den;
Reduce();
return *this;
}
fraction& fraction::operator*=(const fraction f)
{
num = num * f.num;
den = den * f.den;
Reduce();
return *this;
}
fraction& fraction::operator/=(const fraction f)
{
if (f.num == 0) throw "除数不能为0";
num = num * f.den;
den = den * f.num;
Reduce();
return *this;
}
fraction::operator int() const //按c++取整规则
{
return (num / den);
}
fraction::operator long() const //按c++取整规则
{
return (num / den);
}
fraction::operator double() const
{
return ((double)num / (double)den);
}
fraction::operator float() const
{
return ((float)num / (float)den);
}
const fraction& operator+(const fraction f1, const fraction f2)
{
static fraction f;
f.num = f1.num*f2.den + f2.num*f1.den;
f.den = f1.den*f2.den;
f.Reduce();
return f;
}
const fraction& operator-(const fraction f1, const fraction f2)
{
static fraction f;
f.num = f1.num*f2.den - f2.num*f1.den;
f.den = f1.den*f2.den;
f.Reduce();
return f;
}
const fraction& operator*(const fraction f1, const fraction f2)
{
static fraction f;
f.num = f1.num*f2.num;
f.den = f1.den*f2.den;
f.Reduce();
return f;
}
const fraction& operator/(const fraction f1, const fraction f2)
{
static fraction f;
if (f2.num == 0) throw "除数不能为0";
f.num = f1.num*f2.den;
f.den = f1.den*f2.num;
f.Reduce();
return f;
}
bool operator==(const fraction f1, const fraction f2)
{
return ((f1.num == f2.num) && (f1.den == f2.den));
}
bool operator!=(const fraction f1, const fraction f2)
{
return ((f1.num != f2.num) || (f1.den != f2.den));
}
std::ostream &operator<<(std::ostream& os, fraction f)
{
os << f.num << "/" << f.den;
return os;
}
页:
[1]