| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1038 人关注过本帖
标题:C++程序求教
只看楼主 加入收藏
野人王
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-4-7
收藏
 问题点数:0 回复次数:6 
C++程序求教
题目:
   自定义分数类fraction,使用该类可以完成分数的输入、分数的加、减、乘、除二目运算和
一目减运算、分数的约分操作、分数的通分操作、分数的倒数运算、对两个分数进行六种比较
运算、以及对分数的输出等操作。
  将其中使用的普通函数尽量改写成运算符重载函数。如,可重载+、-、*
、/、>、>=、<、<=、==、!=,以实现分数的各种运算。并且实现分数与整数的混合运算。
从分数类派生出一个用来描述整型的Integer类,完善整型类Integer,使其能完成整数的各
种操作。为分数类fraction和整型类Integer各增加一个成员函数digit_number(),用来统计一
个分数中数字字符的数量(如:13/34,数字字符的数量为4)和一个整数中的数字字符的数量

代码如下:
//fraction.h
#ifndef FRACTION_H
#define FRACTION_H
class fraction
{
public:
    fraction()
    {
        numerator = 0;
        denominator = 1;
    }
//    fraction(int m)
//    {
//        numerator = m;
//        denominator = 1;
//    }
    int maxdiv(int a, int b); // get the max divsion of |a| && |b|
    void error();             //deal with errors;
    void get_frc();           //the the value of the fraction
    void display();           //output the fraction
    void sim_frc();           //simplied the fraction
    template <class T>
    int uni_frc(T &f2);  // get the smallest  mutilple of the two denominators
    template <class T>
    T operator+(T f2);  // + f2
    template <class T>
    T operator-(T f2);  // - f2
    template <class T>
    T operator*(T f2);  // * f2
    template <class T>
    T operator/(T f2);  //  / f2
    fraction rec_frc();              // reciprocal
    void operator--();               // --
    template <class T>
    bool operator>(T f2);   //  ">"
    template <class T>
    bool operator>=(T f2);   //  ">="
    template <class T>
    bool operator<(T f2);   //  "<"
    template <class T>
    bool operator<=(T f2);   //  "<="
    template <class T>
    bool operator==(T f2);   //  "=="
    template <class T>
    bool operator!=(T f2);   //  "!="
    int digit_number();
protected:
    int numerator;
    int denominator;
};
#endif

//fra_function.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

int fraction::maxdiv(int a, int b) // get the max divsion of |a| && |b|
{
    int c,t;
    if (a < 0)
        a = -a;
    if(b < 0)
        b = -b;
    if (a < b)
    {
        t = a;
        a = b;
        b = t;
    }
    c = a % b;
    while (c != 0)
    {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

void fraction:: error()  // deal with errors;
{
    if(denominator == 0)
    {
        cout << "Error:denominator cannot be zero !" << endl;
        exit(0);
    }
}

void fraction::get_frc()  // the the value of the fraction
{
    cout << "numerator: ";
    cin >> numerator;
    cout << "denominator: ";
    cin >> denominator;
    error();
    sim_frc();
}

void fraction::display()  //output the fraction
{
    if(denominator == 1 || numerator == 0)
        cout << numerator << endl;
    else
        cout << numerator << "/" << denominator << endl;
}

void fraction::sim_frc()  //simplied the fraction
{
    int a, b;
    a = denominator;
    b = numerator;
    if (a*b != 0)
    {
        denominator /= maxdiv(a,b);
        numerator /= maxdiv(a,b);
    }
    if(denominator < 0)      // make the denominator be positive
    {
        numerator = - numerator;
        denominator = - denominator;
    }
}


template <class T>
T fraction::operator-(T f2)  // - f2
{
    T f;
    numerator *= uni_frc(f2) / denominator;
    f2.numerator *= uni_frc(f2) / f2.denominator;
    f.numerator = numerator - f2.numerator;
    f.denominator = uni_frc(f2);
    f.sim_frc();
    return f;
}

template <class T>
T fraction::operator*(T f2)  // * f2
{
    T f;
    f.numerator = numerator * f2.numerator;
    f.denominator = denominator * f2.denominator;
    f.sim_frc();
    return f;
}

template <class T>
T fraction::operator/(T f2)  //  / f2
{
    T f;
    f.numerator = numerator * f2.denominator;
    f.denominator = denominator * f2.numerator;
    f.error();
    f.sim_frc();
    return f;
}

fraction fraction::rec_frc()  // reciprocal
{
    fraction f;
    f.numerator = denominator;
    f.denominator = numerator;
    f.error();
    f.sim_frc();
    return f;
}

void fraction::operator--()   // --
{
    numerator -= denominator;
}

template <class T>
bool fraction::operator>(T f2)  //  ">"
{
    T f;
    f = operator-(f2);
    if (f.numerator > 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction::operator>=(T f2)  //  ">="
{
    T f;
    f = operator-(f2);
    if (f.numerator >= 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction::operator<(T f2) //  "<"
{
    T f;
    f = operator-(f2);
    if (f.numerator < 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction::operator<=(T f2)  //  "<="
{
    T f;
    f = operator-(f2);
    if (f.numerator <= 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction::operator==(T f2)  //  "=="
{
    T f;
    f = operator-(f2);
    if (f.numerator == 0)
        return true;
    else
        return false;
}

template <class T>
int fraction::uni_frc(T &f2)   // the min mulitple of the denominators
{
    return denominator * f2.denominator / maxdiv(denominator, f2.denominator);
}

template <class T>
T fraction::operator+ (T f2)    // + f2
{
    T f;
    numerator *= uni_frc(f2) / denominator;
    f2.numerator *= uni_frc(f2) / f2.denominator;
    f.numerator = numerator + f2.numerator;
    f.denominator = uni_frc(f2);
    f.sim_frc();
    return f;
}

template <class T>
bool fraction::operator!=(T f2)  //  "!="
{
    T f;
    f = operator-(f2);
    if (f.numerator != 0)
        return true;
    else
        return false;
}

int fraction::digit_number()
{
    int i,j,count = 0;
    i = numerator;
    j = denominator;
    while(i != 0)
    {
        count++;
        i /= 10;
    }
    while(j != 0)
    {
        count++;
        j /= 10;
    }
    return count;
}

//Integer.h
#include "fraction.h"

class Integer:public fraction
{
public:
    Integer(int n = 1):denominator(n)
    {
        numerator = 0;

    }
    void get_Int();
    void display();
    int digit_number();
protected:
    const int denominator ;
};

//Int_function.cpp
#include <iostream>
#include "Integer.h"
using namespace std;

void Integer::get_Int()
{
    cout << "Integer: ";
    cin >> numerator;
}

void Integer::display()
{
    cout << "The Integer is:" << numerator <<endl;
}

int Integer::digit_number()
{
    int k, count = 0;
    k = numerator;
    while (k != 0)
    {
        count++;
        k /= 10;
    }
    return count;
}

//main.cpp
//JUST FOR TEST
#include <iostream>
#include "Integer.h"
using namespace std;

int main()
{
    fraction i1,i2,i3;
    i1.get_frc();
    i2.get_frc();
    i1.display();
     i3= i1 + i2;
    i3.display();
    return 0;
}
搜索更多相关主题的帖子: 倒数 
2008-03-31 16:47
野人王
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-4-7
收藏
得分:0 
各位帮忙看看哪里错了啊,我是新人,多多关照啊,是不是模板的定义有问题啊
2008-03-31 16:48
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你用vc++6.0吗?6.0好像好像不支持在类里面支持函数模板。。只支持类模板。。。我改了大概。。。。。
#include <iostream>
using namespace std;
//#ifndef FRACTION_H
//#define FRACTION_H
template <class T>
class fraction
{
public:
    fraction()
    {
        numerator = 0;
        denominator = 1;
    }
    int maxdiv(int a, int b); // get the max divsion of |a| && |b|
    void error();             //deal with errors;
    void get_frc();           //the the value of the fraction
    void display();           //output the fraction
    void sim_frc();           //simplied the fraction
    //template <class T>
    int uni_frc(T &f2);  // get the smallest  mutilple of the two denominators
    //template <class T>
    T operator+(fraction<T>& f2);  // + f2
    //template <class T>
    T operator-(T f2);  // - f2
    //template <class T>
    T operator*(T f2);  // * f2
    //template <class T>
    T operator/(T f2);  //  / f2

    fraction rec_frc();              // reciprocal
    void operator--();               // --
    //template <class T>
    bool operator>(T f2);   //  ">"
    //template <class T>
    bool operator>=(T f2);   //  ">="
   // template <class T>
    bool operator<(T f2);   //  "<"
    //template <class T>
    bool operator<=(T f2);   //  "<="
   // template <class T>
    bool operator==(T f2);   //  "=="
   // template <class T>
    bool operator!=(T f2);   //  "!="
    int digit_number();
protected:
    int numerator;
    int denominator;
};
//#endif
template <class T>
int fraction<T>::maxdiv(int a, int b) // get the max divsion of |a| && |b|
{
    int c,t;
    if (a < 0)
        a = -a;
    if(b < 0)
        b = -b;
    if (a < b)
    {
        t = a;
        a = b;
        b = t;
    }
    c = a % b;
    while (c != 0)
    {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}
template <class T>
void fraction<T>:: error()  // deal with errors;
{
    if(denominator == 0)
    {
        cout << "Error:denominator cannot be zero !" << endl;
        exit(0);
    }
}
template <class T>
void fraction<T>::get_frc()  // the the value of the fraction
{
    cout << "numerator: ";
    cin >> numerator;
    cout << "denominator: ";
    cin >> denominator;
    error();
    sim_frc();
}
template <class T>
void fraction<T>::display()  //output the fraction
{
    if(denominator == 1 || numerator == 0)
        cout << numerator << endl;
    else
        cout << numerator << "/" << denominator << endl;
}
template <class T>
void fraction<T>::sim_frc()  //simplied the fraction
{
    int a, b;
    a = denominator;
    b = numerator;
    if (a*b != 0)
    {
        denominator /= maxdiv(a,b);
        numerator /= maxdiv(a,b);
    }
    if(denominator < 0)      // make the denominator be positive
    {
        numerator = - numerator;
        denominator = - denominator;
    }
}


template <class T>
T fraction<T>::operator-(T f2)  // - f2
{
    T f;
    numerator *= uni_frc(f2) / denominator;
    f2.numerator *= uni_frc(f2) / f2.denominator;
    f.numerator = numerator - f2.numerator;
    f.denominator = uni_frc(f2);
    f.sim_frc();
    return f;
}

template <class T>
T fraction<T>::operator*(T f2)  // * f2
{
    T f;
    f.numerator = numerator * f2.numerator;
    f.denominator = denominator * f2.denominator;
    f.sim_frc();
    return f;
}

template <class T>
T fraction<T>::operator/(T f2)  //  / f2
{
    T f;
    f.numerator = numerator * f2.denominator;
    f.denominator = denominator * f2.numerator;
    f.error();
    f.sim_frc();
    return f;
}
template <class T>
fraction<T> fraction<T>::rec_frc()  // reciprocal
{
    fraction f;
    f.numerator = denominator;
    f.denominator = numerator;
    f.error();
    f.sim_frc();
    return f;
}
template <class T>
void fraction<T>::operator--()   // --
{
    numerator -= denominator;
}

template <class T>
bool fraction<T>::operator>(T f2)  //  ">"
{
    T f;
    f = operator-(f2);
    if (f.numerator > 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction<T>::operator>=(T f2)  //  ">="
{
    T f;
    f = operator-(f2);
    if (f.numerator >= 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction<T>::operator<(T f2) //  "<"
{
    T f;
    f = operator-(f2);
    if (f.numerator < 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction<T>::operator<=(T f2)  //  "<="
{
    T f;
    f = operator-(f2);
    if (f.numerator <= 0)
        return true;
    else
        return false;
}

template <class T>
bool fraction<T>::operator==(T f2)  //  "=="
{
    T f;
    f = operator-(f2);
    if (f.numerator == 0)
        return true;
    else
        return false;
}

template <class T>
int fraction<T>::uni_frc(T &f2)   // the min mulitple of the denominators
{
    return denominator * f2.denominator / maxdiv(denominator, f2.denominator);
}

template <class T>
T fraction<T>::operator+(fraction<T>& f2)    // + f2
{
    T f;
    numerator *= uni_frc(f2) / denominator;
    f2.numerator *= uni_frc(f2) / f2.denominator;
    f.numerator = numerator + f2.numerator;
    f.denominator = uni_frc(f2);
    f.sim_frc();
    return f;
}

template <class T>
bool fraction<T>::operator!=(T f2)  //  "!="
{
    T f;
    f = operator-(f2);
    if (f.numerator != 0)
        return true;
    else
        return false;
}
template <class T>
int fraction<T>::digit_number()
{
    int i,j,count = 0;
    i = numerator;
    j = denominator;
    while(i != 0)
    {
        count++;
        i /= 10;
    }
    while(j != 0)
    {
        count++;
        j /= 10;
    }
    return count;
}


template <class T>
class Integer:public fraction<T>
{
public:
    Integer(int n = 1):denominator(n)
    {
        numerator = 0;

    }
    void get_Int();
    void display();
    int digit_number();
protected:
    const int denominator ;
};
template <class T>
void Integer<T>::get_Int()
{
    cout << "Integer: ";
    cin >> numerator;
}
template <class T>
void Integer<T>::display()
{
    cout << "The Integer is:" << numerator <<endl;
}
template <class T>
int Integer<T>::digit_number()
{
    int k, count = 0;
    k = numerator;
    while (k != 0)
    {
        count++;
        k /= 10;
    }
    return count;
}

int main()
{
    fraction<int> i1,i2,i3;
    i1.get_frc();
    i2.get_frc();
    i1.display();
     i3= i1 + i2;
    i3.display();
    return 0;
}

学习需要安静。。海盗要重新来过。。
2008-03-31 17:44
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
把你的错误提示发上来看一下。我用VS2008也有错!

[[it] 本帖最后由 zjl138 于 2008-3-31 17:58 编辑 [/it]]

i like linux...
2008-03-31 17:55
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
程序一长。。就需要注意了。。我只是改了大概。。。基本运行了。。。但是里面其它还有问题。。。同时没重载=

学习需要安静。。海盗要重新来过。。
2008-03-31 18:26
野人王
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-4-7
收藏
得分:0 
回复 4# 的帖子
Deleting intermediate files and output files for project 'main - Win32 Debug'.
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: class fraction __thiscall fraction::operator+(class fraction)" (??Hfraction@@QAE?AV0@V0@@Z)
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall fraction::display(void)" (?display@fraction@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall fraction::get_frc(void)" (?get_frc@fraction@@QAEXXZ)
Debug/main.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

main.exe - 4 error(s), 0 warning(s)
在重载的时候出现这样的错误,有人说模板函数的declare和implement不能放在不同的文件中的,但是我把它们放在同一文件中还是有相同的错误
2008-03-31 19:38
acrobatyuer
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-3-28
收藏
得分:0 
飘过.....


还没学到模板和重载呢...
2008-04-01 10:40
快速回复:C++程序求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032232 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved