Question

In: Computer Science

Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in...

Introduction

Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers.

Classes in C++ and Java can represent anything in the real world. This assignment is to write a compiler for Z++ programming language.

The Z++ Programming Language

Your program will test if an expression entered by the application user is valid under the Z++ programming language. Therefore, you need to know a little bit about the Z++ programming language (which of course doesn't really exist, not yet anyway).

The Z++ programming language uses only 6 characters, left and right brackets, parentheses and curly braces: { [ ( } ] ). A left curly brace, bracket or parentheses is referred to as a left character. A right curly brace, bracket or parentheses is referred to as a right character.

A Z++ expression is valid if each left character in the expression "matches" with a corresponding right character in the expression. The test for a match requires going left to right through an expression, character by character. When a right character is encountered, then it is compared to the rightmost left character that was not previously compared to a right character. If the characters match, such as { and }, [and ], or ( and ), then you continue through the expression, comparing each right character to the rightmost left character that was not previously compared to a right character, until either the left and right characters don't match (which means the expression is not valid) or there are no more right characters. When there are no more right characters, if all of the left characters have previously been compared to a right character, the expression is valid. However, if there still are left characters that have not previously been compared to a right character, the expression is invalid.

Let's look at some examples. The following expressions are valid:

[]{}()

{([])}

()[{}]

[{}()]

Note that the matching may be by the right character immediately following the left character, by nesting, or by a combination of the two.

However, the expression [({})) is not valid as [ does not correspond to ). Additionally, the expression ([{}()] is not valid. Even though each right character is matched by a left character, the first left character is left over after you have run out of right characters.

Program Description

Your program, which will be written in C++, not Z++, will prompt the user to enter a string of not more than 20 characters. You may use a character array, a C-string or the C++ string class; the choice is up to you. You can assume the user enters no more than 20 characters (though the user may enter less than 20 characters) and the characters entered are limited to left and right brackets, parentheses and curly braces; you do not need to do any error-checking on this. After the user ends input, by the Enter key, the program checks if the string is a valid Z++ expression, and reports that it either is or isn't. Sample outputs:

Enter an expression: []{}()

It's a valid expression

Enter an expression: ()[{}]

It's a valid expression

Enter an expression: [({)}]

It's NOT a valid expression

Stack Class

Module #3 (http://www.agazaryan.com/csit836/stack.html), which accompanies this assignment, explains a stack and how it will be represented by a class having the member variables and member functions (including a constructor) appropriate for a stack.

Multiple Files

The class will be written using header and implementation files. Your program also will include a driver file, so your multi-file project will have three files:

File Name Purpose
cstack.h Header file for stack
cstack.cpp Implementation file for stack
test.cpp Driver file

Module #3 (http://www.agazaryan.com/csit836/stack.html) gives you the test.cpp file and all the information necessary to write the cstack.h file. Your job is to write the cstack.cpp file. All class members (variables and functions) must be private unless they need to be public.

Solutions

Expert Solution

//Writing cstack.cpp file as asked in question, assuming cstack.h and test.cpp will be taken care of.

// Implementation file cstack.cpp includes a constructor of CStack class and stack utility functions IsEmpty(), IsFull(), Top(), //Pop() and Push()

//Feel free to ask for any clarifications

#include "cstack.h"

CStack::CStack()

{

//character array data[21] should be given in stack.h file

top = -1;

}

//To check whether stack is empty

bool CStack::IsEmpty()

{

return top == -1;

}

//To check whether stack is full

bool CStack::IsFull()

{

//From position 0 to position 19, 20 characters can be pushed in the stack, thus making top as 19

return top == 19;

}

//To return top element of the stack if stack is not empty

char CStack::Top()

{

if(!IsEmpty())

{

return data[top]

}

else

{

return 'ERROR: Stack Empty, Top does not exist'

}

}

// To remove top element of the stack if the stack is not empty

void CStack:Pop()

{   

if(!IsEmpty())

{

top--;

}

else

{

cout<<"ERROR: Stack Empty. Pop operation failed";

}

}

//To enter an element into top position if stack is not full

void CStack::Push(char c)

{

if(!IsFull())

{

data[++top] = c;

}

else

{

cout<<"ERROR: Stack Full. Cannot Push.";

}

}


Related Solutions

C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Chapters 6-8 Review & Molarity Introduction Worksheet Once completed, upload worksheet to the appropriate Assignments folder....
Chapters 6-8 Review & Molarity Introduction Worksheet Once completed, upload worksheet to the appropriate Assignments folder. Write the correct chemical formula for the following combinations or compounds. You must have proper formatting to receive credit! sodium and oxygen Al and O aluminum sulfide potassium phosphide Name the following compounds. Spelling counts! KOH O2 NH4C2H3O2 PbCl2 Solve the following conversions using the dimensional analysis format used in the previous worksheets.  Make sure to show all your work with units (an example has...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at Merks Sales are paid based on an annual salary rather than an hourly wage. However, some employees are paid weekly while others are paid every other week (biweekly). Weekly employees receive 52 paychecks; biweekly employees receive 26 paychecks. The payroll manager wants a program that displays two amounts: an employee’s weekly gross pay and his or her biweekly gross pay. Complete an IPO chart...
what are Sorted lists and their efficiency? in c++ and data structures
what are Sorted lists and their efficiency? in c++ and data structures
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In this assignment you will create a program that will simulate a number guessing game. Skills: random, while-loop, Boolean flags Algorithm to win The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be...
When it comes to stock, companies can issue either preferred stock or common stock. What is...
When it comes to stock, companies can issue either preferred stock or common stock. What is one difference between them? From the company's point of view, what is the difference between issuing stock and bonds?
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition....
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition. Ch. 5, page 220. Take snip of program results.
This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++...
This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++ There should be 3 files for the project: theMain.cpp, dateClass.h, dateClass.cpp (or similar file names) For input/output. Read at least 10 dates in a data file set up this way: The first number is the number of data items in the file. Then after that, each row is one date in the format month day year. here is a sample data file of 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT