Advertisement
boriswinner

gukddydj

Feb 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define size 80
  5.  
  6. enum {nocomments, openedcomment, closedcomment};
  7. char openedbrackets[] = {'(','{','['};
  8. char closedbrackets[] = {')','}',']'};
  9.  
  10. int stacksize = 0;
  11.  
  12. struct stack{
  13.     char data[size];
  14.     int head;
  15. };
  16.  
  17. void push(struct stack astack, char arg){
  18.     astack.data[stacksize] = arg;
  19. }
  20.  
  21. void pop(struct stack astack){
  22.     astack.data[stacksize] = 0;
  23. }
  24.  
  25. char peek(struct stack astack){
  26.     return astack.data[stacksize - 1];
  27. }
  28.  
  29. bool checkbrackets(char* astr){
  30.     int cntopened = 0;
  31.     for (int i = 0; astr[i] != '\0'; ++i){
  32.         if (astr[i] == first) cntopened++;
  33.         else if (astr[i] == second) cntopened--;
  34.         //else if (astr[i]) == otherclose)
  35.         if (cntopened < 0) break;
  36.     }
  37.     return (cntopened == 0);
  38. }
  39.  
  40. int checkcomments(char* astr){
  41.     int res = nocomments;
  42.     for (int i = 0; astr[i] != '\0'; ++i){
  43.         if ((astr[i] == '/') && (astr[i+1] == '*')){
  44.             res = openedcomment;
  45.             astr[i] = '(';
  46.             astr[i+1] = '(';
  47.         }
  48.         if ((astr[i] == '*') && (astr[i+1] == '/')){
  49.             if (res == openedcomment) res = nocomments; else res = closedcomment;
  50.             astr[i] = ')';
  51.             astr[i+1] = ')';
  52.         }
  53.     }
  54.     return res;
  55. };
  56.  
  57. bool checkescapesequence(char* astr){
  58.     bool res = true;
  59.     for (int i = 0; astr[i] != '\0'; ++i){
  60.         if (astr[i] == '\\') {
  61.             switch (astr[i + 1]) {
  62.                 case 'a': break;
  63.                 case 'b': break;
  64.                 case 'f': break;
  65.                 case 'n': break;
  66.                 case 'r': break;
  67.                 case 't': break;
  68.                 case 'v': break;
  69.                 case '\'': break;
  70.                 case '"': break;
  71.                 case '?': break;
  72.                 default:
  73.                     return false;
  74.             }
  75.         }
  76.     }
  77.     return res;
  78. }
  79.  
  80. int main(){
  81.     char* s = malloc(80*sizeof(char));
  82.     FILE *f = fopen("input.txt","r");
  83.     int commentscnt = 0;
  84.     struct stack bracketstack;
  85.  
  86.     for (int i  = 1; !feof(f); i++) {
  87.         fscanf(f, "%s", s);
  88.         bool res;
  89.         res = checkbrackets(s,'(',')');
  90.         printf(res ? "" : "Error: line %d brackets type () disbalance \n",i);
  91.         if (res == 1) changebrackets(s,'(',')');
  92.  
  93.         res = checkbrackets(s,'{','}');
  94.         printf(res ? "" : "Error: line %d brackets type {} disbalance \n",i);
  95.         if (res == 1) changebrackets(s,'{','}');
  96.  
  97.         res = checkbrackets(s,'[',']');
  98.         printf(res ? "" : "Error: line %d brackets type [] disbalance \n",i);
  99.         if (res == 1) changebrackets(s,'[',']');
  100.  
  101.         res = checkbrackets(s,'"','"');
  102.         printf(res ? "" : "Error: line %d brackets type \"\" disbalance \n",i);
  103.         if (res == 1) changebrackets(s,'"','"');
  104.  
  105.         res = checkbrackets(s,'\'','\'');
  106.         printf(res ? "" : "Error: line %d brackets type \'\' disbalance \n",i);
  107.         if (res == 1) changebrackets(s,'\'','\'');
  108.  
  109.         res = checkescapesequence(s);
  110.         printf(res ? "" : "Error: line %d incorrect escape sequence \n",i);
  111.  
  112.         switch (checkcomments(s)){
  113.             case 1: commentscnt += 1; break;
  114.             case 2: commentscnt -= 1; break;
  115.             default: break;
  116.         }
  117.     }
  118.     printf(commentscnt ? "Error: comments disbalance \n" : "");
  119.     return 0;
  120. }
  121.  
  122.  
  123. //http://mech.math.msu.su/~shvetz/54/inf/perl-problems/chBalance_sIdeas.xhtml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement