



![and pair matching pair matching [ and ] pair matching ( and ) pair matching and pair matching { and } pair matching and p](http://img.homeworklib.com/questions/ae09b810-2bb7-11eb-a849-4f24ba889c81.png?x-oss-process=image/resize,w_560)




The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with.
program code to copy
main.cpp
#include <iostream>
#include <stack>
#include <fstream>
#include <string>
#include <iomanip>
#include <cassert>
#include <sstream>
using namespace std;
void printFile(istream &dictfile);
bool balanceFile(istream &dictfile);
bool Matching(char, char);
void getChar(char& c, istream&infile, int& i);
struct charAndLine
{
int line;
char openCharacter;
};
int main()
{
//checkings();
ifstream infile;
string filename;
cout << "Please enter filename for C++ code:
";
cin >> filename;
infile.open(filename.c_str());
if (!infile)
{
cout << "File not found!"
<< endl;
return (1);
}
printFile(infile);
if (balanceFile(infile))
cout << "balance ok" <<
endl;
else
cout << "Balance
baaaaadddddd";
}
void printFile(istream& infile)
{
int i = 1;
string a;
while (infile.peek() != EOF)
{
cout << i << " ";
getline(infile, a);
cout << setw(3) << a
<< endl;
i++;
}
infile.clear();
infile.seekg(0, infile.beg);
}
bool balanceFile(istream &infile)
{
stack <charAndLine> stack1;
char c, d, e, f;
bool match = true;
int numberOfLine = 1;
while (infile.peek() != EOF)
{
//infile.get(c);
getChar(c, infile,
numberOfLine);
if (c == '/'&&infile.peek()
== '*')
{
infile.get(c);
//this must be a *
int matchLine =
numberOfLine;
getChar(c,
infile, numberOfLine); //this is what we're checking
while ((c != '*'
|| infile.peek() != '/') && infile.peek() != EOF)
{
getChar(c, infile, numberOfLine);
}
if
(infile.peek() == EOF)
{
match = false;
cout << "unmatched /* symbol on line "
<< matchLine << endl;
break;
}
cout <<
"pair matching /* and */" << endl;
}
else
{
if (c ==
'/'&&infile.peek() == '/')
{
string newLine;
getline(infile, newLine);
}
}
if (c == '"' || c == '\'')
{
char save;
save = c;
getChar(c,
infile, numberOfLine);
while (c != save
&& infile.peek() != EOF)
{
getChar(c, infile, numberOfLine);
if (c == '\n')
{
match = false;
cout << "unmatched "
<< save << " symbol on line " << numberOfLine
<< endl;
break;
}
}
if (c != save
&& infile.peek() == EOF && infile.peek() !=
'\n')
{
match = false;
cout << "unmatched " << save
<< " symbol on line " << numberOfLine <<
endl;
break;
}
cout <<
"pair matching " << save << " and " << save
<< endl;
}
if (c == '(' || c == '{' || c ==
'[')
{
charAndLine
a;
a.line =
numberOfLine;
a.openCharacter
= c;
stack1.push(a);
}
else if (c == ')' || c == '}' || c
== ']')
{
if
(stack1.empty())
{
cout << "unmatched " << c << "
symbol on line " << numberOfLine << endl;
match = false;
break;
}
charAndLine
openChar;
openChar =
stack1.top();
stack1.pop();
if
(!Matching(openChar.openCharacter, c))
{
match = false;
cout << "unmatched " <<
openChar.openCharacter << " symbol on line " <<
openChar.line << endl;
break;
}
else
cout << "pair matching " <<
openChar.openCharacter << " and " << c <<
endl;
}
}
if (!stack1.empty() && match == true)
{
cout << "unmatched " <<
stack1.top().openCharacter << " symbol on line " <<
stack1.top().line << endl;
match = false;
}
return match;
}
bool Matching(char a, char b)
{
if (a == '{')
{
if (b == '}')
return
true;
else
return
false;
}
if (a == '(')
{
if (b == ')')
return
true;
else
return
false;
}
if (a == '[')
{
if (b == ']')
return
true;
else
return
false;
}
return false;
}
void getChar(char& c, istream&infile, int& i)
{
infile.get(c);
if (c == '\n')
i++;
}
=============================================================
bad_balance2.cpp
/*
bad_balance2.cpp
Test data for CS2 Project 4
Created: 24 October 1997
Modified: 3 November 2017
*/
#define FOO '"* /* */ foo'
#define BAR " /* ' "
int main(int argc, char * argv[])
{
char c = {'a';}
y = 3; '
z = 4;
x = y * z;
y = x / z;
cout << "Hello world\n"; // :)
}
================================================================================
sample output

![< 7 8 9 #define FOO /* */ foo 10 #define BAR 11 12 int main(int argc, char * argv[]) 13 14 char c = {a; } 15 y = 3; 16 z =](http://img.homeworklib.com/questions/1a44e6c0-2bb8-11eb-848f-8761fdb23941.png?x-oss-process=image/resize,w_560)
The last 3 cases are tests .cpp files to test the code. The language of this...
1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
GIVEN CODE. PLEASE FILL IN THE
BLANK.
// Include Block
#include <fcntl.h>
#include <unistd.h>
// Preprocessor declarations
#define BUF_SIZE 10 /* global constant buffer size: Generally this
would be much larger, but I want to show you that it works in a
loop until the entire file is read.*/
// main function
int main(int argc, char *argv[])
{
// Variable declarations
int fd;
// file descripter
char buffer[BUF_SIZE];
// string for...
I need only one C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...
Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE 0644 main(int argC, char* argv[]) { int srcFd; int dstFd; int charCnt; char buf[BUFFERSIZE]; /*Check args*/ if( argC!=3 ){ fprintf( stderr, "usage: %s source destination\n", argv[0]); exit(1); } /*Open the files*/ srcFd= open(argv[1],O_RDONLY); if( srcFd==-1 ){ fprintf(stderr,"Cannot open %s \n", argv[1]); } dstFd= creat(argv[2],COPYMODE); ...
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
Hi, need this question ansered in c++, has multiple levels will
post again if you can complete every level so keep an eye out for
that.
here is a sketch of the program from the screenshot
int main (int argc, char** argv) { enum { total, unique } mode =
total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) {
switch(c) { case 't': mode = total; break; case 'u': mode = unique;
break; } } argc -=...
Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...
I need only one C++ function . It's C++ don't
write any other language.
Hello I need unzip function here is my zip function
below. So I need the opposite function unzip.
Instructions:
The next tools you will build come in a pair, because
one (zip) is a file compression tool, and
the other (unzip) is a file decompression
tool.
The type of compression used here is a simple form of
compression called run-length encoding (RLE). RLE is quite simple:
when...