Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define size 80
- enum {nocomments, openedcomment, closedcomment};
- char openedbrackets[] = {'(','{','['};
- char closedbrackets[] = {')','}',']'};
- int stacksize = 0;
- struct stack{
- char data[size];
- int head;
- };
- void push(struct stack astack, char arg){
- astack.data[stacksize] = arg;
- }
- void pop(struct stack astack){
- astack.data[stacksize] = 0;
- }
- char peek(struct stack astack){
- return astack.data[stacksize - 1];
- }
- bool checkbrackets(char* astr){
- int cntopened = 0;
- for (int i = 0; astr[i] != '\0'; ++i){
- if (astr[i] == first) cntopened++;
- else if (astr[i] == second) cntopened--;
- //else if (astr[i]) == otherclose)
- if (cntopened < 0) break;
- }
- return (cntopened == 0);
- }
- int checkcomments(char* astr){
- int res = nocomments;
- for (int i = 0; astr[i] != '\0'; ++i){
- if ((astr[i] == '/') && (astr[i+1] == '*')){
- res = openedcomment;
- astr[i] = '(';
- astr[i+1] = '(';
- }
- if ((astr[i] == '*') && (astr[i+1] == '/')){
- if (res == openedcomment) res = nocomments; else res = closedcomment;
- astr[i] = ')';
- astr[i+1] = ')';
- }
- }
- return res;
- };
- bool checkescapesequence(char* astr){
- bool res = true;
- for (int i = 0; astr[i] != '\0'; ++i){
- if (astr[i] == '\\') {
- switch (astr[i + 1]) {
- case 'a': break;
- case 'b': break;
- case 'f': break;
- case 'n': break;
- case 'r': break;
- case 't': break;
- case 'v': break;
- case '\'': break;
- case '"': break;
- case '?': break;
- default:
- return false;
- }
- }
- }
- return res;
- }
- int main(){
- char* s = malloc(80*sizeof(char));
- FILE *f = fopen("input.txt","r");
- int commentscnt = 0;
- struct stack bracketstack;
- for (int i = 1; !feof(f); i++) {
- fscanf(f, "%s", s);
- bool res;
- res = checkbrackets(s,'(',')');
- printf(res ? "" : "Error: line %d brackets type () disbalance \n",i);
- if (res == 1) changebrackets(s,'(',')');
- res = checkbrackets(s,'{','}');
- printf(res ? "" : "Error: line %d brackets type {} disbalance \n",i);
- if (res == 1) changebrackets(s,'{','}');
- res = checkbrackets(s,'[',']');
- printf(res ? "" : "Error: line %d brackets type [] disbalance \n",i);
- if (res == 1) changebrackets(s,'[',']');
- res = checkbrackets(s,'"','"');
- printf(res ? "" : "Error: line %d brackets type \"\" disbalance \n",i);
- if (res == 1) changebrackets(s,'"','"');
- res = checkbrackets(s,'\'','\'');
- printf(res ? "" : "Error: line %d brackets type \'\' disbalance \n",i);
- if (res == 1) changebrackets(s,'\'','\'');
- res = checkescapesequence(s);
- printf(res ? "" : "Error: line %d incorrect escape sequence \n",i);
- switch (checkcomments(s)){
- case 1: commentscnt += 1; break;
- case 2: commentscnt -= 1; break;
- default: break;
- }
- }
- printf(commentscnt ? "Error: comments disbalance \n" : "");
- return 0;
- }
- //http://mech.math.msu.su/~shvetz/54/inf/perl-problems/chBalance_sIdeas.xhtml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement