用C++编写两个类

来源:百度知道 编辑:UC知道 时间:2024/07/04 14:18:36
用C++编写两个类,一个有理数类,一个集合类,要求分别实现有理数的基本运算就行,集合类也是,实现基本运算就好,程序不需要多复杂,简简单单就好,急!!!!!!!!!!救命的!!!!!在线等!!!
呃……不用那么麻烦…就是刚学C++的人编的程……谢谢

1.

#include<iostream>
using namespace std;

class number
{
int fenzi;
int fenmu;
public:
number(int m=0,int n=0);
void yuefen(int x,int y);
void zhuanhuan(int g,int h);
number operator +(number a2);
number operator -(number a2);
number operator *(number a2);
number operator /(number a2);
void show();

};

number::number(int m,int n)
{
fenzi=m;
fenmu=n;

}

void number::zhuanhuan(int g,int h)
{
float s;
s=(float)g/h;
cout<<s;
}

void number::yuefen(int x,int y) //分数化简
{ int t;
int i;

if(x>y)
t=y;
else
t=x;
for(i=t;i>=1 ;i--)
if((x%i==0)&&(y%i==0))
{
t=i;
break;
}
if(t)
{
fenzi=x/t;
fenmu=y/t;
}
else
{
fenzi=x;
fenmu=y;
}