Hello
I have problem with adding two numbers that are less than 10 and their result is more than 10.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
typedef class node * node_pointer;
class node
{
public:
int num;
node *nxt;
};
string reverse (string str)
{
int len = str.length();
string tmp = "";
const char *data = str.data();
int i;
for( i=len; i>=0; i--)
{
tmp += data[i];
}
return tmp;
}
node_pointer readingTokens(string& str)
{
node_pointer head = NULL;
node_pointer curr = NULL;
const char* data = str.data();
for(int i = 1; i<str.length(); i++)
{
node_pointer tmp = new node ();
int tmpNum = data[i]-'0';
tmp -> num = tmpNum;
tmp -> nxt = NULL;
if(head== NULL)
{
head = tmp;
curr = head;
}
else
{
curr ->nxt = tmp;
curr = tmp;
}
}
return head;
}
node_pointer addition(node_pointer list1, node_pointer list2)
{
int carry = 0;
string str = "";
std::stringstream result;
while(list1 !=NULL && list2 !=NULL)
{
int sum = list1->num+list2->num+carry;
if(sum >9)
{
carry =1;
sum = sum-10;
}
else
{
carry = 0;
}
result<<sum;
list1 = list1->nxt;
list2 = list2->nxt;
}
if(list1 !=NULL)
{
int sum = list1->num+carry;
if(sum >9)
{
carry =1;
sum -=10;
}
else
{
carry = 0;
}
result<<sum;
list1 = list1->nxt;
}
while(list2 != NULL)
{
int sum = list2->num+carry;
if(sum >9)
{
carry =1;
sum -=10;
}
else
{
carry = 0;
}
result<<sum;
list2 = list2->nxt;
}
string tmpstr = result.str();
string tmp = reverse(tmpstr);
return readingTokens(tmp);
}
void borrow(node_pointer list)
{
if(list->num ==0)
{
list->num += 10;
borrow(list->nxt);
list->nxt->num -=1;
}
}
node_pointer sub(node_pointer list1, node_pointer list2)
{
int carry = 0;
string str = "";
std::stringstream result;
while(list1 !=NULL && list2 !=NULL)
{
if (list1->num >= list2->num)
{
result<<list1->num-list2->num;
}
else
{
list1->num+=10;
borrow(list1->nxt);
list1->nxt->num-=1;
result<<list1->num-list2->num;
}
list1=list1->nxt;
list2=list2->nxt;
}
while(list1 !=NULL)
{
result<<list1->num;
list1 = list1->nxt;
}
while(list2 != NULL)
{
result<<list2->num;
list2 = list2->nxt;
}
string tmpstr = result.str();
string tmp = reverse(tmpstr);
return readingTokens(tmp);
}
void printList( node_pointer list)
{
cout <<"Result = ";
node_pointer tmp = list;
int sum = 0;
while(tmp != NULL && sum == 0)
{
sum += tmp->num;
tmp = tmp->nxt;
}
if(sum !=0)
{
while(list != NULL)
{
cout<<list->num;
list = list->nxt;
}
}
else
{
cout << 0;
}
cout << endl;
}
void printList2( node_pointer list)
{
cout <<"Result =-";
node_pointer tmp = list;
int sum = 0;
while(tmp != NULL && sum == 0)
{
sum += tmp->num;
tmp = tmp->nxt;
}
if(sum !=0)
{
while(list != NULL)
{
cout<<list->num;
list = list->nxt;
}
}
else
{
cout << 0;
}
cout << endl;
}
int main(){
// get input from user0
char cont='y';
do
{
string str1="";
string str2="";
char op;
cout<<"Enter first number:"<<endl;
cin>> str1;
cout<<"Enter second number:"<<endl;
cin>>str2;
// cout<<
// reverse the 2 numbers
str1=reverse(str1);
str2=reverse(str2);
node_pointer list1 = readingTokens(str1);
node_pointer list2 = readingTokens(str2);
cout<<"What operation you want, + or -:"<<endl;
cin>>op;
if( op == '+'){
node_pointer result_list = addition(list1,list2);
printList(result_list);
}
else if (op == '-'){
int type= 1;
if(str1.length()< str2.length())
{
type = 2;
}
else if(str1.length() == str2.length())
{
const char * data1 = str1.data();
const char * data2 = str2.data();
for(int i = str1.length()-1; i>0; i--)
{
if(data1[i]> data2[i])
{
break;
}
if(data2[i]>data1[i])
{
type =2;
break;
}
}
}
node_pointer result_listsub;
if(type == 1)
{
result_listsub = sub(list1,list2);
printList(result_listsub);
}
else
{
result_listsub= sub(list2,list1);
printList2(result_listsub);
while(result_listsub->num==0)
{
result_listsub= result_listsub->nxt;
result_listsub->num *= -1;
}
}
}
else
{
cout<<"Invalid operation"<<endl;
}
cout << "Do you want to continue?:"<<endl;
cin >> cont;
}
while(cont == 'y' || cont == 'Y');
}