In: Computer Science
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.
//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.";
}
}