c-平衡圆括号,堆栈不迭代到文本文件中的其他行



我正在读取一个文本文件,以确定该文件中的括号是否平衡(每个左括号都有一个闭合的括号(。代码正在运行,但它并没有准确地从文件中读取数据,也没有逐行读取。错误是什么
这是文本文件包含的数据
((
[]
[
]
{}

这是代码

#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);
}

以下建议的代码:

  1. 执行所需操作
  2. 干净地编译

现在,提出的代码:

#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( "nnecho 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: %cn", 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 !

最新更新