Modify your program created in the previous question so that it also looks for matching open and closing single AND double quotes. You may need to add an additional stack. Consider that matching quotes normally go inside any matching curly braces and parenthesis, but not the other way around. If curly braces or parentheses are found between matching quotes, consider them part of the string and NOT part of the programming code. Also, think about the differences between for what you use single and double quotes. Here is my code:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
 

Computer Science

Modify your program created in the previous question so that it also looks for matching open and closing single AND double quotes. You may need to add an additional stack. Consider that matching quotes normally go inside any matching curly braces and parenthesis, but not the other way around. If curly braces or parentheses are found between matching quotes, consider them part of the string and NOT part of the programming code. Also, think about the differences between for what you use single and double quotes.

Here is my code:

// A small demonstration program for a stack.
#include <iostream> // Provides cin, cout
#include <stack> // Provides stack
#include <string> // Provides string
#include <fstream> //Creates and/or read files
using namespace std;

// PROTOTYPE for a function used by this demonstration program
bool balanceSymbols(const string& expr);
// Postcondition: A true return value indicates that the parentheses in the
// given expression are balanced. Otherwise, the return value is false.

int main()
{
string userInput;

cout << "Enter File name:(Sample 1.txt or Sample 2.txt) to check if symbols are balanced\n";
getline(cin, userInput);
if (balanceSymbols(userInput))
cout << "Those parentheses are balanced.\n";
else
cout << "Those parentheses are not balanced.\n";
return 0;
}

bool balanceSymbols(const string& fileName)
// Library facilities used: stack, string
{
string expression; //to store current line
ifstream inFile(fileName);
stack<char> store; // Stack to store the left parentheses as they occur
bool flag = false; // Becomes true if a needed parenthesis is not found
while (getline(inFile,expression)) {

for (int i = 0; !flag && (i < expression.length()); ++i)
{

if (expression[i] == '(')
store.push(expression[i]);
else if (expression[i] == '{')
store.push(expression[i]);
else if ((expression[i] == ')')) {
// if right paranthesis encountered, match with left paranthesis at top
if (store.empty())
flag = true;
else if (store.top() == '(')
store.pop(); // Pops the corresponding left parenthesis.
else {
flag = true; // if the stack top has other type of paranthesis
}
}
else if ((expression[i] == '}')) {
// if right paranthesis encountered, match with left paranthesis at top
if (store.empty())
flag = true;
else if (store.top() == '{')
store.pop(); // Pops the corresponding left parenthesis.
else {
flag = true; // if the stack top has other type of paranthesis
}
}
}
}
inFile.close();
return (store.empty() && !flag);
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY