*livre* 2020-02-15 15:45 采纳率: 0%
浏览 374

大数加减,VS上可以运行,oj上显示段错误,哪里出问题了?

#include
using namespace std;
struct list
{
char num;
list * next;
};
void creat(list * & head)
{
list *s,*p;
s=new list;
s->next=NULL;
cin.get(s->num);
while(s->num!='\n')
{
if((s->num>='0'&&s->num<='9')||s->num=='-')
{
if(head==NULL)
{head=s;
head->next=NULL;
p=s;}
else if(head->num=='-')
{if(p->num=='-')
{p->next=s;
p=s;}
else
{s->next=head->next;
head->next=s;}}
else if(head->num>='0'&&head->num<='9')
{s->next=head;
head=s;}
}
s=new list;
s->next=NULL;
cin.get(s->num);
}
delete s;
return;
}
void add(list *h1,list *h2,list * & h3)
{
list *h;
h=new list;
h->next=NULL;
int a,b,d,c=0;
while(h1||h2)
{
if(h1&&h2)
{
a=(int)h1->num-48;
b=(int)h2->num-48;
d=a+b+c;
c=0;
if(d/10==0)
{
h->num=d+48;
c=0;
}
else
{
c=d/10;
d%=10;
h->num=d+48;
}
if(h3==NULL)
{h3=h;
h3->next=NULL;}
else
{h->next=h3;
h3=h;}
h1=h1->next;
h2=h2->next;
h=new list;
}
else if(h1)
{
a=(int)h1->num-48;
d=a+c;
if(d/10==0)
{
h->num=d+48;
c=0;
}
else
{
c=d/10;
d%=10;
h->num=d+48;
}
h->next=h3;
h3=h;
h1=h1->next;
h=new list;
}
else if(h2)
{
a=(int)h2->num-48;
d=a+c;
if(d/10==0)
{
h->num=d+48;
c=0;
}
else
{
c=d/10;
d%=10;
h->num=d+48;
}
h->next=h3;
h3=h;
h2=h2->next;
h=new list;
}
}
if(c)
{
h->num=c+48;
h->next=h3;
h3=h;
h=new list;
}
}
int count(list *head)
{
int f;
list *g;
g=head;
for(f=0;g;f++)
{
g=g->next;
}
return f;
}
int compare(list *h1,list *h2)
{
int t,a,b,e,j;
e=count(h1);
j=count(h2);
if(e>j)
t=1;
else if(e t=0;
else
{
while(h1||h2)
{
a=(int)h1->num-48;
b=(int)h2->num-48;
if(a>=b)
t=1;
else
t=0;
h1=h1->next;
h2=h2->next;
}
}
return t;
}
void Minus(list *h1,list *h2,list * & h3)
{
int a,b,c=0,d,t;
list *h;
h=new list;
h->next=NULL;
t=compare(h1,h2);
if(t)
{
while(h1||h2)
{
if(h1&&h2)
{
a=(int)h1->num-48;
b=(int)h2->num-48;
if(a-c>=b)
{
d=a-c-b;
h->num=d+48;
c=0;
}
else
{
d=a-c+10-b;
h->num=d+48;
c=1;
}
if(h3==NULL)
{h3=h;
h3->next=NULL;}
else
{h->next=h3;
h3=h;}
h1=h1->next;
h2=h2->next;
h=new list;
}
else if(h1)
{
a=h1->num-48;
if(a-c>=0)
{
d=a-c;
h->num=d+48;
c=0;
}
else
{
d=a-c+10;
h->num=d+48;
c=1;
}
h->num=d+48;
h->next=h3;
h3=h;
h1=h1->next;
h=new list;
}
}
}
else
{
Minus(h2,h1,h3);
h->num='-';
h->next=h3;
h3=h;
}
}
void show(list *head)
{
int a,c,d=1,q=1;
list *h;
h=head;
if(h->num=='-')
{
h=h->next;
}
while(q)
{
if(h->num!='0')
q=0;
else
h=h->next;
}
a=count(h);
c=a%3;
while(h)
{
if(c)
{
cout<num;
h=h->next;
c--;
d=0;
}
else if(h&&d)
{
c=3;
d=0;
}
else if(h)
{
cout<<',';
c=3;
}
}
cout< }
int main()
{
int x,a,b,c,i;
char y;
cin>>x;
for(i=0;i {
a=1;
b=1;
c=0;
cin>>y;
cin.get();
list * h1=NULL;
list * h2=NULL;
list * h3=NULL;
creat(h1);
creat(h2);
if(y=='+')
{
if(h1->num=='-')
{
a=0;
h1=h1->next;
}
if(h2->num=='-')
{
b=0;
h2=h2->next;
}
if(a&&b)
add(h1,h2,h3);
else if(a)
Minus(h1,h2,h3);
else if(b)
Minus(h2,h1,h3);
else
{
list *s;
s=new list;
s->num='-';
s->next=NULL;
add(h1,h2,h3);
s->next=h3;
h3=s;
}
}
else if(y=='-')
{
if(h1->num=='-')
{
a=0;
h1=h1->next;
}
if(h2->num=='-')
{
b=0;
h2=h2->next;
}
if(a&&b)
Minus(h1,h2,h3);
else if(a)
add(h1,h2,h3);
else if(b)
{
list *s;
s=new list;
s->num='-';
s->next=NULL;
add(h1,h2,h3);
s->next=h3;
h3=s;
}
else
Minus(h2,h1,h3);
}
if(h3->num=='-')
{
c=1;
h3=h3->next;
}
if(c)
cout<<'-';
show(h3);
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 解决一个加好友限制问题 或者有好的方案
    • ¥15 关于#java#的问题,请各位专家解答!
    • ¥15 急matlab编程仿真二阶震荡系统
    • ¥20 TEC-9的数据通路实验
    • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
    • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
    • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
    • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
    • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
    • ¥30 求解达问题(有红包)