SHARE
TWEET

Untitled

a guest Nov 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int Pntr = 0;
  7. const char D1[4] = "([{";
  8. const char D2[4] = ")]}";
  9.  
  10. void Push (char *MassStack, char Ch ) {
  11.     MassStack[Pntr] = Ch;
  12.     Pntr++;
  13. }
  14.  
  15. char Pop (char *MassStack) {
  16.     Pntr--;
  17.     if (Pntr > -1) {
  18.         return (MassStack[Pntr]);
  19.     }
  20.     else {
  21.         return ('0');
  22.     }
  23. }
  24.  
  25. char OpenBracket(char Ch) {
  26.     switch (Ch) {
  27.         case (')'): return('(');
  28.         case (']'): return('[');
  29.         case ('}'): return ('{');
  30.         default: return ('0');
  31.     }
  32. }
  33.  
  34. bool Proc (char *StrSrc) {
  35.     int i = 0;
  36.     Pntr = 0;
  37.     char MassStack [100];
  38.     bool Balance = true;
  39.     while ((i < strlen(StrSrc)) && Balance) {
  40.         if (strchr(D1, StrSrc[i]) != NULL) {
  41.             Push(MassStack, StrSrc[i]);
  42.         }
  43.         else {
  44.             if (strchr(D2, StrSrc[i]) != NULL) {
  45.                 if (Pop(MassStack) != OpenBracket(StrSrc[i])) {
  46.                     Balance = false;
  47.                 }
  48.             }
  49.         }
  50.         i++;
  51.     }
  52.     return((Pntr == 0) && Balance);
  53. }
  54.  
  55. bool IsStrCorrect(char *StrSrc) {
  56.     const char D[7] = "([{)]}";
  57.     bool isCorrect = false;
  58.     for (int i = 0; i <  strlen(StrSrc); i++) {
  59.         if (strchr(D, StrSrc[i]) != NULL) {
  60.             isCorrect = true;
  61.         }
  62.     }
  63.     return (isCorrect);
  64. }
  65.  
  66. char *FileNameInputRead () {
  67.     bool isCorrect = true;
  68.     char *FileName;
  69.     do {
  70.         puts("Enter file name:");
  71.         FileName = (char*)malloc(255);
  72.         gets(FileName);
  73.         if (fopen(FileName, "r") == NULL) {
  74.             puts("This file does not exist!");
  75.         }
  76.         else{
  77.             isCorrect = false;
  78.         }
  79.     }
  80.     while (isCorrect);
  81.     return(FileName);
  82. }
  83.  
  84. void IsBracketCorrect() {
  85.     char *FileNameIn;
  86.     FileNameIn = FileNameInputRead();
  87.     FILE *FileIn = fopen(FileNameIn, "r");
  88.     char StrSrc[255];
  89.     while (fgets(StrSrc, 255, FileIn)) {
  90.         printf("%s", StrSrc);
  91.         if (IsStrCorrect(StrSrc)) {
  92.             if (Proc(StrSrc)) {
  93.                 puts("Brackets agreed.");
  94.             }
  95.             else {
  96.                 puts("Brackets are not agreed.");
  97.             }
  98.         }
  99.         else {
  100.             puts("There are no brackets in the line!");
  101.         }
  102.     }
  103. }
  104.  
  105. int main() {
  106.     printf("This program will check the balance of the brackets in the lines\n");
  107.     IsBracketCorrect();
  108.     return 0;
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top