Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
my code has many bugs, I don't know how to do,please help,and it's very urgent

C++
#include<iostream>
using namespace std;
struct node
{int data;node *next,*previous;};
void creat(node *&head)
{node *s,*p;
 char num[100];
 cin>>num;
 int i=0;
 s=new node;
 s->data=num[i]-48;
 while(num[i]!='\0')
 { if(head==NULL) head=s;
   else {p->next=s;s->previous=p;}
 i++; 
 p=s;
 s=new node;
 s->data=num[i]-48;
 }
 p->next=NULL;
 head->previous=NULL;
 delete s;
 return;
}
void plus(node *head1,node *head2)
{int number[100];
 int q=1,p=1,m=0,i;
 while(head1->next!=NULL) {head1=head1->next;p++;}
 while(head2->next!=NULL) {head2=head2->next;q++;}
if(p==q)
{for(i=0;p>0;i++,p--,q--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;
}

if(m==1) number[i]=1,i++;
}
else if(p>q)
{for(i=0;q>0;i++,q--,p--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;}
	while(p>0) 
{number[i]=head1->data+m;
 if(number[i]>9) m=1,number[i]-=10;
 else m=0;
 i++;
 p--;
 head1=head1->previous;}
	if(m==1) number[i]=1;i++;
}
       else
{for(i=0;p>0;i++,p--,q--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;}
while(q>0)
{number[i]=head2->data+m;
if(number[i]>9) m=1,number[i]-=10;
else m=0;
i++;
q--;
head2=head2->previous;}
if(m==1) number[i]=1,i++;
}
for(i=i-1;i>=0;i--)
{cout<<number[i];}
cout<<endl;
}
void minus(node *head1,node *head2)
{int number[100];
 int q=1,p=1,m=0,i,a,b,z;
 node *head;
 while(head1->next!=NULL) {head1=head1->next;p++;}
 while(head2->next!=NULL) {head2=head2->next;q++;}
if(p>q)
{for(i=0;q>0;q--,p--,i++)
{if((head1->data-m)>=head2->data) number[i]=head1->data-head2->data-m;
else number[i]=head1->data+10-head2->data-m,m=1;
head1=head1->previous;
head2=head2->previous;
}
for( ;p>0;p--,i++)
	{if(number[i]=head1->data-m<0) number[i]=head1->data-m,m=0;
else number[i]=head1->data+10-m;m=1;
	 head1=head1->previous;}
i=i-1;
for( ;i>=0;i--) 
{cout<<number[i];}
cout<<endl;
}
else if(p<q)>
{for(i=0;p>0;q--,p--,i++)
{if((head2->data-m)>=head1->data) number[i]=head2->data-head1->data-m;
else {number[i]=head2->data+10-head1->data-m;m=1;}
head1=head1->previous;
head2=head2->previous;}
for( ;q>0;q--,i++)
{if(number[i]=head2->data-m<0) number[i]=head2->data-m,m=0;
else number[i]=head2->data+10-m;m=1;
	 head2=head2->previous;}
i=i-1;
cout<<'-';
for( ;i>=0;i--) 
{cout<<number[i];}
cout<<endl;
}
else
{while(head1->previous!=NULL){head1=head1->previous;}
 while(head1->previous!=NULL) {head2=head2->previous;}
for( ;head1->next!=NULL;head1=head1->next,head2=head2->next)
{a=head1->data;
 b=head2->data;
 if(b>a) {head=head1;head1=head2;head2=head;z=0;break;}
 if(a>b) {z=1;break;}
 z=1;
}
while(head1->next!=NULL) {head1=head1->next;}
while(head2->next!=NULL) {head2=head2->next;}
for(i=0;q>0;q--,i++)
{if((head1->data-m)>=head2->data) number[i]=head1->data-head2->data-m;
else {number[i]=head1->data-head2->data-m+10;m=1;}
head1=head1->previous;
head2=head2->previous;
}
i=i-1;
while(number[i]==0) {i--;}
if(i==-1) cout<<'0';
else 
{if(z==0) cout<<'-';
for( ;i>=0;i--) 
{cout<<number[i];}}
cout<<endl;
}

}

int main()
{node *head1=NULL,*head2=NULL;
creat(head1);
creat(head2);
minus(head1,head2);
}
Posted
Updated 8-Jun-12 4:59am
v3
Comments
Sergey Alexandrovich Kryukov 8-Jun-12 11:02am    
This is not how it works out. This is not a question. Debugging your code is some work, not giving expert advice. To have some advice, you need to ask your question and provide sufficient detail.
--SA
Albert Holguin 8-Jun-12 11:26am    
Good luck with that! ...urgent coding is the best type...lol

1. You need to explain where the bugs are and what problems they cause.
2. It's not urgent to anyone except you.
 
Share this answer
 
From what I can see of your code you're trying to write something that does infinite precision arithmetic with each digit being held as a node in a linked list.

Here's the big problem with your code - it's way too big and hairy. It needs it's legs chopping off and being given a shave. Throw out what you've got, go back to the algorithm(s) you're trying to implement, use the standard library (cin and cout don't count) and implement it(them) in the most direct way.

Let's take addition as an example... Starting with the lowest place value add each digit and any carry from the previous addition until both of the numbers run out of digits. That's a fairly simple statement of what you have to do but your code is at least 40 lines long. A quick pseudo code implementation would be something like:

- sign extend the smaller number so it's got the same number of digits in it as the longer
- set carry to zero
- for each place value
- add the digits at these place values and carry and assign then to the result and carry

Er, that's it - one loop and maybe a conditional or two.

So go and have a rethink and start again. Urgent or no it'll pay you back.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900