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

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 优质创作者: Java、后端开发技术领域 2024-07-25 21:17
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    gt;next=NULL;
    int e = count(h1);
    int j = count(h2);
    if(compare(h1, h2))
    {
    list *tmp = h1;
    h1 = h2;
    h2 = tmp;
    }
    while(h1 || h2)
    {
    a = (h1) ? h1->num - '0' : 0;
    b = (h2) ? h2->num - '0' : 0;
    if(a < b)
    {
    a += 10;
    list *tmp = h1;
    while(tmp->next && tmp->next->num == '-')
    {
    tmp->num = 9 + '0';
    tmp = tmp->next;
    }
    if(tmp->next != NULL)
    {
    tmp->num--;
    if(tmp == h1)
    {
    while(h1->num == '0')
    {
    tmp = h1;
    h1 = h1->next;
    delete tmp;
    }
    }
    }
    }
    d = a - b;
    h->num = d + '0';
    h->next = h3;
    h3 = h;
    h1 = (h1) ? h1->next : NULL;
    h2 = (h2) ? h2->next : NULL;
    h = new list;
    h->next = NULL;
    }
    }
    这段代码实现了两个大整数的减法运算。首先,通过比较两个大整数的位数,确定被减数和减数的大小关系,确保被减数大于等于减数。然后逐位相减,如果被减数小于减数,则向高位借位。最后将结果存储在h3中。 示例: 输入:h1为123,h2为45 输出:h3为78
    评论

报告相同问题?