Questions
the purpose of the code is to implement a stack how can i convert this code...

the purpose of the code is to implement a stack

how can i convert this code in a way that these changes apply to it?

// simplify the logic of the main

// getline(cin, line) needs to be changed to a normal cin >> line;

//make a function for 'list'

// not allowed temporary stack (stack s2)

//merge the catches (errors)

// introduce another function for main

// avoid repetitive code

here is the code:

#include

#include

#include

#include

#include

using namespace std;

class Stack {

    public:

        bool isEmpty();

        int top();

        int pop();

        void push(int);

    private:

        vector elements;

};

bool Stack::isEmpty() {

    return elements.empty();

}

int Stack::top() {

    if(isEmpty()) {

        throw runtime_error("error: stack is empty");

    }

    else {

        return elements.back();

    }

}

int Stack::pop() {

    if(isEmpty()) {

        throw runtime_error("error: stack is empty");

    }

    else {

        int item = elements.back();

        elements.pop_back();

        return item;

    }

}

void Stack::push(int item) {

    elements.push_back(item);

}

int main() {

    string line, cmd;

    int val;

    Stack stack;

    cout << "stack> " << endl;

    while(getline(cin, line)) {

        if(line.find_first_not_of(" \t") == string:: npos) {

            cout << "stack> ";

            continue;

        }

        line = line.substr(line.find_first_not_of(" \t"));

        cmd = line.substr(0, line.find(" "));

        if(cmd == "push") {

            try {

                line = line.substr(line.find(" ") + 1);

                if(line.size() == 0 || line.at(0) == '-' || line.at(0) == '+' && line.find_first_of("0123456789") > 1 || line.find_first_not_of("0123456789+-") == 0) {

                    throw runtime_error("error: not a number");

                }

            

                else {

                    val = atoi(line.c_str());

                    stack.push(val);

                }

            }

            catch(runtime_error& excpt) {

                cout << excpt.what() << endl;

            }

        }

        else if(cmd == "pop") {

            try {

                val = stack.pop();

                cout << val << endl;

            }

            catch(runtime_error& excpt) {

                cout << excpt.what() << endl;

            }

        }

        else if(cmd == "top") {

            try {

                val = stack.top();

                cout << val << endl;

            }

            catch(runtime_error& excpt) {

                cout << excpt.what() << endl;

            }

        }

        else if(cmd == "list") {

            Stack s2;

            bool first = true;

            cout << "[";

            while(!stack.isEmpty()) {

                val = stack.pop();

                s2.push(val);

                if(!first) {

                    cout << ",";

                }

                cout << val;

                first = false;

            }

            cout << "]" << endl;

            while(!s2.isEmpty()) {

                stack.push(s2.pop());

            }

        }

        else if(cmd == "end") {

            break;

        }

        else {

            cin.ignore(numeric_limits::max(), '\n');

            cout << "error: invalid command" << endl;

        }

        cout << "stack> " << endl;

    }   

}

In: Computer Science

Understand the following tools, the steps in creating the tool, and the purpose/usefulness of each: Activity...

Understand the following tools, the steps in creating the tool, and the purpose/usefulness of each: Activity Diagram, Use Case Description, Use Case Diagram, CRC Card, Class Diagram, Sequence Diagrams, Communication Diagram, or Behavioral State Machine. Define it, describe its purpose/usefulness, and list/define the major steps in using the tool. You should be able to illustrate the tool using a scenario.

In depicting a class on a class diagram, each class is shown with 3 parts. Define each part and explain what each part shows. Include an illustration.

In: Computer Science

Using C++, identify suitable coding modules for the following (a) Overload the * operator so that...

Using C++, identify suitable coding modules for the following

(a) Overload the * operator so that two instances of Quat can be multiplied using the * operator.

Given that q1 and q2 are Quaternions. Let q1 = (a1, b1i, c1j, d1k) and q2 = (a2, b2i, c2j, d2k)

The product (q1 * q2) is ( a1a2 – b1b2 – c1c2 – d1d2, (a1b2 + b1a2 + c1d2 – d1c2)i, (a1c2 + c1a2 + d1b2 – b1d2)j, (a1d2 + d1a2 + b1c2 – c1b2)k )

For example
sq1 = (5, 2i, 6j, 8k)
sq2 = (3, 5i, 7j, 6k)
sq3 = sq1 * sq2 = (-85, 11i, 81j, 38k)


(b) Overload the == operator so that we can check whether two instances of Quat are equal. Two instances are equal if each element in one instance is equal to the corresponding element in the other instance.

In: Computer Science

You have 10 shirts, 9 pants, and 4 belts. How many outfits can you make out...

You have 10 shirts, 9 pants, and 4 belts. How many outfits can you make out of these? (Assume all colors are perfectly coordinated.)

In: Computer Science

What are some of the basic precautions a computer owner or network administrator can take to...

What are some of the basic precautions a computer owner or network administrator can take to help make the computing environment secure?

What are some of the basic security threats that a user or administrator might have to face?

In: Computer Science

In the system development life cycle (SDLC), why does the planning phase exists and what does...

In the system development life cycle (SDLC), why does the planning phase exists and what does it contribute to the development of a system?

In: Computer Science

In the system development life cycle (SDLC), why does the design phase exists and what does...

In the system development life cycle (SDLC), why does the design phase exists and what does it contribute to the development of a system?

In: Computer Science

Unix treats file directories in the same fashion as files; that is, both is defined by...

Unix treats file directories in the same fashion as files; that is, both is defined by the same type of data structure, call an inode. As with files, directories include a non-bit protection string. If care is not taken, this can create access control problems. For example, consider a file with protection mode 644 (rw- r-- r--) contained in a directory with protection mode 730 (rwx -wx ---). Write a small report (~250 words) how might the file be compromised in this case?

In: Computer Science

Program #3 ● Filename: pig.py For this assignment, you’ll implement a dice game called PIG, pitting...

Program #3

● Filename: pig.py

For this assignment, you’ll implement a dice game called PIG, pitting two human players against each other. PIG is played with a standard six-sided die and proceeds as follows:

  • ● Each player starts with zero points, and the first to 20 points wins.

  • ● The current player chooses to roll or hold.

  • ● If they choose roll:

    • ○ The six-sided die is rolled

    • ○ The value of the die is added to their round points.

    • ○ The player goes again UNLESS...

    • ○ ...If the value on the die is 1, then the round is over and the player has lost all the points they

      accumulated in the round.

  • ● If they choose hold:

    • ○ The points accumulated in the round are added to the player’s overall score.

    • ○ The round is over.

  • ● When the round ends (either because the player rolled a 1 or they chose to hold), it becomes the other

    player’s turn unless somebody has won.

  • ● We check to see if someone won at the end of each round (which is kind of unfair, because if Player

    One gets 20 points on the first round, then Player Two never gets a chance, but oh well tough for them).

    Requirements:

  • ● Prompt each player to either R (roll) or H (hold).

  • ● If they enter anything else, continue prompting them until they enter a correct input.

  • ● On a roll, randomly generate a value 1-6 to represent the die.

  • ● End the round when the die roll is value 1 (round points are lost), or the player chooses Hold (round

    points are added to that player’s overall score).

  • ● End the game one either player has 20 or more points and announce the winner.

  • ● Report everything as you go along -- what the die roll was, number of points so far, etc.

  • ● As always with your CS5001 programs, your input/output must be friendly and informative.

    Helpful hints

    Python has a random module that can help you generate random numbers. You probably want to import random for this part of the assignment and call randint(...)

    import random # near the top of your file random.randint(1, 6) # or something like this in your program

    AMAZING points:

  • ● Allow them to enter upper or lowercase letters. R/r for roll, and H/h for hold.

  • ● Make your code extensible enough that we could add an arbitrary number of players with minimal

    coding-pain. (Consider using a list to store the player names and another list for the number of points each player has.)

In: Computer Science

I want to compare between breaking the shift (Caesar Cipher), substitution, and Vigenere cipher by using...

I want to compare between breaking the shift (Caesar Cipher), substitution, and Vigenere cipher by using a chosen-plaintext attack or by known-plaintext attack. How much plaintext must be encrypted in order for the adversary to completely recover the key for those ciphers in chosen-plaintext attack and in known-plaintext attack? is there difference in each attack?

In: Computer Science

Create a c++ program to compute the product of two integers. call create the following functions:...

Create a c++ program to compute the product of two integers. call create the following functions:

1. getNum - to accept only positive numbers && will call computeProd.

2.computeProd - to compute the product of the numbers & will call the function displayProduct.

3. displayProduct - to display the product.

Call the function getNum in the main function.

Compute the product w/o using the multiplication operator(*).

using #include <iostream> only

In: Computer Science

Python Please An n-sided regular polygon has all its sides of the same length and all...

Python Please

An n-sided regular polygon has all its sides of the same length and all its angles of the same degree. It is also called an equilateral and equiangular polygon. In this activity, you will design a class named RegularPolygon that contains:

  1. A private int data attribute named n that defines the number of sides of the polygon.
  2. A private float attribute named side that stores the length of the side.
  3. The constructor ( __init__ method) that creates a regular polygon with the specified n (default 3), side (default 1).
  4. The accessor and mutator methods for all data fields.
  5. The method getPerimeter () that returns the perimeter of the polygon. (use perimeter = n*side)
  6. The method getArea() the returns the area of the polygon. The formula for computing the area of a regular polygon of area = (n*s2)/(4*tan(pi/n)), s is the side.
  7. A __str__ method that will display the data attributes of RegularPolygon class.

Write a main function to test the RegularPolygon class.

  1. Create four objects of RegularPolygon class using a variety of calls to the initializer (or constructor).
  2. Make calls to the mutator methods and display the object each time.
  3. Make calls the accessor methods and display the data attribute each time.
  4. Make calls to the getPerimeter and getArea and display the value returned each time.

In: Computer Science

In Angel, you will find a class called Employee. This class should include a constructor which...

In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method).

You will create a new class called Manager that inherits from the class Employee. Add a field named department of type String. Supply a method toStringthat prints the manager's name, department, and salary. Remember, you may not change anything in the Employee class.

You will then create a test class that uses the Manager class. The test class should prompt the user to enter name, department and salary. This information should be stored in an array. Upon entry of 3 employee records, the program should output the information you entered.

Your program should be able to handle a maximum of 3 entries.

You may write the program as a console application or using dialog boxes.

please write a code using java. Make sure the code is correct for java eclipse.

In: Computer Science

C++ Language Create Login Register program without open .txt file simple program also if you can...

C++ Language

Create Login Register program without open .txt file
simple program also if you can hide password input
option menu
1) Login
2) Register
3) exit

In: Computer Science

b) With the aid of examples, discuss the various issues that could indicate that a Disaster...

b) With the aid of examples, discuss the various issues that could indicate that a Disaster Recovery Plan is not in order. [25 marks].

In: Computer Science