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
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...
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...
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...
The provided code is my solution, stripped of the details needed to make it work. It is not a “good” program. It lives in a single file, does not use classes, and it has those evil global variables. That is by design. I want to to craft code. Use my code as a guide to help you put together the needed parts. #include #include #include // defaults const int MAX_STEPS = 100; // how long do we run the simulation...
/// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...
How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...