I'm writing a program that is supposed to add two linked lists together and display their sum in vigesimal digits. The problem is that when I have a carry it will not add to the next node, and also when I try to add in lowercase values as opposed to uppercase, my answer is different, when the uppercases and lowercases of the same letter should get the same answer. Not sure what exactly what I'm doing wrong, can someone point me in the right direction? I'm assuming my issue lies in my Add() function, but i can't figure it out.
#include <iostream>
#include <string>
#undef NULL
usingnamespace std;
typedefchar element;
constint NULL=0;
constint BASE=20;
const element SENTINEL='#';
class listnode{
public:
element data;
listnode*next;
};
class LList{
private:
listnode*head;
listnode*tail;
public:
LList();
~LList();
void Clean();
void ReadBackward();
void Print();
void InsertHead(element thing);
void InsertTail(element thing);
void Duplicate(LList&Source);
void Add();
void Multiply();
void DisplayMenu();
void GetMenuChoice();
void Steal(LList&Victim);
void GetCommand();
void Reverse();
int CharToInt(char userval);
char IntToChar(int userval);
};
int main(){
LList A; //L1
A.GetCommand();
}
void LList::Print(){
//PRE: The N.O. LList is valid
//POST: The N.O. LList is unchanged, and its elements have been
//displayed
Reverse();
int counter;
listnode*temp;
temp=head;
while(temp!=NULL){
cout<< temp->data;
temp=temp->next;
}
cout << endl <<endl;
Reverse();
}
void LList::ReadBackward(){
//PRE: The N.O. LList is valid
//POST: The N.O. LList will be valid using data provded by the user
//and will be stored in a forward order
element userval;
listnode*temp;
Clean();
cout<<endl;
cout <<"Enter a vigesimal number, followed by " <<SENTINEL <<": ";
cin>> userval;
while(userval!=SENTINEL){
temp=new listnode;
temp->data=userval;
temp->next=head;
if(head==NULL)
tail=temp;
else
;
head=temp;
cin>>userval;
}
cout <<endl <<"Entering completed" <<endl <<endl;
cout << "Current vigesimal number is: ";
}
int LList::CharToInt(char userval){
//Converts individual vigesimal numbers into integers to perform calc.
char ch;
int num;
ch=userval;
if((ch>=48)||(ch<=57))
num=ch-'0';
elseif((ch>=65)||(ch<=74))
num=ch-'A'+10;
elseif ((ch>=97)||(ch<= 106))
num=ch-'a'+10; //converts lower to upper, then to int
return num;
}
char LList::IntToChar(int userval){
//Converts integer number to vigesimal number to display to the user
int num;
char ch;
num=userval;
if((num>=0)||(num<=9))
ch=num+'0';
elseif ((num>9)||(num<=19)) //num is A-J
ch=num+'A'-10;
return ch;
}
void LList::Reverse(){
//PRE: NO LList is valid
//POST: NO LList is unchanged, except its elements are now in reverse
//order
LList Helper; //calls constructor, makes head NULL
listnode*temp;
temp=head;
while(temp!=NULL){
Helper.InsertHead(temp->data);
temp=temp->next;
}
Steal(Helper);
}
void LList::Clean(){
//PRE: The N.O. LList is valid
//POST: The N.O. LList is valid and empty, and all of the memory
//for its listnodes has been returned to the system memry pool
listnode*temp;
while(head!=NULL){
temp=head;
head=head->next;
delete temp;
}
}
LList::LList(){
//PRE: None
//POST: The N.O. LList is valid and empty
head=NULL;
}
LList::~LList(){
//PRE: The N.O. LList is valid
//POST: The N.O. LList is valid and empty, and all memory for its
//listnodes has been returned to the heap
Clean();
}
void LList::InsertHead(element thing){
//PRE: The N.O. LList is valid
//POST: The N.O. LList is unchanged, except it now contains a new
//listnode at its head end containing thing
listnode*temp;
temp=new listnode;
temp->data=thing;
temp->next=head;
if(head==NULL)
tail=temp;
else
;
head=temp;
}
void LList::InsertTail(element thing){
//PRE: The N.O. LList is valid
//POST: The N.O. LList is unchanged, except it now contains a new
//listnode at its tail end containing thing
listnode*temp;
temp=new listnode;
temp->data=thing;
temp->next=NULL;
if(head==NULL)
head=temp;
else
tail->next=temp;
tail=temp;
}
void LList::Steal(LList & Victim){
Clean();
head=Victim.head;
tail=Victim.tail;
Victim.head=NULL;
}
void LList::Add(){
//This function adds two linked lists together, storing their sum in a
//third linked list
LList Result; //sum of both llists
LList B; //2nd llist
B.ReadBackward(); //Gets elements to add from user
B.Print();
cout<< "Adding a new vigesimal number to the current vig. number"
<<endl <<endl;
int sum;
int sum1;
int sum2;
int carry;
sum=0;
sum1=0;
sum2=0;
carry=0;
listnode*temp1;
listnode*temp2;
temp1=head;
temp2=B.head;
while((temp1!=NULL)&&(temp2!=NULL)){
sum1=CharToInt(temp1->data);
sum2=CharToInt(temp2->data);
temp1=temp1->next;
temp2=temp2->next;
sum=(sum1+sum2+carry)%BASE;
carry=(sum1+sum2+carry)/BASE;
Result.InsertTail(IntToChar(sum));
}
while((temp1==NULL)&&(temp2!=NULL)){
sum2=CharToInt(temp2->data);
temp2=temp2->next;
sum=(sum2+carry)%BASE;
carry=(sum2+carry)/BASE;
Result.InsertTail(IntToChar(sum2));
}
while((temp2==NULL)&&(temp1!=NULL)){
sum1=CharToInt(temp1->data);
temp1=temp1->next;
sum=(sum1+carry)%BASE;
carry=(sum1+carry)/BASE;
Result.InsertTail(IntToChar(sum1));
}
Steal(Result);
Print();
GetCommand();
}
I deleted some irrelevant functions so the code would fit.
Lines 108,110,112: You're using an OR condition when you need an AND condition. e.g. Any value >= 48 will cause the first if to be true and will cause the second condition to be ignored.
Line 115: What happens is the character entered is not a vigesimal digit? You're going to return an uninitialized (garbage) value.
Lines 125,127: Same issue with OR condition. You need an AND condition here also.
Line 130: Same issue returning uninitialized value if userval is out of range.
Thanks that helped a lot! Here is my updated add function, now it works sometimes but if there is a carry value it will flip it the wrong way, but if there is not a carry its fine. For example if I add 34A+1 i get 34B, but if I add I+J I get H1 instead of 1H. Not sure where the error lies.
void LList::Add(){
//This function adds two linked lists together, storing their sum in a
//third linked list
cout <<endl
<< "Adding a new vigesimal number to the current vig. number."
<<endl;
LList Result; //sum of both llists
LList B; //2nd llist
B.ReadBackward(); //Gets elements to add from the user
B.Print(); //Print those elements to the user
int sum; //sum of both numbers
int sum1; //sum of first number
int sum2; //sum of second number
int carry;
sum=0;
sum1=0;
sum2=0;
carry=0;
listnode*temp1;
listnode*temp2;
temp1=head;
temp2=B.head;
while((temp1!=NULL)||(temp2!=NULL)){
//while at least one of the lists is not empty
if(temp1!=NULL){ //Native object not empty
sum1=CharToInt(temp1->data); //gets integer value
temp1=temp1->next; //pointer pointing to next
//listnode in list
}
else // no more values to add
sum1=0;
if(temp2!=NULL){ //List B is not empty
sum2=CharToInt(temp2->data); //gets integer value
temp2=temp2->next; //pointer pointing to next listnode
}
else //no more values to add
sum2=0;
sum=sum1+sum2+carry;//sum is sum of NO + B + carry value
carry=0;//but carry is currently 0
while(sum>19){
sum=sum-BASE;
carry++;
Result.InsertTail(IntToChar(carry));
}
Result.InsertTail(IntToChar(sum));
sum=0;
}
cout<< "Adding completed." <<endl <<endl;
cout << "Current vigesimal number is: ";
Steal(Result);
Print();
GetCommand();
}