Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdlib.h>
- int Pntr = 0;
- const char D1[4] = "([{";
- const char D2[4] = ")]}";
- void Push (char *MassStack, char Ch ) {
- MassStack[Pntr] = Ch;
- Pntr++;
- }
- char Pop (char *MassStack) {
- Pntr--;
- if (Pntr > -1) {
- return (MassStack[Pntr]);
- }
- else {
- return ('0');
- }
- }
- char OpenBracket(char Ch) {
- switch (Ch) {
- case (')'): return('(');
- case (']'): return('[');
- case ('}'): return ('{');
- default: return ('0');
- }
- }
- bool Proc (char *StrSrc) {
- int i = 0;
- Pntr = 0;
- char MassStack [100];
- bool Balance = true;
- while ((i < strlen(StrSrc)) && Balance) {
- if (strchr(D1, StrSrc[i]) != NULL) {
- Push(MassStack, StrSrc[i]);
- }
- else {
- if (strchr(D2, StrSrc[i]) != NULL) {
- if (Pop(MassStack) != OpenBracket(StrSrc[i])) {
- Balance = false;
- }
- }
- }
- i++;
- }
- return((Pntr == 0) && Balance);
- }
- bool IsStrCorrect(char *StrSrc) {
- const char D[7] = "([{)]}";
- bool isCorrect = false;
- for (int i = 0; i < strlen(StrSrc); i++) {
- if (strchr(D, StrSrc[i]) != NULL) {
- isCorrect = true;
- }
- }
- return (isCorrect);
- }
- char *FileNameInputRead () {
- bool isCorrect = true;
- char *FileName;
- do {
- puts("Enter file name:");
- FileName = (char*)malloc(255);
- gets(FileName);
- if (fopen(FileName, "r") == NULL) {
- puts("This file does not exist!");
- }
- else{
- isCorrect = false;
- }
- }
- while (isCorrect);
- return(FileName);
- }
- void IsBracketCorrect() {
- char *FileNameIn;
- FileNameIn = FileNameInputRead();
- FILE *FileIn = fopen(FileNameIn, "r");
- char StrSrc[255];
- while (fgets(StrSrc, 255, FileIn)) {
- printf("%s", StrSrc);
- if (IsStrCorrect(StrSrc)) {
- if (Proc(StrSrc)) {
- puts("Brackets agreed.");
- }
- else {
- puts("Brackets are not agreed.");
- }
- }
- else {
- puts("There are no brackets in the line!");
- }
- }
- }
- int main() {
- printf("This program will check the balance of the brackets in the lines\n");
- IsBracketCorrect();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement