Question: I need to Design, write, and execute a C++ program to add and multiply hexadecimal numbers, consisting of an arbitrary number of hexadecimal digits. The
I need to Design, write, and execute a C++ program to add and multiply hexadecimal numbers, consisting of an arbitrary number of hexadecimal digits.
The user should be able to work with an ongoing, "current" hexadecimal number by repeatedly choosing from the following actions:
e enter the current hexadecimal number from the keyboarda add a new hexadecimal number to the current hexadecimal numberm multiply a new hexadecimal number by the current hexadecimal number
h help the user by displaying these choicesq quit the program
Here are some guidelines and what I've written so far.
* Your program should be able to handle whole hexadecimal numbers, either positive or zero, containing any number of digits.
* When your program starts executing, it should automatically start with a current hexadecimal number of 0. Each time before getting the user's choice, the program should display the current hexadecimal number. The hexadecimal number should be displayed in the conventional left-to-right order, and, as is customary, in groups of three digits (grouped from right-to-left) separated by commas. Furthermore, no more than 10 groups of three digits should be displayed on any single line of output -- if more space is needed, the program should skip to the next line, indent to the beginning of the hexadecimal number plus an additional 2 to 4 spaces (whichever is needed to line up the groups vertically), and then continue displaying the hexadecimal number.
* Whenever the user chooses to enter the current hexadecimal number from the keyboard, he or she should be allowed to enter all of the hexadecimal digits (0 through F) of that hexadecimal number, without any embedded commas, in the conventional left-to-right order. The user should also be able to enter either uppercase or lowercase letters for any of the hexadecimal digits A through F. In case the hexadecimal number being entered is especially long, the user should be allowed to hit the "enter" key as many times as desired to go on to new, fresh lines. Finally, after all of the hexadecimal digits have been entered, the user should signal the end of the hexadecimal number by typing the special "#" character as the sentinel.
* Whenever the user chooses to add or multiply the current hexadecimal number with a new hexadecimal number, the program should correctly perform this calculation, making the resulting hexadecimal number become the new current hexadecimal number.
*
#include iostream
using namespace std;
#undef NULL
const int NULL = 0;
typedef int element;
const element SENTINEL = -1;
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;
L.Read();
L.Print();
}
void LList::Print() {
}
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();
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;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
