c - 打印带有不平衡括号的行数

时间:2017-10-19 13:35:44

标签: c

我正在编写一个解析括号程序,用于检查用c编写的文本文件是否具有不平衡的括号。现在我试图打印导致问题的线路数量但是卡住了。这是我的代码:

if(isPlaying){
    timeSlider.value += Time.deltaTime;
    timer = timeSlider.value;
    print(timeSlider.value);
    if((newTimes[0]>=(timer-0.1f)) && (newTimes[0]<=(timer+0.1f))){
          print("time: " + newTimes[0]+ " at run time: " + timer);
          NewTimes.RemoveAt(0);
    }
}

我正考虑为每个导入的行添加索引,并将所有字符串连接到一个表达式中。例如,转身 #include<stdio.h> #include<stdlib.h> #include<string.h> #define bool int /* structure of a stack node */ struct sNode { char data; struct sNode *next; }; /* Function to push an item to stack*/ void push(struct sNode** top_ref, int new_data); /* Function to pop an item from stack*/ int pop(struct sNode** top_ref); /* Returns 1 if character1 and character2 are matching left and right Parenthesis */ 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; /* Declare an empty character stack */ struct sNode *stack = NULL; /* Traverse the given expression to check matching parenthesis */ while (exp[i]) { /*If the exp[i] is a starting parenthesis then push it*/ if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[') push(&stack, exp[i]); /* If exp[i] is an ending parenthesis then pop from stack and check if the popped parenthesis is a matching pair*/ else if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') { /*If we see an ending parenthesis without a pair then return false*/ if (stack == NULL) { printf("\nError: Closing parenthesis is not matched\n"); return 0; } /* Pop the top element from stack, if it is not a pair parenthesis of character then there is a mismatch. This happens for expressions like {(}) */ else if ( !isMatchingPair(pop(&stack), exp[i]) ) { pop(&stack); return 0; } } i++; } /* If there is something left in expression then there is a starting parenthesis without a closing parenthesis */ if (stack == NULL) return 1; /*balanced*/ else { printf("\nError: Opening parenthesis not matched\n"); return 0; /*not balanced*/ } } /* UTILITY FUNCTIONS */ /*driver program to test above functions*/ int main(int argc, char* argv[]) { FILE* file = fopen("hw3.txt", "r"); /* should check the result */ char line[256]; while (fgets(line, sizeof(line), file)) { /* note that fgets don't strip the terminating \n, checking its presence would allow to handle lines longer that sizeof(line) */ areParenthesisBalanced(line); } /* may check feof here to make a difference between eof and io failure -- network timeout for instance */ fclose(file); /*char exp[100] = "{()}[]abc("; printf("%s", &exp); areParenthesisBalanced(exp);*/ return 0; } /* Function to push an item to stack*/ void push(struct sNode** top_ref, int new_data) { /* allocate node */ struct sNode* new_node = (struct sNode*) malloc(sizeof(struct sNode)); if (new_node == NULL) { printf("Stack overflow n"); getchar(); exit(0); } /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*top_ref); /* move the head to point to the new node */ (*top_ref) = new_node; } /* Function to pop an item from stack*/ int pop(struct sNode** top_ref) { char res; struct sNode *top; /*If stack is empty then error */ 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; } } 1 first line 2 second line 3 third line 因为1 first line 2 second line 3 third line无法在不同的行中匹配括号。

我尝试了几种方法,但没有使用它们。

任何帮助将不胜感激。提前谢谢。

--- --- EDIT

更具体地说,我的主要问题是如何检查整个文件并使用索引号打印错误行。

0 个答案:

没有答案