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.
Source Code:
#include<iostream>
using namespace std;
class Stack{
private:
char *ch;
int topPtr = -1;
public:
Stack(int size){
topPtr =
-1;
ch = new
char[size];
}
void push(char x){
if(topPtr ==
19){
cout<<"Stack Overflow"<<endl;
return;
}
ch[++topPtr] =
x;
}
void pop(){
if(topPtr ==
-1){
cout<<"Stack is empty"<<endl;
return;
}
topPtr--;
}
char top(){
if(topPtr ==
-1){
cout<<"Stack is empty"<<endl;
return NULL;
}
return
ch[topPtr];
}
bool isEmpty(){
if(topPtr ==
-1)
return true;
return
false;
}
bool isFull(){
if(topPtr ==
19)
return true;
return
false;
}
};
bool paranthesisChecking(char *s){
Stack stk(20);
int i = 0;
while(s[i]!=NULL){
if(s[i] == '{' || s[i] == '[' ||
s[i] == '('){
stk.push(s[i]);
i++;
}
else if(s[i] == '}' &&
stk.top() == '{'){
stk.pop();
i++;
}
else if(s[i] == ')' &&
stk.top() == '('){
stk.pop();
i++;
}
else if(s[i] == ']' &&
stk.top() == '['){
stk.pop();
i++;
}
else{
return
false;
}
}
if(stk.isEmpty())
return true;
return false;
}
int main(){
for(int i=0;i<3;i++){
char s[20];
cout<<"Enter an expression:
";
cin.getline(s,20);
bool res =
paranthesisChecking(s);
if(res){
cout<<"It\'s a valid expression"<<endl;
}
else{
cout<<"It\'s NOT a valid expression"<<endl;
}
}
}
Output:
Please appreciate the solution if you find it helpful.
If you have any doubts in the solution, feel free to ask me in the comment section.