Need to write a program that asks the user to submit file. The program will then
ID: 3677505 • Letter: N
Question
Need to write a program that asks the user to submit file. The program will then make sure all starting brackets (i.e {([])} ) have a matching end bracket in that given file. If it doesn't the program will then let the user know what type of bracket it needs to be looked at and what row and column the error is.It also SHOULDN'T account for brackets that are in comments or in quotations. need to create 2 dementional array and pass file user submits through bracketcheck function.
was given main
int main(){
string filename;
if (argc >= 2) {
filename = argv[1]; // command line arguments are given. The first argument is the filename
}
else {
cout << "Enter name of file to check: ";
cin >> filename;
}
cout << "File to check:" << filename << endl;
ifstream infile(filename);
if (!infile) {
cout << "That file does not exist" << endl;
return (-1); // end the program
}
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
system("pause");
return(0);
}
Introduction
Most programming languages require that brackets (round brackets, square brackets, curly braces) are "balanced" and "nested." This means that every opening bracket should have a matching closing bracket of the same type and that two matching pairs should completely contain one inside the other. Thus, {()} and [() ()] are examples of nested brackets, while {(}) and (())) are not nested.
These rules hold for C++ too. However, brackets that appear within comments and quoted strings do not count towards nesting. Thus, these are valid:
cout << "Sum of [4, 3, ... = " << (4 + (3 + (2 + 1))) << endl;
The problem
Write a program that takes as its input a text file that contains some C++ code. Your program should determine if all brackets within the text file are nested with the exception of those appearing within comments and quoted strings. When your program detects a mismatched pair, it should print a message indicating the location (line numbers and column numbers) of the mismatch. If all pairs are matched, the program should indicate that at the end.
Data structures
You can write your own implementation of a data structure, use an implementation developed in class, or a container from the C++ Standard Library
File input/output
Your program should read the input text from a file. The skeleton program that is included here has code to read a text file line by line. This code can take the file name from the command line or ask the user. Note that code running within Visual Studio considers the project directory as the default directory in which to open files.
Simplifying assumptions
Only round brackets (), square brackets [], and curly braces {} need to be matched.
The maximum depth of nesting is 80, i.e., there could be at most 80 pairs contained one within the other (note that there is no limit to the total number of brackets).
Comments will be indicated only with // (not with /* ... */).
Strings within quotes will be confined to one line.
Skeleton code and examples
You have been supplied with a brackets_check.cpp C++ source file. The file is complete and should compile cleanly. This file has code to read a text file and can be used as a starting point for your code.
You are also provided with a few example text files. The appropriate outputs for these inputs are:
Output for example1.cpp
Output for example2.cpp
Output for example3.cpp
Output for example4.cpp
File to check:example4.cpp
Explanation / Answer
Answer for Question:
This below c++ code may help you to given problem statement.
i.e balanced parenthesis code
#include<iostream>
#include<stack>
#include<string>
using namespace std;
bool ArePair(char opening,char closing)
{
if(opening == '(' && closing == ')') return true;
else if(opening == '{' && closing == '}') return true;
else if(opening == '[' && closing == ']') return true;
return false;
}
bool AreParanthesesBalanced(string exp)
{
stack<char> S;
for(int i =0;i<exp.length();i++)
{
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
S.push(exp[i]);
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(S.empty() || !ArePair(S.top(),exp[i]))
return false;
else
S.pop();
}
}
return S.empty() ? true:false;
}
int main()
{
/*Code to test the function AreParanthesesBalanced*/
string expression;
cout<<"Enter an expression: "; // input expression from STDIN/Console
cin>>expression;
if(AreParanthesesBalanced(expression))
cout<<"Balanced ";
else
cout<<"Not Balanced ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.