我正在阅读一个文本文件,以确定该文件中的括号是否平衡(对于每个打开的括号,都有一个封闭的括号)。该代码正在运行,但不能完全从文件中准确读取数据,也不能逐行读取。有什么问题吗?
这是文本文件包含的数据
()
[]
[
]
{}
{
这是代码
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_SIZE 1000
struct Stack{
int top;
char arr[MAX_SIZE];
} st;
void init(){
st.top = -1;
}
bool isEmpty(){
if(st.top == -1){
return true;
}else{
return false;
}
}
bool isFull(){
if(st.top == MAX_SIZE-1){
return true;
}else{
return false;
}
}
void push(char item){
if(isFull()){
printf("Stack is full");
}else{
st.top++;
st.arr[st.top] = item;
}
}
void pop(){
if(isEmpty()){
printf("Stack is empty");
}else{
st.top--;
}
}
char gettop(){
return st.arr[st.top];
}
bool ArePair(char opening,char closing)
{
if(opening == '(' && closing == ')') return true;
else if(opening == '{' && closing == '}') return true;
else if(opening == '[' && closing == ']') return true;
return false;
}
void main()
{
int length=0; //,i,j;
init();
int i;
char output[MAX_SIZE];
FILE * filepointer;
filepointer = fopen("ajay1.txt", "r");
if(filepointer == NULL)
{
printf("No File Found");
return 1;
}
for(i=0; fgets(output, sizeof(output), filepointer) !=NULL; i++)
{
//fclose(filepointer);
// init();
//printf("Enter an expression to check:");
//gets(output);
length = strlen(output);
for(i=0;i<length;i++){
if(output[i] == '(' || output[i] == '{' || output[i] == '['){
push(output[i]);
}else if(output[i] == ')' || output[i] == '}' || output[i] == ']'){
char a = gettop();
printf("%c",a);
if(isEmpty() || !ArePair(gettop(),output[i])){
printf("\nResult - Invalid expression - Not a Balanced one !");
return 0;
}else{
pop();
}
}
}
if(isEmpty()){
printf("\nResult - Valid expression - Perfectly Balanced !");
}else{
printf("\nResult - Invalid expression - Not a Balanced one !");
}
}
fclose(filepointer);
}
答案 0 :(得分:0)
以下建议的代码:
现在,建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//#include <string.h>
#define MAX_SIZE 1000
struct Stack
{
int top;
char arr[MAX_SIZE];
};
struct Stack st;
void init()
{
st.top = -1;
}
bool isEmpty()
{
if(st.top == -1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(st.top == MAX_SIZE-1)
{
return true;
}
else
{
return false;
}
}
void push(char item)
{
if( isFull() )
{
puts("Stack is full");
}
else
{
st.top++;
st.arr[st.top] = item;
}
}
void pop()
{
if( isEmpty() )
{
puts( "Stack is empty" );
}
else
{
st.top--;
}
}
char gettop()
{
return st.arr[st.top];
}
bool ArePair(char opening,char closing)
{
if( opening == '(' && closing == ')')
return true;
else if( opening == '{' && closing == '}')
return true;
else if( opening == '[' && closing == ']')
return true;
return false;
}
int main( void )
{
init();
char output[MAX_SIZE];
FILE * filepointer;
filepointer = fopen("ajay1.txt", "r");
if(filepointer == NULL)
{
perror("fopen failed");
exit( EXIT_FAILURE );
}
while( fgets(output, sizeof(output), filepointer) )
{
puts( "\n\necho of line read: " );
puts( output );
for( size_t i=0; output[i]; i++ )
{
if( output[i] == '\n' )
{
puts( "finished with current line" );
continue;
}
printf( "Current char under test: %c\n", output[i] );
if(output[i] == '(' || output[i] == '{' || output[i] == '[')
{
push(output[i]);
continue;
}
if(output[i] == ')' || output[i] == '}' || output[i] == ']')
{
if( isEmpty() )
{
puts( "unbalanced pair" );
continue;
}
if( !ArePair( gettop(), output[i]) )
{
puts( "pair not balanced" );
continue;
}
else
{
puts( "pair matched" );
}
pop();
}
}
}
if(isEmpty())
{
puts("\nResult - Valid expression - Perfectly Balanced !");
}
else
{
puts("\nResult - Invalid expression - Not a Balanced one !");
}
fclose(filepointer);
}
给出发布的文件内容,这是输出:
echo of line read:
()
Current char under test: (
Current char under test: )
pair matched
finished with current line
echo of line read:
[]
Current char under test: [
Current char under test: ]
pair matched
finished with current line
echo of line read:
[
Current char under test: [
finished with current line
echo of line read:
]
Current char under test: ]
pair matched
finished with current line
echo of line read:
{}
Current char under test: {
Current char under test: }
pair matched
finished with current line
echo of line read:
{
Current char under test: {
finished with current line
Result - Invalid expression - Not a Balanced one !