麻烦各位大哥帮我修改一下这个程序,急用啊

samsam10 2008-04-23 12:52:46
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; //实际用来存储数据的长度
};
#include<iostream.h>
#include<string.h>

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;
}

//用字符串初始化类
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++)
number[loop]=other.num[loop];
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);
//找出较短的数的位数
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;
inr k=0;//用来标志是否需要向前借一的临时量

//第一阶段:有短的数组的元素参与的减法
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(temp2<0)k=1;
else k=0;
}

//把数组最左边的零去掉
while((middle.num[0]=='0')&&(middle.len>1))
{
middle.len=middle.lem-1;
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 & stream,MEAG & obj)
{
for(int loop=0;loop<=obj.len-1;loop++)
ostream<<obj.num[loop];
return stream;
}
istream & operator>>(iatream & stream, MEAG & obj)
{cout<<"请输入一个数:";
istream>>obj.num;
obj.len=stream(obj.num);
return stream;
}
...全文
169 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
不会,帮顶
萧乐颜 2008-04-23
  • 打赏
  • 举报
回复
MEAG minus(const MEAG& other); //减法

MS楼主,没写这个的具体操作吧,虽然我加了<functional>,但是还是会报错"无法解析的外部指令",还是要写一下的.
kingsun555 2008-04-23
  • 打赏
  • 举报
回复
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-(); //重载负号

有些函数的返回类型就不对吧
HelloDan 2008-04-23
  • 打赏
  • 举报
回复

istream & operator>>(istream & is, MEAG & obj) // here
{
cout <<"请输入一个数:";
is>>obj.num;
obj.len=strlen(obj.num); //这样试一下吧。
return is;
}
HelloDan 2008-04-23
  • 打赏
  • 举报
回复


#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;
}

//胡乱改了一下, 我不看内容的,编译器中报错我改哪,但最后还是有一个没有改完,因为我不明白。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧