gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:26:30 25 4
gpt4 key购买 nike

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

这是代码

#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( "\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 !

关于c - 平衡括号,堆栈不迭代到文本文件中的其他行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53038058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com