【发布时间】:2014-03-30 00:20:03
【问题描述】:
我需要写一个程序来计算分数,这是我的头文件:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
using namespace std;
class Fraction {
private:
int *numer;
int *denom;
int gcd(int, int);
public:
void reduce();
int getNum();
int getDen();
Fraction();
Fraction(int numerator);
Fraction(int num, int den);
Fraction(string s); // take string parameter of the form of "numerator/defnominator
Fraction(Fraction &other); // copy constructor
Fraction & operator=(Fraction & rhs);
~Fraction();
// overloading arithematic operation
Fraction & operator+ (Fraction & rhs);
Fraction & operator- (Fraction & rhs);
Fraction & operator* (Fraction & rhs);
Fraction & operator/ (Fraction & rhs);
bool operator > (Fraction & rhs);
bool operator >= (Fraction & rhs);
bool operator == (Fraction & rhs);
bool operator < (Fraction & rhs);
bool operator <= (Fraction & rhs);
bool operator!=(Fraction & rhs);
Fraction & operator++();
Fraction & operator++(int);
Fraction & operator--();
Fraction & operator--(int);
Fraction & operator+=(Fraction & rhs);
Fraction & operator-=(Fraction & rhs);
Fraction & operator*=(Fraction & rhs);
Fraction & operator/=(Fraction & rhs);
// Exponentiation
Fraction & operator^(int n);
bool isZero();
bool isProper(); // a fracton is proper if abs(numerator) < (denominator)
bool isNegative();
bool isPositive();
operator string();
operator double();
string toString();
string toProperString(); // properString format of 15/4 is 3 3/4
friend ostream & operator << (ostream & out, Fraction & rhs);
friend istream & operator >> (istream & in, Fraction &rhs);
};
#endif
对于 Fraction.cpp 文件:
我有将派系格式转换为字符串的功能,这是我有的,但不确定它是否正确。 这是我现在得到的:
#include <iostream>
#include<cmath>
#include <cassert>
#include <string>
#include<iomanip>
#include <stdlib.h>
#include "Fraction.h"
#include <sstream>
using namespace std;
Fraction::Fraction()
{
numer = new int(0);
denom = new int(1);
//cout<<"construtor"<<endl;
}
int Fraction::getNum()
{
return *numer;
}
int Fraction::getDen()
{
return *denom;
}
int Fraction::gcd(int n, int d) //moving sign to numerator
{
//assert((n > 0 && d > 0)); //ensuring numerator and demominator have no common divisors
//while (n != d) // return the greatest common divisor
//{
// if (n < d)
// d = d - n;
// else
// n = n - d;
//}
//return n;
if(n<0)
n=-n;
if(d<0)
d=-d;
if(n<d)
return gcd(d,n);
if(d==0)
return n;
return gcd(d,n%d);
}
Fraction::Fraction(int numerator)
{
numer = new int(numerator);
denom= new int (1);
//printf("Fraction::Fraction(int numerator) \n");
}
Fraction::Fraction(int num, int den)
{
assert (den != 0);
numer = new int(num);
denom = new int(den);
reduce();
//cout<<"reduce function"<<endl;
}
void Fraction::reduce()
{
int sign = 1;
if (*numer < 0)
{
sign = -1;
*numer = -*numer;
}
if (*numer< 0)
{
sign = -1;
*denom = -*denom;
}
assert(*denom != 0);
int d = 1;
if (*numer>0)
{
d= gcd(*numer, *denom);
*numer = sign*(*numer / d);
*denom = *denom / d;
}
}
Fraction::Fraction(string s)
{
string delimiter = "/";
string n = s.substr(0, s.find(delimiter));
string d=s.substr(s.find(delimiter)+1,s.length());
numer = new int (atoi(n.c_str()));
denom = new int(atoi(d.c_str())); //every first time using pointer in constructor
cout << n << d << endl;
//constructor
}
Fraction::Fraction(Fraction &other)
{
numer = new int( other.getNum());
denom = new int(other.getDen());
//cout<<"copy construtor"<<endl;
}
Fraction::~Fraction()
{
delete numer;
numer = 0; //if (numer) if(denom)
delete denom;
denom = 0;
}
Fraction & Fraction::operator=(Fraction & rhs)
{
if (this != &rhs)
{ //do i need delete pointer here?
*numer = rhs.getNum();
*denom = rhs.getDen();
}
else
return *this;
}
Fraction & Fraction::operator+ (Fraction & rhs)
{
Fraction *result = new Fraction(this->getNum()*rhs.getDen() + this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen());
result->reduce();
return *result;
}
Fraction & Fraction::operator- (Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getDen() - this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen()));
result->reduce();
return *result;
}
Fraction & Fraction::operator*(Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getNum(), this->getDen()*rhs.getDen()));
result->reduce();
return *result;
}
Fraction & Fraction::operator/(Fraction & rhs)
{
Fraction *result=new Fraction((this->getNum()*rhs.getDen(), this->getDen()*rhs.getNum()));
result->reduce();
return *result;
}
bool Fraction::operator > (Fraction & rhs)
{
if (((float)getNum() / getDen())>((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator >= (Fraction & rhs)
{
if (((float)getNum() / getDen()) >= ((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator == (Fraction & rhs)
{
if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
return true;
else
return false;
}
bool Fraction::operator < (Fraction & rhs)
{
if (((float)getNum() / getDen())<((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator <= (Fraction & rhs)
{
if (((float)getNum() / getDen()) <= ((float)rhs.getDen() / rhs.getNum()))
return true;
else
return false;
}
bool Fraction::operator!=(Fraction & rhs)
{
if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
return false;
else
return true;
}
Fraction & Fraction::operator++() //pre fix
{
*numer += *denom;
reduce();
return *this;
}
Fraction & Fraction::operator++(int inInt) //post fix
{
Fraction *temp = new Fraction (*this);
*numer += *denom;
reduce();
return *temp;
}
Fraction & Fraction::operator--()
{
*numer -= *denom;
reduce();
return *this;
}
Fraction & Fraction::operator--(int)
{
Fraction *temp = new Fraction(*this);
*numer -= *denom;
reduce();
return *temp;
}
Fraction & Fraction::operator+=(Fraction & rhs)
{
*numer = (*numer)*rhs.getDen() + (*denom)*rhs.getNum();
*denom = (*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator-=(Fraction & rhs)
{
*numer = (*numer)*rhs.getDen() - (*denom)*rhs.getNum();
*denom = (*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator*=(Fraction & rhs)
{
*numer=(*numer)*rhs.getNum();
*denom=(*denom)*rhs.getDen();
reduce();
return *this;
}
Fraction & Fraction::operator/=(Fraction & rhs)
{
*numer=(*numer)*rhs.getDen();
*denom=(*denom)*rhs.getNum();
reduce();
return *this;
}
Fraction & Fraction::operator^(int n)
{
*numer = (double)pow((float)*numer, n);
*denom = (double)pow((float)*denom, n);
reduce();
return *this;
}
bool Fraction::isZero()
{
return (*numer) == 0;
}
bool Fraction::isProper()
{
if (abs(*numer)<abs(*denom))
return true;
else
return false;
}
bool Fraction::isNegative()
{
if (*numer<0)
return true;
else
return false;
}
bool Fraction::isPositive()
{
if (*numer>0)
return true;
else
return false;
}
Fraction::operator string()
{
return this->toString();
}
Fraction::operator double()
{
return ((double)(*numer) / (*denom));
}
string Fraction::toString()
{
char num[100], deom[100];
char *s = new char[50];
itoa(*numer, num, 10);
itoa(*denom, deom, 10);
char * delimiter = new char[2];
delimiter[0] = '\/';
delimiter[1] = '\0'; //stops copying delimiter
strcpy(s, num);
strcat(s, delimiter);
// strcat(s,'\0');
strcat(s, deom);
// strcat(s,'\0');
return s;
}
string Fraction::toProperString()
{
int a = *(this->numer) / *(this->denom);
int num = *(this->numer) % *(this->denom);
ostringstream ostr;
ostr <<a << " " << num << "/" << *(this->denom);
return ostr.str();
}
ostream & operator << (ostream & out, Fraction & rhs)
{
if(rhs.getDen()!=1)
{
out << rhs.getNum() << "/" << rhs.getDen();
}
else
cout<<rhs.getNum();
return out;
}
istream & operator >> (istream & in, Fraction &rhs)
{
int n, d;
in >> n;
char c;
in >> c;
if (c == '/')
in >> d;
else
{
in.putback(c);
d = 1;
}
rhs = Fraction(n, d);
return in;
}
这是主程序
#include "Fraction.h"
void main()
{
Fraction a1;
Fraction a(1,2);
Fraction b(4,5);
Fraction c(6,8);
Fraction d(b);
Fraction g(-4,8);
Fraction g1(4,-10);
Fraction z(7,5);
cout << a1 << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << g << endl;
cout << g1 << endl;
string s = "3/4";
string s1 = "2/-3";
Fraction b1(s);
Fraction b2(s1);
cout << b1 << endl;
cout << b2 << endl;
a1 = b + c; cout << a1 << endl;
a1 = b-c ; cout << a1 << endl;
a1 = b*c ; cout << a1 << endl;
a1 = b / c; cout << a1 << endl;
b += a; cout << b << endl;
b -= a; cout << b << endl;
b /= a; cout << b << endl;
b++ ; cout << b << endl;
++b ; cout << b << endl;
b-- ; cout << b << endl;
--b ; cout << b << endl;
b /= a; cout << b << endl;
b *a ; cout << b << endl;
b^2 ; cout << b << endl;
cout << a.toString() << endl;
cout << z.toProperString() << endl;
Fraction f1(-4,5);
Fraction f2(0,1);
cout << "is f1 negative?" << f1.isNegative() << endl;
cout << "is f2 zero?" << f2.isZero() << endl;
cout << "is f1 a proper fraction?" << f1.isProper() << endl;
cout << "is f1 a positive fraction? " << f1.isPositive() << endl;
a = Fraction(9, 8);
b = Fraction(7, 8);
if (a < b)
cout << "a is smaller than b" << endl;
else
cout << " a is larger than b" << endl;
if(a==b)
cout << "a is equal to b" << endl;
else
cout << "a does not equal to b" << endl;
}
由于没有人愿意帮助我,所以我把所有的代码都贴在这里给大家。 在我的 TA 的帮助下,我花了一些时间完成了这个项目。
【问题讨论】:
-
头文件是作业的一部分还是你写的?为什么要使用 numer 和 denom 的指针?
-
这是作业的一部分。没有人喜欢指针。
-
operator^是求幂的错误选择,因为它有一个意想不到的优先级。隐式转换函数(尤其是operator double,它会丢失精度)通常不是一个好主意,因为它们可能会在您不期望的时候发生。而且使用指针而不是整数成员简直太疯狂了。我希望这是一个示例,说明如何不设计一个类;否则,你会被灌输一些非常坏的习惯。 -
另外,
operator+等必须返回一个值,而不是引用。返回对局部变量的引用(在函数返回时已被销毁)不仅仅是一个坏主意。这是绝对错误的。更好的方法是放弃你的课程,转而投资good book。 -
嗯,您的帖子实际上并没有提出问题。它只是一堵代码墙。请在帖子中提出问题。
标签: c++ string class operator-keyword