Please do not forget the multiple files Question: As you know from the first ass
ID: 3586594 • Letter: P
Question
Please do not forget the multiple filesQuestion:
As you know from the first assignment, at ancient Val E Community College, an evil computer science instructor named Genghis Khent delighted in tormenting his Data Structures students with apparently unsolvable questions on data structures. Being a community college instructor, he also was always looking for more ways to make money, especially since he had a free source of programming labor, his Data Structures students.
Genghis Khent thought he could make money with a new programming language, G++ (the G being for Genghis of course). He told his hapless Data Structures students to write a G++ compiler. The student's reward for success was an "A" on the assignment. However, the price of failure was high, a mandatory "all you can eat" lunch at the Val E College Cafeteria. Few survived, and those who did wished they hadn't.
According to legend, a direct descendant of Genghis Khent teaches at this college and intends to continue in the footsteps of his ancestor. You do not know the identity of this descendant because the descendant has cleverly changed his last name just enough to hide his ancestry, but soon (like now) you will have to write the same program.
The G++ Programming Language
Your program will test if an expression entered by the application user is valid under the G++ programming language. Therefore, you need to know a little bit about the G++ programming language (which of course doesn't really exist, not yet anyway).
The G++ 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 G++ 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 G++, 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 G++ 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:
File Name Purpose cstack.h Header file for stack cstack.cpp Implementation file for stack test.cpp Driver file 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.
What You Turn In Turn in to CANVAS a zip file of cstack.h, cstack.cpp and test.cpp. I do not need or want the other files that Visual C++ generates—just the code you wrote—as I can just include your files in a project I create. You also may include in your zip file a pleaformercy.txt file with any comments or message you want to give to me. Please do not forget the multiple files
Question:
As you know from the first assignment, at ancient Val E Community College, an evil computer science instructor named Genghis Khent delighted in tormenting his Data Structures students with apparently unsolvable questions on data structures. Being a community college instructor, he also was always looking for more ways to make money, especially since he had a free source of programming labor, his Data Structures students.
Genghis Khent thought he could make money with a new programming language, G++ (the G being for Genghis of course). He told his hapless Data Structures students to write a G++ compiler. The student's reward for success was an "A" on the assignment. However, the price of failure was high, a mandatory "all you can eat" lunch at the Val E College Cafeteria. Few survived, and those who did wished they hadn't.
According to legend, a direct descendant of Genghis Khent teaches at this college and intends to continue in the footsteps of his ancestor. You do not know the identity of this descendant because the descendant has cleverly changed his last name just enough to hide his ancestry, but soon (like now) you will have to write the same program.
The G++ Programming Language
Your program will test if an expression entered by the application user is valid under the G++ programming language. Therefore, you need to know a little bit about the G++ programming language (which of course doesn't really exist, not yet anyway).
The G++ 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 G++ 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 G++, 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 G++ 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:
File Name Purpose cstack.h Header file for stack cstack.cpp Implementation file for stack test.cpp Driver file 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.
What You Turn In Turn in to CANVAS a zip file of cstack.h, cstack.cpp and test.cpp. I do not need or want the other files that Visual C++ generates—just the code you wrote—as I can just include your files in a project I create. You also may include in your zip file a pleaformercy.txt file with any comments or message you want to give to me. Please do not forget the multiple files
Question:
As you know from the first assignment, at ancient Val E Community College, an evil computer science instructor named Genghis Khent delighted in tormenting his Data Structures students with apparently unsolvable questions on data structures. Being a community college instructor, he also was always looking for more ways to make money, especially since he had a free source of programming labor, his Data Structures students.
Genghis Khent thought he could make money with a new programming language, G++ (the G being for Genghis of course). He told his hapless Data Structures students to write a G++ compiler. The student's reward for success was an "A" on the assignment. However, the price of failure was high, a mandatory "all you can eat" lunch at the Val E College Cafeteria. Few survived, and those who did wished they hadn't.
According to legend, a direct descendant of Genghis Khent teaches at this college and intends to continue in the footsteps of his ancestor. You do not know the identity of this descendant because the descendant has cleverly changed his last name just enough to hide his ancestry, but soon (like now) you will have to write the same program.
The G++ Programming Language
Your program will test if an expression entered by the application user is valid under the G++ programming language. Therefore, you need to know a little bit about the G++ programming language (which of course doesn't really exist, not yet anyway).
The G++ 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 G++ 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 G++, 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 G++ 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:
File Name Purpose cstack.h Header file for stack cstack.cpp Implementation file for stack test.cpp Driver file 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.
What You Turn In Turn in to CANVAS a zip file of cstack.h, cstack.cpp and test.cpp. I do not need or want the other files that Visual C++ generates—just the code you wrote—as I can just include your files in a project I create. You also may include in your zip file a pleaformercy.txt file with any comments or message you want to give to me.
Explanation / Answer
Given below are the needed files
cstack.h
class CStack {
public:
CStack();
bool IsEmpty();
bool IsFull();
void Push(char item);
void Pop();
char Top();
private:
int top;
char data[21];
};
cstack.cpp
#include "cstack.h"
CStack::CStack()
{
top = 0;
}
bool CStack::IsEmpty()
{
return top == 0;
}
bool CStack::IsFull()
{
return top == 21;
}
void CStack::Push(char item)
{
if(!IsFull())
data[top++] = item;
}
void CStack::Pop()
{
if(!IsEmpty())
top--;
}
char CStack::Top()
{
return data[top-1];
}
test.cpp
#include "cstack.h"
#include <iostream>
#include <cstring>
using namespace std;
bool isValidExpression (CStack&, char*);
int main (void)
{
char expression[21];
cout<< "Enter an expression: ";
cin >>expression;
CStack stack1; // creates an instance of a stack (stack1) using class constructor
if (isValidExpression (stack1, expression))
cout << " It's a valid expression" << endl;
else
cout << " It's NOT a valid expression" << endl;
return 0;
}
bool isValidExpression (CStack& stackA, char* strExp)
{
char ch;
for(int i = 0; strExp[i] != ''; i++)
{
ch = strExp[i];
if(ch == '(' || ch == '{' || ch == '[') //is opening parenthesis
stackA.Push(ch);
else //not opening
{
if(ch == ')')
{
if (stackA.IsEmpty() || stackA.Top() != '(') //if its closing , but the stack is empty or top not matching
return false;
else
stackA.Pop();
}
else if(ch == '}')
{
if (stackA.IsEmpty() || stackA.Top() != '{') //if its closing , but the stack is empty or top not matching
return false;
else
stackA.Pop();
}
else if(ch == ']')
{
if (stackA.IsEmpty() || stackA.Top() != '[') //if its closing , but the stack is empty or top not matching
return false;
else
stackA.Pop();
}
}
}
if(stackA.IsEmpty())
return true;
else
return false;
}
// end of driver file