Subtraction in a Linked List Calculator

My subtraction doesn't work. it gives me an illogical number
that I can't identify how did it even appear.
could someone see what is the mistake and tell me who to resolve it

appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
using namespace std;

class Node
{
public:
	int value;
	Node * next;
	Node();
	Node(int);
};
Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}
void Print(Node * h)
{
	if(!h) return;
	Print(h->next);
	cout<<h->value;
}
void AddHead(Node* & head, int n)
{
	Node * tmp = new Node(n);
	tmp->next = head;
	head=tmp;
}
void Addition( Node* h1,Node* h2)
{  
	int carry = 0, sum;
Node* result = NULL;
Node* tmp = NULL;
Node* prev = NULL;
while(h1 !=NULL || h2 != NULL)
{ 
	
	 sum = carry +((h1? h1->value: 0) +(h2? h2->value: 0));
	 carry = (sum >=10)? 1:0;
	 sum = sum % 10;
	 tmp = new Node(sum);
	 if(result== NULL){
		 result = tmp;}
	 else{
		 prev->next = tmp;}
	 prev = tmp;
	 if(h1) h1=h1->next;
	 if(h2) h2=h2->next;
}
if(carry > 0)
	tmp->next = new Node(carry);
Print(result);
}
void Subtraction( Node* h1,Node* h2)
{
	int carry = 0, sum;
Node* result = NULL;
Node* tmp = NULL;
Node* prev = NULL;
while(h1 !=NULL || h2 != NULL)
{ 
	 sum = carry + ((h2? h2->value: 0) - (h1? h1->value: 0));
	 carry = (sum < 0)? -1:0;
	 if(carry < 0) sum += 10;
	 /*sum = sum % 10;*/
	 tmp = new Node(sum);
	 if(result== NULL){
		 result = tmp;}
	 else{
		 prev->next = tmp;}
	 prev = tmp;
	 if(h1) h1=h1->next;
	 if(h2) h2=h2->next;
}
//if(carry < 0)
	/*tmp->next = new Node(carry);*/
Print(result);
}
int Length(Node* head)
{
	int i=0;
	while(head)
	{
		i++;
		head=head->next;
	}
	return i;
}
int main()
{ 
	string num1,num2;
	char operation, ans;
	int v;

	do
	{
	Node* h1 = NULL;
	Node* h2 = NULL;
	
	cout<<"Enter first number: ";
	cin >> num1;

	for(unsigned int i=0; i<num1.length(); i++)
        {
			v= num1[i] - '0';
                AddHead(h1,v);
        }
	Print(h1);
	cout<<endl;
	cout<<"Enter second number: ";
	cin>> num2;
	for(unsigned int i=0; i<num2.length(); i++)
        {
                v= num2[i] - '0';
                AddHead(h2,v);
        }
	Print(h2);
	cout<<endl;

	cout<<"Do you want to add or substract the numbers enter + or -: ";
	cin>>operation;
	if(operation == '+')
	{
	cout<<"Result: ";
	 Addition(h1,h2);
	 cout<<endl;
	}
	else if(operation == '-')
	{
	cout<<"Result: ";
	if(h2 > h1)
	{cout<<"-"; Subtraction(h2, h1);}//this is where i switch the numbers
	else if(h1 > h2)
	{Subtraction(h1,h2);}
	cout<<endl;
	}
	cout<<"Again :";
	cin>> ans;
	}
	while((ans == 'Y') || (ans == 'y'));
	return 0;
}
Last edited on
anyone would like to see this one?

It is not as confusing as it looks.

Just the Subtraction function needs to have a look at.

thanks.
Try something easy like: 12 - 3 then 3 - 12.

Maybe
1
2
3
4
5
// Don't use the carry variable
if ( sum < 0 && h2 != NULL && h2->next != NULL ) {
     sum += 10;
     h2->next->value--;
}

Also you are comparing h1 to h2 to see if the value is going to be negative but they are pointers, not the value of the numbers. Those if checks do nothing to compute the answer.
Topic archived. No new replies allowed.