Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Matching Parentheses in C language(not cpp nor c#) The following program is written by other chegg expert but it has bug. #include #include #include #define

Matching Parentheses in C language(not cpp nor c#) image text in transcribed

The following program is written by other chegg expert but it has bug.

#include

#include

#include

#define MAX_LENGTH 100

typedef struct {

char c;

int pos;

} Paren;

int is_match(char c1, char c2) {

return (c1 == '(' && c2 == ')') ||

(c1 == '[' && c2 == ']') ||

(c1 == '{' && c2 == '}');

}

int main(int argc, char *argv[]) {

if (argc != 3) {

printf("Usage: %s input_file output_file ", argv[0]);

return 1;

}

FILE *fin = fopen(argv[1], "r");

if (!fin) {

printf("Cannot open input file: %s ", argv[1]);

return 1;

}

FILE *fout = fopen(argv[2], "w");

if (!fout) {

printf("Cannot open output file: %s ", argv[2]);

return 1;

}

Paren stack[MAX_LENGTH];

int top = 0;

char buffer[MAX_LENGTH];

int pos = -1;

fgets(buffer, MAX_LENGTH, fin);

for (int i = 0; i

char c = buffer[i];

if (c == '(' || c == '[' || c == '{') {

stack[top].c = c;

stack[top].pos = i;

top++;

} else if (c == ')' || c == ']' || c == '}') {

top--;

if (top

pos = i;

break;

}

}

}

if (top == 0 && pos == -1) {

fprintf(fout, "There are %d balanced parentheses. ", (int)strlen(buffer) / 2);

} else {

fprintf(fout, "Error is spot at position %d at first. ", pos);

}

fclose(fin);

fclose(fout);

return 0;

}

Sample 2 has error The wrong output of the program is Error is spot at position -1 at first. which is wrong

Sample 1 Input: (())()

Sample 1 Output: There are 3 balanced parentheses.

Sample 2 Input: (())(

Sample 2 Output: Error is spot at position 5 at first.

Sample 3 Input: (())()()

Sample 3 Output: There are 4 balanced parentheses.

Sample 4 Input: [{]}

Sample 4 Output: Error is spot at position 2 at first.

Please use FILE*, fopen, fclose, fscanf and fprintf as both input and output are inside a .txt file. If you think it is inconvenient, you can use your way.

You are going to write a program to check whether the parentheses are balanced or not. For example, "( ( ) ) ( ) " is a balanced parentheses, while "( ( ) ) (" and "( ( ) ) )" are unbalanced. There are three types of parentheses, which are ( ), [] and \{\} . If the parentheses are balanced, you need to output the number of matching parentheses. If the parentheses are unbalanced, you need to output the first position (zero-based) that you spot error. Your program should read the input from the file, and output the answer to another file. The first argument is the input file name, while the second argument is the output file name. Name your program as "lab3-q3.c". Input file: A string contains ( ), [ ] and \{\} only. The length of input will not exceed 100 characters. Output file: If the parentheses are balanced, output "There are X balanced parentheses." If the parentheses are unbalanced, output "Error is spot at position Y at first." Y is a zero-based number. Sample 1 Input: (())() Sample 1 Output: There are 3 balanced parentheses. Sample 2 Input: (())( Sample 2 Output: Error is spot at position 5 at first

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

Understanding and Addressing Anxiety

Answered: 1 week ago