65,210
社区成员
发帖
与我相关
我的任务
分享
istream & operator>>(istream & is, MEAG & obj) // here
{
cout <<"请输入一个数:";
is>>obj.num;
obj.len=strlen(obj.num); //这样试一下吧。
return is;
}
#include <iostream>
#include <string>
using namespace std;
class MEAG
{public:
MEAG(char number[]); //用字符串初始化类
MEAG(); //缺省初始化
MEAG::MEAG(const MEAG& other); //用对象初始化类
MEAG add(const MEAG& other); //加法
MEAG minusl(const MEAG& other); // 减法的辅助函数
MEAG minus(const MEAG& other); //减法
MEAG operator+(const MEAG& other); //重载加号
MEAG operator-(const MEAG& other); //重载减法
MEAG operator=(const MEAG& other); //重载等号
MEAG operator-(); //重载负号
friend ostream & operator <<(ostream & stream,MEAG & obj); // 重载输出符
friend istream & operator>>(istream & stream,MEAG & obj); //重载输入符
protected:
char num[200];
int len; //实际用来存储数据的长度
};
//用字符串初始化类
MEAG::MEAG(char number[])
{
len=strlen(number);
for(int loop=0;loop <=len-1;loop++)
num[loop]=number[loop];
}
//用对象初始化类
MEAG::MEAG(const MEAG& other)
{
len=other.len;
for(int loop=0;loop <=other.len-1;loop++)
num[loop]=other.num[loop]; //number->num
return;
}
//缺省初始化
MEAG::MEAG()
{
len=0;
return;
}
//加法算法是:假若A=1512345,B=2468,则第一步是先让5+8,4+6,3+4,2+2
//其中设一个K作为进1的标志位,然后第二步为让剩下的151与K相加
MEAG MEAG::add(const MEAG& other)
{//声明一个临时对象作为返回值
MEAG temp;
temp.len=((len>=other.len)?len:other.len)+1;
//
int min=((len <=other.len)?len:other.len); // add one more (
//找出较短的数的位数
int k=0;
int a=len;
int b=other.len;
int c=temp.len;
//有短的数组的元素参与的加法
for(int loop=0;loop <=min-1;loop++)
{
int temp1=(int)num[--a]%48+(int)other.num[--b]%48+k;
temp.num[--c]=char(temp1%10+48);
k=temp1/10;
}
//长的数组剩下的元素参与的加法
if(len==other.len)
{
temp.num[--c]=char(k+48);
}
if(len>other.len)
{
for(;a>=1;)
{
int temp2=(int)num[--a]%48+k;
temp.num[--c]=(char)temp2%10+48;
k=temp2/10;
}
temp.num[--c]=char(k+48);
}
if(len <other.len)
{
for(;b>=1;)
{
int temp1=(int)other.num[--b]%48+k;
temp.num[--c]=char(temp1%10+48);
k=temp1/10;
}
temp.num[--c]=char(k+48);
}
//把数组最左边的零去掉
while((temp.num[0]=='0')&&(temp.len>1))
{
temp.len=temp.len-1;
for(int loop=1;loop <=temp.len;loop++)
{temp.num[loop-1]=temp.num[loop];}
}
return temp;
}
//减法的辅助函数,相当于被减数大于减数的减法
MEAG MEAG::minusl(const MEAG& other)
{
MEAG temp,middle;
temp.len=len;
int a=len;
int b=other.len;
int c=temp.len;
int k=0;//用来标志是否需要向前借一的临时量 inr->int
//第一阶段:有短的数组的元素参与的减法
for(int loop=0;loop <=other.len-1;loop++)
{
int temp1=num[--a]-other.num[--b]-k;
temp.num[--c]=(temp1+10)%10+48;
if(temp1 <0)k=1;
else k=0;
}
//第二阶段:长的数组剩下的元素参与的减法
for(;c>=1;)
{
int temp2=num[--a]%48-k;
temp.num[--c]=(temp2+10)%10+48;
if(temp2 <0)k=1;
else k=0;
}
//把数组最左边的零去掉
while((temp.num[0]=='0')&&(temp.len>1))
{
temp.len=temp.len-1;
int temp1=other.num[--a]-num[--b]-k;
middle.num[--c]=(temp1+10)%10+48;
if(temp1<0)
k=1; //temp2->temp1
else
k=0;
}
//把数组最左边的零去掉
while((middle.num[0]=='0')&&(middle.len>1))
{
middle.len=middle.len-1; // lem->len
for(int loop=1;loop <=middle.len;loop++)
{middle.num[loop-1]=middle.num[loop];}
}
middle=-middle;
if(len <other.len)
{
int a=other.len;
int b=len;
int c=other.len;
int k=0;
middle.len=other.len;
for(int loop=0;loop <=len-1;loop++)
{
int temp1=other.num[--a]-num[--b]-k;
middle.num[--c]=(temp1+10)%10+48;
if(temp1 <0)k=1;
else k=0;
}
for(;c>=1;)
{
int temp2=other.num[--a]%48-k;
middle.num[--c]=(temp2+10)%10+48;
if(temp2 <0)k=1;
else k=0;
}
while((middle.num[0]=='0')&&(middle.len>1))
{
middle.len=middle.len-1;
for(int loop=1;loop <=middle.len;loop++)
{middle.num[loop-1]=middle.num[loop];}
}
middle=-middle;
}
return middle;
}
MEAG MEAG::operator+(const MEAG & other)
{
MEAG temp;
temp=add(other);
return temp;
}
MEAG MEAG::operator-(const MEAG& other)
{
MEAG temp;
temp=minus(other);
return temp;
}
MEAG MEAG::operator=(const MEAG & other)
{
len=other.len;
for(int loop=0;loop <=len-1;loop++)
num[loop]=other.num[loop];
return *this;
}
MEAG MEAG::operator-()
{
MEAG temp;
temp.len=len;
if(num[0]=='-')
{for(int loop=1;loop <=len-1;loop++)
{temp.num[loop-1]=num[loop];
temp.len=temp.len-1;}
}
else
{
for(int loop=len;loop>=1;loop--)
{temp.num[loop]=num[loop-1];
temp.num[0]='-';
temp.len=temp.len+1;
}
}
return temp;
}
ostream & operator <<(ostream & os,MEAG & obj) //here
{
for(int loop=0;loop <=obj.len-1;loop++)
os<<obj.num[loop];
return os;
}
istream & operator>>(istream & is, MEAG & obj) // here
{
cout <<"请输入一个数:";
is>>obj.num;
obj.len=is(obj.num); //这里实在不知道是干什么的。
return is;
}
int main()
{
MEAG obj1;
cin>>obj1;
cout <<"你所输入的第一个对象是" <<obj1 <<'\n';
cout <<"\n";
MEAG obj2;
cin>>obj2;
cout <<"你所输入的第二个对象是" <<obj2 <<'\n';
cout <<'\n';
MEAG obj3=obj1+obj2;
MEAG obj4=obj1-obj2;
cout <<'\n';
cout <<"以下显示加法与减法:" <<'\n';
cout <<" " <<obj1 <<"+" <<obj2 <<'\n' <<"=";
cout <<obj3 <<'\n';
cout <<'\n';
cout <<" " <<obj1 <<"-" <<obj2 <<'\n' <<"=";
cout <<obj4 <<'\n';
return 0;
}
//胡乱改了一下, 我不看内容的,编译器中报错我改哪,但最后还是有一个没有改完,因为我不明白。