Computer Science C++

Online tutoring services

Need help with this question or any other Computer Science assignment help task? Click on the button below to to hire an expert cheap.

Assignment 7

In this assignment, you will write one class and a function that will convert an infix expression to an equivalent postfix expression. You will not write a main() routine. One will be supplied for you and it will call your conversion function.

1. Initial Setup

  1. Log in to Unix.
  2. Run the setup script for Assignment 7 by typing:
    setup 7

2. Files We Give You

When you run the setup command, you will will get four files: the usual makefileinpost_main.cpp, which is the main routine that calls your functions; a sample input file called; and the correct output generated by that input file, postfix.key.

Once again, use diff to verify that your output matches that found in postfix.key.

3. Files You Must Write

You will write the following files:

  • mystack.h - contains the class definition for the mystack class.
  • mystack.cpp - contains the definitions for member functions of the mystack class.
  • inpost.cpp - contains your convert() function.
  • inpost.h - contains the function prototype for convert() so that the main() can call it.

Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in the same source file.

3.1. The mystack class

The mystack class represents a stack of characters implemented as an array. This stack will be used by the algorithm that converts an infix string to a postfix string.

Like the other classes we've written this semester, this class should be implemented as two separate files. The class definition should be placed in a header file called mystack.h.

Data Members

The mystack class should contain the following private data members:

  • a pointer to an char. I'll refer to this data member as the stack array pointer. It will be used to dynamically allocate an array of char (the stack array).
  • size_t variable used to keep track of the number of elements in the stack array. I'll refer to this data member as the stack capacity.
  • size_t variable used to track the number of values current stored in the stack array. I'll refer to this data member as the stack size. The stack size must always be less than or equal to the stack capacity. Unless the stack is empty, the top item in the stack is always located in the stack array at location (stack size - 1).

The data type size_t is defined in several header files, including <cstddef> and <cstdlib>.

In addition to the data members described above, your class definition will need prototypes for the public member functions described below.

Member Functions

The definitions for the member functions of the class should be placed in a separate source code file called mystack.cpp. Make sure to #include "mystack.h" at the top of this file.

The mystack class should have the following member functionss (most of which are quite small):

  • mystack::mystack()
  • This "default" constructor for the mystack class should initialize a new mystack object to an empty stack. When the function ends:
  • The stack size for the new object should be 0.
  • The stack capacity for the new object should be 0.
  • The stack array pointer should be nullptr.
  • mystack::mystack(const mystack& x)
  • This "copy constructor" for the mystack class should initialize a new mystack object to the same string as the existing mystack object x. When the function ends:
  • The stack size for the new object should be equal to the stack size of the object x.
  • The stack capacity for the new object should be equal to the stack capacity of the object x.
  • If the stack capacity is 0, the stack array pointer for the new object should be nullptr. Otherwise, the stack array pointer should point to an array of char with a number of elements equal to the stack capacity. The array should contain the same values as the stack array of the object x. The contents of any array elements that are not actually part of the stack are unimportant.
  • mystack::~mystack()
  • The destructor should delete the stack array.
  • mystack& mystack::operator=(const mystack& x)
  • This overloaded copy assignment operator should assign one mystack object (the object x) to another (the object that called the member function, which is pointed to by this). The state of the data members when the function ends should be same as described above for the copy constructor.
  • size_t mystack::capacity() const
  • This member function should return the stack capacity.
  • size_t mystack::size() const
  • This member function should return the stack size.
  • bool mystack::empty() const
  • This member function should return true if the stack size is 0. Otherwise, it should return false.
  • void mystack::clear()
  • This member function should set the stack size back to 0.
  • void mystack::reserve(size_t n)
  • This member function modifies an object's stack capacity without changing the stack size or the contents of the stack array. The required logic is:
  • If n is less than the stack size or n is equal to the current stack capacity, simply return.
  • Set the stack capacity to n.
  • Declare a temporary array pointer (a pointer to an char).
  • Use the temporary array pointer to dynamically allocate an array of char. The number of elements in the new temporary array should be equal to the stack capacity.
  • Copy the contents of the stack array into the temporary array.
  • Delete the stack array.
  • Set the stack array pointer to the temporary array pointer.
  • const char& mystack::top() const
  • This member function should return the top item in the stack. You may assume this function will not be called if the stack is empty.
  • void mystack::push(char value)
  • This member function should push the character value onto the top of the stack. The required logic is:
  • If the stack size is equal to the stack capacity, the reserve() function will need to be called to increase the stack's capacity. If the current capacity is 0, the capacity should be increased to 1. Otherwise, the current capacity should be doubled.
  • Copy value into the stack array as the new top item in the stack.
  • Increase the stack size by 1.
  • void mystack::pop()
  • This member function should pop the top item off of the stack by decreasing the stack size by 1. You may assume this function will not be called if the stack is empty.

Important Point

Some of the member functions of the mystack class will not be used in Assignment 7. However, you are still required to write them, and you should expect to lose points if they do not work correctly. Thoroughly testing the member functions of this class to make sure that they work is your responsibility.

3.2. inpost.cpp

This file should contain a definition for the following function:

    string convert(const string& infix)

This function converts the infix expression passed to it as a C++ string into an equivalent postfix expression stored in a string object. You may assume that infix is a valid expression, that only '(' and ')' will be used (i.e., no '{}' or '[]' will appear in the expression), that all variables will be single characters always in lower case, and that all constants will be integers.

The operators used, in order of precedence from highest to lowest, are

  1. ~ (unary negation) and ^ (exponentiation)
  2. * (multiplication) and / (division)
  3. + (addition) and - (subtraction)

Your function must take the expression in infix (which is a C++ string that may contain unnecessary whitespace and/or parentheses) and convert it to a postfix string stored in a mystring object. It is very important that the postfix expression does not contain any leading or trailing whitespace and that the operators/operands are separated by exactly one space.

Infix to postfix conversion algorithm

Here is a description of the logic for the infix to postfix conversion using a stack.

  • Let opstack be a stack used to hold operators during conversion.
  • Let infix be a string containing the infix (input) expression tokens.
  • Let postfix be a string where a postfix (output) expression will be constructed.

To convert the infix expression to a postfix expression:

Scan the infix string from left to right, extracting and processing one token at a time, skipping over whitespace:

  • If the token is an operand, append it to the postfix string.
  • If the token is a left parenthesis '(', push it on the opstack.
  • If the token is a right parenthesis ')', pop the opstack until the corresponding left parenthesis is removed. Append each operator popped to the end of the postfix string and discard the '(' and ')' parenthesis tokens.
  • If the token is an operator then, first, pop all operators from opstack and append them to the postfix string until either a) opstack is empty or b) the operator on opstack has a lower precedence than the new operator token. Then push the new operator token into opstack.

When the end of the infix string is encountered, pop and append all remaining operators from opstack and append them to the postfix string.

4. Output

The only output from this program is generated by the main routine supplied for you in inpost_main.cpp.

The following is a sample of what the given main() function will output when you run your finished program.

  infix: ( d +1) *2
postfix: d 1 + 2 *

  infix: a-e-a
postfix: a e - a -

  infix: (a-e-a)/(  ~d + 1)
postfix: a e - a - d ~ 1 + /

  infix: (a^2 + ~b ^ 2) * (5 - c)
postfix: a 2 ^ b ~ 2 ^ + 5 c - *

  infix: ~ 3*~(a+1)- b/c^2
postfix: 3 ~ a 1 + ~ * b c 2 ^ / -

  infix: 246 + b /123
postfix: 246 b 123 / +

  infix: ( 246+(( b /123) ) )
postfix: 246 b 123 / +

5. Hints

  • Start by writing the mystack class, making sure to test it thoroughly. As with the matrix class from Assignment 6, you should write the member functions for this class incrementally, building and testing as you go.
  • The mystack class is extremely similar in terms of its memory management to the Vector class used as an example in the notes on dynamic storage allocation, so you can use that as your model when writing the destructor, copy constructor, and copy assignment operator. The logic for the various member functions of the class is also identical to what's outlined in the notes on array-based stacks.
  • Save the convert() function for last. You may want to make a copy of and modify it to only include a few simple infix expressions at first, adding more as you proceed.
  • You may want to write a function that takes a character operator and returns an integer precedence for that operator. This can be done using a simple switch statement or something more sophisticated. The precedence of a left parenthesis character '(' should be less than that of any of the operators.
  • If you look at the algorithm to convert infix to postfix you will note that you never use the stack to store operands (variables or constants). You only use the stack to store operators or '(', which are always single characters.
  • The way the mystack class works, the operation "pop the stack and append what was popped to the postfix string" will actually require three steps: 1) call top() to get the top character from the stack, 2) append that character to the postfix string (adding a space separator as needed), and 3) call pop() to remove the top character from the stack.
  • In processing the infix string, you may find it useful to use the standard library functions isspace()isdigit(), and islower(), which are declared in the header file <cctype>.
  • It is also worth familiarizing yourself with the member functions of the C++ string class. For example, there are several member functions for appending to a string that might be very useful on this assignment.

Get Help With a similar task to - Computer Science C++

Login to view and/or buy answers.. or post an answer

Related Questions

Similar orders to Computer Science C++
Make code and test for a pac man type game.
At the very least I would need the StudentAttackerController.Java code. The code would need to be tested by the “TestAgent - Scored” configuration that would run 100 trials and give an average score at the end. The average score for the code need to have a score 6500 or higher....
Gameboard with hidden tiles; three guesses to pick all safe squares; gameboard based on files from user input (included)
For this assignment, you will be writing a game. The rules of the game are simple you have a game board with hidden tiles. You have to make three guesses if you pick all safe squares, you win, if you select a bad square, you lose. All game boards have are square, meaning both sides have the same length. The game board size will never be larger than 25 or below 1. The gameboard contains only 4 possible values (3 are requirements, the 4th is for bonus). If the square contains a 'S' it is safe. Meaning you have successfully made one guess. If the square contains a 'B' you lose. Meaning the game is over. If the square contains a 'R' you must pick again, this guess doesn’t count towards your guesses. If the square contains a 'C' it safely clears the area. (See bonus information in code) A game board file has the format ( for a size of 3. ) 3 SSS SRS SBS The game ends after successfully guessing three safe squares or guessing a losing square. After each guess, your program should display the contents of the gameboard square and print the corresponding message. Assignment Requirements Unlike previous examples, this assignment provides a starting point. Would you please c...
C++ computer science programming project
Add the ability for your Canadian Experience scene to have two running machines. At some point in your animation you will indicate that a machine is to start running. You might have your character pretend to flip a switch, for example. From there on the machine will run continuously. You will create your code in the library MachineLib. It is an additional project in your solution. You will get your solution working using the MachineDemo project, also an additional project in your solution. MachineDemo will allow you to work on just the machines, getting them working first. Then you will integrate them into your project. I attach some of instructions...
this assignment is to do it on Mysql workbench
use Mysql workbench to creat all queries see the watched file and follow everything on it it has everything you need to know. please let me know if you have any question...