const int maxsize=100;
template <class DateType>
class Seqlist
{
public:
Seqlist(DateType a[],int b);
Seqlist<int> add(Seqlist<int> a,Seqlist<int> b);
private:
DateType date[maxsize];
int length;
};
template <class DateType>
Seqlist<DateType>::Seqlist(DateType a[],int b)
{
if(b>maxsize)throw "参数非法";
else
length=b;
for(int i=0;i<length;i++)
date[i]=a[i];
}
template <class DateType>
Seqlist<int> Seqlist<int>::add(Seqlist<int> a,Seqlist<int> b)
{
int flag=0;int d[3]={0,0,0};int x=3;
Seqlist<int> c(d,x);
int i=0;
while(i<a.length&&i<b.length)
{
c.date[i]=(a.date[i]+b.date[i]+flag)%10;
flag=(a.date[i]+b.date[i]+flag)/10;
i++;
}
while(i<a.length)
{
c.date[i]=(a.date[i]+flag)%10;
flag=(a.date[i]+flag)/10;
i++;
}
while(i<b.length)
{
c.date[i]=(b.date[i]+flag)%10;
flag=(b.date[i]+flag)/10;
i++;
}
if(flag==1)c.date[i]=1;
if(a.length<b.length)c.length=b.length+flag;
else c.length=a.length+flag;
return c;
}
int main()
{
int a[3]={1,2,3};
int b[3]={1,2,3};
int e=3;
Seqlist<int> a1(a,e),a2(b,e);
a1.add(a1,a2);
return 0;
}
程序运行到一半崩溃了,求大神改一下!
你自己先理清代码逻辑,自己分析出来才有成就感
你这是编译错误。从哪里copy过来的吧
#include<iostream>
const int maxsize=100;
template <class DateType>
class Seqlist
{
public:
Seqlist(DateType a[],int b);
Seqlist<int> add(Seqlist<int> a,Seqlist<int> b);
private:
DateType date[maxsize];
int length;
};
template <class DateType>
Seqlist<DateType>::Seqlist(DateType a[],int b)
{
if(b>maxsize)throw "参数非法";
else
length=b;
for(int i=0;i<length;i++)
date[i]=a[i];
}
template <class DateType>
Seqlist<int> Seqlist<DateType>::add(Seqlist<int> a,Seqlist<int> b)
{
int flag=0;int d[3]={0,0,0};int x=3;
Seqlist<int> c(d,x);
int i=0;
while(i<a.length&&i<b.length)
{
c.date[i]=(a.date[i]+b.date[i]+flag)%10;
flag=(a.date[i]+b.date[i]+flag)/10;
i++;
}
while(i<a.length)
{
c.date[i]=(a.date[i]+flag)%10;
flag=(a.date[i]+flag)/10;
i++;
}
while(i<b.length)
{
c.date[i]=(b.date[i]+flag)%10;
flag=(b.date[i]+flag)/10;
i++;
}
if(flag==1)c.date[i]=1;
if(a.length<b.length)c.length=b.length+flag;
else c.length=a.length+flag;
return c;
}
int main()
{
int a[3]={1,2,3};
int b[3]={1,2,3};
int e=3;
Seqlist<int> a1(a,e),a2(b,e);
Seqlist<int> c=a1.add(a1,a2);
return 0;
}
报错的话加个断点,一步步跑一下就知道了,也没多少代码