Introduction Introduction to Data Structures programming assignments can be comp
ID: 3748410 • Letter: I
Question
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, 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:
Module #3 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.
MODULE 3 TEST.CPP FILE
Driver
Neither the header nor the implementation file for either class contains a main function. As you know, a C++ program cannot run without one (and only one) main function. Therefore, your project will include, in addition to a header and implementation file for each class (the stack and the queue), a "driver" file to test the project. A driver is a .cpp file used to test your project, and it is the one (and only) file in your project to have the main function..
Below is partial code (I'm not going to give it all away) for the driver file. As before, the code is a bold type and my annotations are italicized.
// test.cpp, the code for driver file
#include "cstack.h"
#include <iostream>
#include <cstring>
using namespace std;
/*
You need to include cstack.h because the driver file refers to the CStack class.
You also need need to include standard header files iostream and cstring because
the driver file uses standard library functions such as cin and cout and C-string functions also
*/
bool isValidExpression (CStack&, char*);
/*
This is the prototype of a function implemented later in this file
(this function is not a member function of any class) that determines
if the expression that is its second parameter is a valid G++ expression
and returns true if the expression is valid, false otherwise. This determination
is made using the CStack class, an instance of which is the first parameter.
That parameter is declared by reference but also could be declared by value.
*/
int main (void) // The driver file has the main function
{
char expression[21]; // allocate memory for user string input
cout<< "Enter an expression: ";
cin >>expression;
CStack stack1; // creates an instance of a stack (stack1) using class constructor
if (isValidExpression (stack1, expression))
/* This calls the isValidExpression function (which you still need to write) to
fill the data array of the stack. The parameter is the stack instance stack1 */
cout << " It's a valid expression";
else
cout << " It's NOT a valid expression";
return 0;
}
/* This ends the main function. Now we (you) have
to write the isValidExpression function. I'll give you
the function header, and then some hints. */
bool isValidExpression (CStack& stackA, char* strExp)
{
// code goes here
}
// end of driver file
File Name Purpose cstack.h Header file for stack cstack.cpp Implementation file for stack test.cpp Driver fileExplanation / Answer
test.cpp
#include<bits/stdc++.h>
#include "Cstack.h"
using namespace std;
bool isValidExpression(CStack& stackA, string strExp);
int main()
{
string expression;
cout<<"Enter the expression";
cin>>expression;
CStack stack1;
if(isValidExpression(stack1, expression))
{
cout<<"Expression is Valid"<<endl;
}
else
{
cout<<"Expression is Not Valid";
}
return 0;
}
bool isValidExpression(CStack& stackA, string strExp)
{
int i=0, len=strExp.length();
while(i<len)
{
if(!stackA.isEmpty() && ((strExp[i]==')' &&stackA.top()=='(')||(strExp[i]=='}' &&stackA.top()=='{')||(strExp[i]==']' &&stackA.top()=='[')))
{
stackA.pop();
}
else
{
if(strExp[i]=='{' || strExp[i]=='(' || strExp[i]=='[')
{
stackA.push(strExp[i]);
}
else
return false;
}
i++;
}
if(!stackA.isEmpty())
return false;
else
return true;
}
Cstack.h
class CStack
{
int t;
public:
char s[20];
CStack(){t=-1;}
bool push(char x);
bool pop();
char top();
bool isEmpty();
};
bool CStack ::push(char x)
{
if(t>=20-1)
{
return false;
}
else
{
s[++t]=x;
return true;
}
}
bool CStack :: isEmpty()
{
if(t==-1)
return true;
else
return false;
}
bool CStack :: pop()
{
if(isEmpty())
{
return false;
}
else
{
t--;
return true;
}
}
char CStack::top()
{
if(isEmpty())
{
return -1;
}
else
{
return s[t];
}
}
//If you have any doubt ask in the comment section
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.