In: Computer Science
C programming
Program must use FileIO to read in a file formatted like below and print it to the screen;
#include<stdio.h>
#include<stdio.h>
int main)int argc, char * argv[])
{
printf("hello\n");
return 0;
}
it then must check if there are balanced Parentheses and balanced brackets.
If it is balanced it must display "Code is balanced" and if its not balanced it must display "code is not balanced"
Code for Parentheses and balanced brackets
#include <stdio.h>
#include <stdlib.h>
#define bool int
struct sNode
{
char data;
struct sNode* next;
};
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
bool isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
// Return 1 if expression has balanced Parenthesis
bool areParenthesisBalanced(char exp[])
{
int i = 0;
struct sNode* stack = NULL;
while (exp[i])
{
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);
if (exp[i] == '}' || exp[i] == ')'
|| exp[i] == ']')
{
if (stack == NULL)
return 0;
else if (!isMatchingPair(pop(&stack), exp[i]))
return 0;
}
i++;
}
if (stack == NULL)
return 1; //balanced
else
return 0; //not balanced
}
int main()
{
char exp[100] = "{()}[]"; //Declaration for paranthesis check
if (areParenthesisBalanced(exp))
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
void push(struct sNode** top_ref, int new_data)
{
struct sNode* new_node
= (struct sNode*)malloc(sizeof(struct sNode));
if (new_node == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
char res;
struct sNode* top;
if (*top_ref == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
SAMPLE INPUT - "{()}[]"
SAMPLE OUTPUT - balanced