Question

In: Computer Science

Understand the code and explain the code and answer the questions. Type your answers as comments....

Understand the code and explain the code and answer the questions. Type your answers as comments.

#include

#include

using namespace std;

// what is Color_Size and why it is at the end?

enum Color

{

       Red, Yellow, Green, Color_Size

};

// what is Node *next and why it is there?

struct Node

{

       Color color;

       Node *next;

};

// explain the code below

void addNode(Node* &first, Node* &last, const Color &c)

{

       if (first == NULL)

       {

              first = new Node;

              first->color = c;

              first->next = NULL;

              last = first;

       }

       else

       {

              last->next = new Node;

              last->next->color = c;

              last->next->next = NULL;

              last = last->next;

       }

}

// explain the code below

string colorToString(const Color &c)

{

       if (c == Red) return "Red";

       else if (c == Yellow) return "Yellow";

       else if (c == Green) return "Green";

}

// explain the code below

Color intToColor(const int &i)

{

       if (i == 0) return Red;

       else if (i == 1) return Yellow;

       else if (i == 2) return Green;

       else return Color_Size;

}

// explain the code below and why the number 48 is there?

Color charToColor(const char &c)

{

       if (c >= 48 && c <= (48 + Color_Size - 1))

       {

              return intToColor(c - 48);

       }

       else

       {

              return Color_Size;

       }

}

// explain the code below

string printNodes(Node* &first)

{

       if (first == NULL) return "";

       Node *n = first;

       string text = "List: ";

       while (n != NULL)

       {

              if (n->color == Red) text += colorToString(Red);

              else if (n->color == Yellow) text += colorToString(Yellow);

              else if (n->color == Green) text += colorToString(Green);

              if (n->next != NULL) text += ", ";

              n = n->next;

       }

       return text;

}

// explain the code below

string printMenu()

{

       string text = "Choose Color to Add to the List:\n";

       for (int i = 0; i < Color_Size; i++)

       {

              text += to_string(i) + "> ";

              text += colorToString(intToColor(i));

              text += "\n";

       }

       text += to_string(Color_Size) + "> " + "Print List";

       return text;

}

// explain the code below

bool invalidInput(const char &input)

{

       return (input < 48) || (input > (48 + Color_Size));

}

// explain the code below

void freeMemory(Node* &first)

{

       if (first == NULL) return;

       Node *n = first;

       while (n != NULL)

       {

              first = first->next;

              delete n;

              n = first;

       }

}

int main()

{

       bool loopIt = true;

       char input;

       Node *first, *last;

       first = NULL;

       last = NULL;

       while (loopIt)

       {

              cout << printMenu() << endl << endl;

              cout << "Input: ";

              cin >> input;

              cout << endl;

              if (invalidInput(input))

              {

                     cout << "invalid input! the app will close . . ." << endl;

                     loopIt = false;

              }

              else

              {

                     if (charToColor(input) == Color_Size)

                     {

                           cout << printNodes(first) << endl << endl;

                     }

                     else

                     {

                           addNode(first, last, charToColor(input));

                     }

              }

       }

       freeMemory(first);

       system("pause");

       return 0;

}

Solutions

Expert Solution

#include

#include

using namespace std;

// what is Color_Size and why it is at the end?

/* Color_Size is part of the enum, enum is used to assign integer values to strings, i.e in our case Red will have value 0, Yellow will have value 1, Green will have value 2 and Color_Size will have value 3. Color_Size is at the end so that it corresponds to the number of colors the enum have. */

enum Color

{

       Red, Yellow, Green, Color_Size

};

// what is Node *next and why it is there?

/* Node* next is a pointer pointing to the next node in the list */

struct Node

{

       Color color;

       Node *next;

};

// explain the code below

/* This code is adding a node at the end of the linked list having colors. If the list is empty initially it will add the first node, otherwise it will add the node to end of the linked list. */

void addNode(Node* &first, Node* &last, const Color &c)

{

       if (first == NULL)

       {

              first = new Node;

              first->color = c;

              first->next = NULL;

              last = first;

       }

       else

       {

              last->next = new Node;

              last->next->color = c;

              last->next->next = NULL;

              last = last->next;

       }

}

// explain the code below

/* This code is converting the enum color to string value and returning it. The enum is actually integer values as stated above. So it is converting those integer values to strings. */

string colorToString(const Color &c)

{

       if (c == Red) return "Red";

       else if (c == Yellow) return "Yellow";

       else if (c == Green) return "Green";

}

// explain the code below

/* This code is converting the integer values back to the enum values and returning it. */

Color intToColor(const int &i)

{

       if (i == 0) return Red;

       else if (i == 1) return Yellow;

       else if (i == 2) return Green;

       else return Color_Size;

}

// explain the code below and why the number 48 is there?

/* This code is converting a character variable to it's corresponding enum value and returning it. The number 48 is here because 48 is the ASCII value of the number '0' so its using 48 to convert the character to the integer */

Color charToColor(const char &c)

{

       if (c >= 48 && c <= (48 + Color_Size - 1))

       {

              return intToColor(c - 48);

       }

       else

       {

              return Color_Size;

       }

}

// explain the code below

/* This code is returning a string which contains the colors of all the nodes one by one. It traverses all the nodes of the linked list and add the color of the node to the string and returns that string. */

string printNodes(Node* &first)

{

       if (first == NULL) return "";

       Node *n = first;

       string text = "List: ";

       while (n != NULL)

       {

              if (n->color == Red) text += colorToString(Red);

              else if (n->color == Yellow) text += colorToString(Yellow);

              else if (n->color == Green) text += colorToString(Green);

              if (n->next != NULL) text += ", ";

              n = n->next;

       }

       return text;

}

// explain the code below

/* It will print a menu which will give options to add any color to the list or print the list.*/

string printMenu()

{

       string text = "Choose Color to Add to the List:\n";

       for (int i = 0; i < Color_Size; i++)

       {

              text += to_string(i) + "> ";

              text += colorToString(intToColor(i));

              text += "\n";

       }

       text += to_string(Color_Size) + "> " + "Print List";

       return text;

}

// explain the code below

/* This will check if the input by the user greater than the number of colors present in the enum. It will return true in that case.*/

bool invalidInput(const char &input)

{

       return (input < 48) || (input > (48 + Color_Size));

}

// explain the code below

/* This will delete our linked list of nodes and free the memory taken up by the nodes.*/

void freeMemory(Node* &first)

{

       if (first == NULL) return;

       Node *n = first;

       while (n != NULL)

       {

              first = first->next;

              delete n;

              n = first;

       }

}

int main()

{

       bool loopIt = true;

       char input;

       Node *first, *last;

       first = NULL;

       last = NULL;

       while (loopIt)

       {

              cout << printMenu() << endl << endl;

              cout << "Input: ";

              cin >> input;

              cout << endl;

              if (invalidInput(input))

              {

                     cout << "invalid input! the app will close . . ." << endl;

                     loopIt = false;

              }

              else

              {

                     if (charToColor(input) == Color_Size)

                     {

                           cout << printNodes(first) << endl << endl;

                     }

                     else

                     {

                           addNode(first, last, charToColor(input));

                     }

              }

       }

       freeMemory(first);

       system("pause");

       return 0;

}


Related Solutions

Based on the below code: Answer the following questions and explain your answers: i)Presume that the...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the code is instantiated in a process and it has JUST returned from its own call to fork() and there is now a clone of that process that is itself “just about” to pick up execution. Where will the clone child pick up its execution? ii)How does the clone child know it’s a clone? How does the parent process know it is not a clone,...
There are 3 short answer questions in this paper. Type your answers where indicated. Your answers...
There are 3 short answer questions in this paper. Type your answers where indicated. Your answers can be in bullet point form. A local resort in the Blue Mountains is for sale. What are the potential valuation effects of COVID-19 and of the bad bushfire season the year before? Identify how these events would be taken into consideration in a valuation model. Be sure to identify positive or negative valuation effects and whether these effects are short term or long...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an ARM assembly program to calculate the value of the following function: f(y) = 3y^2 – 2y + 10 when y = 3. My Code below, that needs comments added: FunSolve.s LDR R6,=y LDR R1,[R6] MOV R2,#5 MOV R3,#6 MUL R4,R1,R1 MUL R4,R4,R2 MUL R5,R1,R3 SUB R4,R4,R5 ADD R4,R4,#8 st B st y DCD 3
Select three (3) of the five questions to answer. You must type your answers and your...
Select three (3) of the five questions to answer. You must type your answers and your responses to each question should be no more than 500 words per question (1,500 total). Reference/cite the content in the relevant modules to support your answers/argument - no external sources or group work. Protest movements can have an illusion of victory. Sometimes they maintain a state of balance between opposing forces or actions, an equilibrium (i.e., gained something here, but lost something there). Use...
please I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
Answer true or false to the following questions please explain your answers and answer all please  ...
Answer true or false to the following questions please explain your answers and answer all please   1.The cytoplasmatic side of the integral membrane proteins is often glycosylated. 2. Cholesterol is more enriched at the outer leaflet of the plasma membrana 3.Inner leaflet of the plasma membrane is enriched with glycolipids. 4.Membrane proteins that pomp ions in and out of the cell have specific Km values. 5. A symport would function as an antiport if its orientation in the membrane were...
As you answer these questions, be sure that you EXPLAIN YOUR ANSWERS IN DETAIL AND SHOW...
As you answer these questions, be sure that you EXPLAIN YOUR ANSWERS IN DETAIL AND SHOW YOUR CALCULATIONS. Remember you are showing off how much you know about economics and one or two sentences shows me you don’t know very much. Question One: Fiscal Policy Assume the United States economy has the following: • GDP is $18,500 billion down from $19,350 billion nine months ago. • Unemployment is at 6.8% up from 4.2% nine months ago. • Inflation is stable...
Please read and understand the following program code. Use it to answer the following 4 questions....
Please read and understand the following program code. Use it to answer the following 4 questions. #include <iostream> #include <string> #include <sstream> using namespace std; class timeStamp // uses 24-hour times (0..24) { public:     timeStamp(void);     timeStamp(int, int, int);     void setTime(int, int, int);     timeStamp *addTimes(timeStamp *);     timeStamp *subTimes(timeStamp *);     string toString(void);     int hour;     int minute;     int second; }; int main() {     timeStamp noon(12, 0, 0);     timeStamp *teaTime = new timeStamp(51,...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a string to integer. The function will take the number as a string then will return it as integer. E.g. str2int(“123”) will return 123 str2int(“005”) will return 5 str2int(“102”) will return 102
Answer each question. Type question and answer, provide a cited rationale for your answers. Answer the...
Answer each question. Type question and answer, provide a cited rationale for your answers. Answer the following questions using the discussion board: • What is Orthostatic Hypotension? • Name the Chain of Infection • How do you collect data about pain from your patients? • Name the types of isolation precaution. and why I choose these answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT