What\'s wrong with my code? I need it to add and multiply hexadecimal numbers. H
ID: 3692916 • Letter: W
Question
What's wrong with my code? I need it to add and multiply hexadecimal numbers. Here's my code.
#include <iostream>
using namespace std;
#undef NULL
const int NULL = 0;
typedef int element;
const element SENTINEL = #;
class listnode {
public:
element data;
listnode * next;
};
class LList {
private:
listnode * head;
listnode * tail;
public:
void Read();
void Print();
void InsertTail();
void Clean();
void DeleteHead();
LList();
~LList();
int main() {
LList L;
char menu_option;
do {
L.Print();
display_menu();
menu_option = read_int();
switch (menu_option) {
case a:
L.Read();
break;
case b:
L.Addition();
break;
case c:
L.Multiply();
break;
case d:
L.display_menu();
break;
case e:
cout << “Quitting the program.” << endl;
break;
default:
cout << “Not a valid input.” << endl;
break;
}
}
void LList::Print() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is unchanged and its elements have
//been displayed to the user.
listnode * temp;
temp = head;
while(temp != NULL) {
cout << temp -> data << endl;
temp = temp -> next;
}
}
void LList::Read() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and is made up of elements
//provided by the user.
Clean();
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element();
while(userval != SENTINEL) {
InsertTail(userval);
userval = read_element();
int read_int() {
//variable dec+def
int user_input; //input - user input
//type checking
cin >> user_input;
while (!cin.good()){
cout << "Response must be a whole number, try again: ";
cin.clear();
cin.ignore(80, ' ');
cin >> user_input;
}
return user_input;
}
void display_menu() {
cout << "Options:" << endl
<< “e enter the current hexadecimal number from the keyboard.” << endl
<< “a add a new hexadecimal number to the current hexadecimal number” << endl
<< “m multiply a new hexadecimal number by the current hexadecimal number” << endl
<< “h help the user by displaying these choices” << endl
<< “q quit the program” << endl
<< “Enter a command: “;
}
void LList::InsertTail(element val) {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is unchanged, except it now has a
//new listnode at its tail end containing val.
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = NULL;
if (head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
}
void LList::Clean() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and empty and the memory
//for its listnodes have been given back to the system
//memory pool.
while(head != NULL)
DeleteHead();
}
element LList::DeleteHead() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is unchanged, except the listnode
//at its head end has been removed and its memory given back
//to the system memory pool. Its element has been returned.
listnode * temp;
element val;
temp = head;
head = head -> next;
val = temp -> data;
delete temp;
return temp -> data;
}
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 of the
//memory of its listnodes have been given back to the
//system memory pool.
Clean();
}
void LList::InsertHead(element val) {
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = head;
if(head == NULL)
tail = temp;
else;
head = temp;
}
void LList::Addition(){
LList Add; //Local Object for elements to be added to Native Object
LList Result; //Local Object for sum of Add elements and Native Object elements
int num1; //Decimal value of Native Object elements
int num2; //Decimal value of Add elements
int carry; //To be added to next set of values
int sum; //Result of addition
element val; //Hexadecimal value of sum
listnode * temp1;
listnode * temp2;
cout << "Enter hex to add: ";
Add.Reverse();
temp1 = head;
temp2 = Add.head;
carry = 0;
while (temp1 != NULL && temp2 != NULL) {
if(carry != 0)
Result.InsertTail(carry + 48);
else
;
num1 = AsciiConverter(temp1); //Returns decimal value of hexadecimal
num1 += carry;
num2 = AsciiConverter(temp2); //Returns decimal value of hexadecimal
sum = (num1 + num2) % 16; //Base-16 addition
carry = (num1 + num2) / 16;
//Convert back to ascii for hexadecimal character
if (sum <= 9)
val = sum + 48;
else
val = sum + 55;
//Putting the hexadecimal characters on local link list
Result.InsertTail(val);
//Move to next element on link lists
temp1 = temp1->next;
temp2 = temp2->next;
}
//Attach native object link list head and tail to local link list
Steal(Result);
}
int LList::AsciiConverter(listnode* node){
int num;
if (node != NULL)
num = node->data;
else
num = 0;
if (num >= 48 && num <= 57)
num -= 48;
else if (num >= 65 && num <= 70)
num -= 55;
else
;
return num;
}
void reverse(listnode **head)
{
if (!head)
return;
reverseUtil(*head, NULL, head);
}
void reverseUtil(listnode *curr, node *prev, listnode **head)
{
/* If last node mark it head*/
if (!curr->next)
{
*head = curr;
/* Update next to prev node */
curr->next = prev;
return;
}
/* Save curr->next node for recursive call */
listnode *next = curr->next;
/* and update next ..*/
curr->next = prev;
reverseUtil(next, curr, head);
}
Explanation / Answer
main.cpp: At global scope:
main.cpp: In member function 'int LList::main()':
main.cpp:52:22: error: 'display_menu' was not declared in this scope
display_menu();
^
main.cpp:53:32: error: 'read_int' was not declared in this scope
menu_option = read_int();
^
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.