搜索
查看: 1431|回复: 4
打印 上一主题 下一主题

问题

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-11 15:21:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
本帖最后由 981013 于 2013-8-11 15:23 编辑

最近做了个存放大整数的class:lllint
但重载>>时发现了问题
[code=Cpp width=740px]friend istream &operator >>(istream &is,lllint& a)
{
char* b;
is>>b;
a=b;
return is;
}[/code]
在执行以上这段时出错了(内存读写错误)
接下来我把这段移到了main()里;
[code=Cpp width=740px]char* b;
cin>>b;
cout<<b<<endl;
lllint a=b;
cout<<a<<endl;[/code]
接下来的情况让我晕了:
1.直接运行:输出字符串b前就出错了
2.把cin>>改成直接用字符串常量为b赋值:正常
于是我想可能是cin有问题
但是:
3.把最后两行注释掉:正常,输出的字符串同2
然后我就晕了……{:soso_e134:}
其他代码应该都没问题,保险起见还是都发一下
[code=Cpp width=740px]#ifndef LLLINT_H
#define LLLINT_H
#include <mylib.h>
#include <fstream>
using mine::intlength;
class lllint
{
public:
lllint()
{
m_nData=NULL;
m_nSize=0;
m_bMinus=false;
}
lllint(int a)
{
if(a<0)m_bMinus=true;
else m_bMinus=false;
m_nSize=intlength(a);
m_nData=new int[intlength(a)];
for(int b=0; b<intlength(a); b++)
m_nData=0;
m_nData[0]=abs(a);
order();

}
lllint(char* a)
{
if(a[0]=='-')
{
m_bMinus=true;
a++;
}
else m_bMinus=false;
m_nSize=0;
for(int b=0; a!='\0'; b++)
{
if(a>'9'||a<'0')
{
cout<<"所给字符串不是纯数字字符串!构造失败!"<<endl;
m_nData=NULL;
m_nSize=0;
return;
}
m_nSize++;
}
m_nData=new int[m_nSize];
for(int b=0;b<m_nSize;b++)
m_nData=a[m_nSize-1-b]-'0';
}
void print(bool bl=true)
{
if(m_bMinus)cout<<'-';
for(int a=m_nSize-1; a>=0; a--)
cout<<m_nData[a];
if(bl)
cout<<endl;
else cout<<ends;
}
~lllint()
{
delete [] m_nData;
}
lllint(const lllint& other)
{
m_nSize=other.m_nSize;
m_bMinus=other.m_bMinus;
m_nData=new int[m_nSize];
for(int a=0; a<m_nSize; a++)
m_nData[a]=other.m_nData[a];
}
lllint& operator=(const lllint& other)
{
m_nSize=other.m_nSize;
m_bMinus=other.m_bMinus;
delete [] m_nData;
m_nData=new int[m_nSize];
for(int a=0; a<m_nSize; a++)
m_nData[a]=other.m_nData[a];
return *this;
}
lllint& operator=(int a)
{
if(a<0)m_bMinus=true;
else m_bMinus=false;
m_nSize=intlength(a);
delete [] m_nData;
m_nData=new int[intlength(a)];
for(int b=0; b<intlength(a); b++)
m_nData=0;
m_nData[0]=abs(a);
order();
return *this;
}
lllint& operator=(char* a)
{
if(a[0]=='-')
{
m_bMinus=true;
a++;
}
else m_bMinus=false;
m_nSize=0;
for(int b=0; a!='\0'; b++)
{
if(a>'9'||a<'0')
{
cout<<"所给字符串不是纯数字字符串!构造失败!"<<endl;
m_nData=NULL;
m_nSize=0;
return *this;
}
m_nSize++;
}
delete [] m_nData;
m_nData=new int[m_nSize];
for(int b=0;b<m_nSize;b++)
m_nData=a[m_nSize-1-b]-'0';
return *this;
}
friend ostream &operator <<(ostream &os,lllint& a)
{
if(a.m_bMinus)os<<'-';
for(int b=a.m_nSize-1; b>=0; b--)
os<<a.m_nData;
return os;
}
friend fstream &operator <<(fstream &fs,lllint& a)
{
if(a.m_bMinus)fs<<'-';
for(int b=a.m_nSize-1; b>=0; b--)
fs<<a.m_nData;
return fs;
}
friend istream &operator >>(istream &is,lllint& a)
{
char* b;
is>>b;
cout<<b<<endl;
a=b;
return is;
}
private:
int* m_nData;
int m_nSize;
bool m_bMinus;
void order()
{
for(int a=0; a<m_nSize; a++)
{
int ls=m_nData[a];
m_nData[a]=m_nData[a]%10;
m_nData[a+1]=(ls-m_nData[a])/10;

}
}
};
#endif // LLLINT_H
[/code]
其中的intlength:
[code=Cpp width=740px]int intlength(int a)
{
a=abs(a);
for(int b=0;b<=10;b++)
{
if(a/(int)pow(10,(double)b)<1)
return b;
}
}[/code]

最佳答案

查看完整内容

std::string buf; is >> buf; 然后拷贝过去 另外我扫了一眼class的设计 首先数据没必要都放在堆里,这个lllint类不是容器 然后对于较小的整数,每个数字放在一个int数组里非常浪费空间 还有opreator=, 没有检查*this和other是不是同一个东西
沙发
发表于 2013-8-11 15:21:53 | 只看该作者
981013 发表于 2013-8-11 15:42
怎么解决?
弄个缓冲区?

std::string buf;
is >> buf;

然后拷贝过去

另外我扫了一眼class的设计

首先数据没必要都放在堆里,这个lllint类不是容器
然后对于较小的整数,每个数字放在一个int数组里非常浪费空间
还有opreator=, 没有检查*this和other是不是同一个东西
板凳
发表于 2013-8-11 15:39:57 | 只看该作者
char* b;
is>>b;

b指向随机一个空间,随便写进去于是内存错误了
地板
 楼主| 发表于 2013-8-11 15:42:58 | 只看该作者
rosynirvana 发表于 2013-8-11 15:39
char* b;
is>>b;

怎么解决?
弄个缓冲区?
5#
发表于 2013-8-11 21:01:18 | 只看该作者
好难啊啊啊!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表