Please help me complete the following in C (NOT C++). Please also utilize a STAC
ID: 3851797 • Letter: P
Question
Please help me complete the following in C (NOT C++). Please also utilize a STACK functionality in completing the code, utilizing preferably only stdlib, stdio, string, (and bool only if necessary). If you are unable to do that kindly leave this answer to someone who is able to. I would appreciate it if you would design the program so that it may read a text file as suggested. Also, see the below suggestion from the problem:
One way to approach the problem is to read in a character, if it is a left marker then push it onto the stack. If it is a right marker then check the top of the stack (if it exists), if it is the correct left marker then pop it out and continue testing the string. If you encounter a right marker and the top of the stack is not the left hand version of the marker you just picked up or the stack is empty then you can determine that the string is not correct, clear the stack and go on to the next example. If you finish a whole string (encounter a new line) without encountering a problem then you can determine that the input string is correct.
You are given a string consisting of parenthesis style characters[ ] and { }. A string of this tvpe is said to be correct if (a) It is an empty string (b) If string A is correct and string B is correct then string AB is correct. (c) If A is correct then (A), [A] and (A] are all correct. You are to write a program that takes as input from the keyboard a series of strings of this type that can be of any length. The first line of input will contain a single integer n telling vou how many strings you will be testing. Following that integer will be n lines (each ending in a new line character) that represent the strings you are supposed to test with one string per line Your program should output Yes if a given string is correct and No otherwise For example if the user types the following as input Then your program should give the following output Yes No Yes You will note that if you do your program correctly there is no prompt for data and the yes and no answers will be interspersed with your input data when you enter it manually. The following would be a more accurate representation of what you see on the screen when you cut and paste the input into your program. Yes Yes You may find it useful to use a text file for testing your code so that you do not have to keep retyping the tests. You can do this from the command line by typing the name of the executable file followed by a less than sign and the name of your file. For example, if my executable is named day7 then I could type day7Explanation / Answer
Here is the code with output . Please post a comment if any issues. If happy with the answer, request you to rate it. Thank you very much.
You can run the program in normal way where user enter input lines OR using file redirection.
1. First compile the program.
gcc markers.c
2. To run normal , type ./a.out and press enter
3. To run using file redirection, just type all your inputs, like the number of lines , and the different inputs into a file and simply use the following command
./a.out < input.txt
#include <stdio.h>
#include <stdlib.h>
int main()
{
char stack[256]; //a stack used for storing markers
int top = -1;
//declare the markers
char ROUND_LEFT = '(', ROUND_RIGHT = ')';
char SQUARE_LEFT = '[', SQUARE_RIGHT = ']';
char CURLY_LEFT = '{', CURLY_RIGHT = '}';
int n, i;
char ch;
int correct;
printf(" Enter number of input lines: ");
scanf("%d", &n);
getchar();//flush the newline
printf(" ");
for(i = 1; i <= n; i++)
{
correct = 1; //assume input line is correct
top = -1; //reset stack top to -1
while(1)
{
ch = getchar(); //read 1 character at a time
if(correct) //as long as the input is still correct , check for marker cases
{
//if its any of the left markers, simply push it onto stack
if(ch == ROUND_LEFT || ch == SQUARE_LEFT || ch == CURLY_LEFT)
stack[++top] = ch;
else if(ch == ROUND_RIGHT) //if its ), then check if stack is not empty and stack top is (, if not input is not correct
{
if(top >= 0 && stack[top] == ROUND_LEFT)
top --;
else
correct = 0;
}
else if(ch == CURLY_RIGHT)//if its {, then check if stack is not empty and stack top is {, if not input is not correct
{
if(top >= 0 && stack[top] == CURLY_LEFT)
top --;
else
correct = 0;
}
else if(ch == SQUARE_RIGHT)//if its ], then check if stack is not empty and stack top is [, if not input is not correct
{
if(top >= 0 && stack[top] == SQUARE_LEFT)
top --;
else
correct = 0;
}
}
//found a newline, it means end of the current input line. Prints it validation status
if(ch == ' ')
{
if(correct && top==-1)
printf("Yes ");
else
printf("No ");
break;
}
}
}
}
sample input file input.txt
3
([])
(([{}])))
([()[]()]) ()
output
$ gcc markers.c
$ ./a.out
Enter number of input lines: 3
([])
Yes
(([{}])))
No
([()[]()]) ()
Yes
$ ./a.out < input.txt
Enter number of input lines:
Yes
No
Yes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.