读取字符串时scanf和fgets函数是否以类似方式工作

我需要将中缀表达式转换为后缀表达式,然后评估结果后缀表达式。当我使用scanf从用户读取中缀表达式时,代码可以通过打印后缀表达式和结果后缀表达式的值来正常工作,但是当我使用fgets时,它会打印出堆栈溢出。输入:1 + 3 * 5 /(7-4),预期输出为135 * 74-/ +和6。

我已附上以下代码。

Stack.h - (FILE-NAME)

#ifndef STACK_H
#define STACK_H

typedef char stack_Element;
typedef struct {
    stack_Element *contents;
    int top;
    int max_size;
}stackT;

int stack_init(stackT *stackp, int max_size);
void stack_destroy(stackT *stackp);
int stack_push(stackT *stackp, stack_Element element);
int stack_pop(stackT *stackp, stack_Element *element);
int stack_full(stackT *stackp);
int stack_empty(stackT *stackp);

#endif
Stack.c (FILE-NAME)
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"

int stack_init(stackT *stackp, int max_size)
{
    stack_Element *new_content;

    new_content = (stack_Element *)malloc(sizeof(stack_Element) * max_size);
    if (new_content == NULL)
        return -1;
    stackp->contents = new_content;
    stackp->max_size = max_size;
    stackp->top = -1;
    return 2;
}

void stack_destroy(stackT *stackp)
{
    free(stackp->contents);
    stackp->contents = NULL;
    stackp->max_size = 0;
    stackp->top = -1;
}

int stack_full(stackT *stackp)
{
    return stackp->top >= stackp->max_size - 1;
}

int stack_empty(stackT *stackp)
{
    return stackp->top < 0;
}

int stack_push(stackT *stackp, stack_Element element)
{
    if(stack_full(stackp))
        return -1;
    stackp->contents[++stackp->top] = element;
    return 0;
}

int stack_pop(stackT *stackp, stack_Element *element)
{
    if (stack_empty(stackp))
        return -1;
    *(element) = stackp->contents[stackp->top--];
    return 0;
}
infix_postfix.h (FILE-NAME)

#ifndef INFIX_TO_POSTFIX_H
#define INFIX_TO_POSTFIX_H
#include"stack.h"
#define size 50

int infix_to_postfix(char *infix, char *postfix);
int priority(char operator);
int perform_the_conversion(char *infix, char *postfix, stackT *operand_stack);

#endif
infix_to_postfix.c (FILE-NAME)

#include"infix_to_postfix.h"
#include"stack.h"

int infix_to_postfix(char *infix, char *postfix)
{
    stackT operand_stack;
    int ret_stack_init = 0;
    int ret_val = 0;

    ret_stack_init = stack_init((&operand_stack), 100);
    if (ret_stack_init == -1)
        return ret_stack_init;
    ret_val = perform_the_conversion(infix, postfix, (&operand_stack));
    stack_destroy(&operand_stack);
    return ret_val;
}

int priority(char operator)
{
    if (operator == '+' || operator == '-')
        return 1;
    else if (operator == '*' || operator == '/')
        return 2;
    else
        return 0;
}

int perform_the_conversion(char *infix, char *postfix, stackT *operand_stack)
{
    int i = 0;
    int count = 0;
    int ret_val = 0;
    char element;

    for (i = 0; infix[i]; i++) {
        if (infix[i] >= '0' && infix[i] <= '9') {
            postfix[count++] = infix[i];
        } else if (infix[i] == '(') {
            ret_val = stack_push(operand_stack, infix[i]);
            if (ret_val == -1)
                return 0;
        } else if (infix[i] == ')') {
            while (1) {
                ret_val = stack_pop(operand_stack, &element);
                if (ret_val == -1)
                    return 1;
                if (element == '(')
                    break;
                postfix[count++] = element;
            }
        } else if (priority(infix[i]) >
            priority(operand_stack->contents[operand_stack->top])) {
            ret_val = stack_push(operand_stack, infix[i]);
            if (ret_val == -1)
                return 0;
        } else {
            while (priority(operand_stack->contents[operand_stack->top])
                 >= priority(infix[i])) {
                ret_val = stack_pop(operand_stack, &element);
                if (ret_val == -1)
                    return 1;
                postfix[count++] = element;
            }
            ret_val = stack_push(operand_stack, infix[i]);
            if (ret_val == -1)
                return 0;
        }
    }
    while (operand_stack->top != -1) {
        ret_val = stack_pop(operand_stack, &element);
        if (ret_val == -1)
            return 1;
        postfix[count++] = element;
    }
    postfix[count++] = '\0';
    return 2;
}
postfix_evaluate.h (FILE-NAME)

#ifndef POSTFIX_EVALAUATE_H
#define POSTFIX_EVALUATE_H
#include"stack.h"

#include<stdio.h>
int postfix_evaluate(char *postfix, int *result);
int perform_postfix_evaluate(stackT *operand_stack, char *postfix, int *result);

#endif
postfix_evaluate.c (FILE-NAME)

#include"postfix_evaluate.h"
#include"stack.h"

int postfix_evaluate(char *postfix, int *result)
{
    stackT operand_stack;
    int ret_val = 0;

    ret_val = stack_init(&operand_stack, 100);
    if (ret_val == -1)
        return ret_val;
    ret_val = perform_postfix_evaluate((&operand_stack), postfix, result);
    stack_destroy(&operand_stack);
    return ret_val;
}

int perform_postfix_evaluate(stackT *operand_stack, char *postfix, int *result)
{
    int i = 0;
    char a = 0;
    char b = 0;
    int c = 0;
    int ret_val = 0;

    for (i = 0; postfix[i]; i++) {
        if (postfix[i] >= '0' && postfix[i] <= '9') {
            postfix[i] = postfix[i] - '0';
            ret_val = stack_push(operand_stack, postfix[i]);
            if (ret_val == -1)
                return 0;
        } else {
            ret_val = stack_pop(operand_stack, &a);
            if (ret_val == -1)
                return 1;
            ret_val = stack_pop(operand_stack, &b);
            if (ret_val == -1)
                return 1;
            switch (postfix[i]) {
            case '+':
                c = b + a;
                break;
            case '-':
                c = b - a;
                break;
            case '*':
                c = b * a;
                break;
            case '/':
                c = b / a;
                break;
            default:
                break;
            }
            ret_val = stack_push(operand_stack, c);
            if (ret_val == -1)
                return 0;
        }
    }
    *result = operand_stack->contents[operand_stack->top];
    return 2;
}

main.c (FILE-NAME)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"infix_to_postfix.h"
#include"postfix_evaluate.h"

int main(void)
{
    char infix[size];
    char postfix[size];
    int result = 0;
    int ret_val = 0;
    int i = 0;

    printf("Enter the infix expression\n");
    fgets(infix, sizeof(infix), stdin); 
    ret_val = infix_to_postfix(infix, postfix);
    if (ret_val == -1) {
        printf("Memory not available in heap\n");
        return 0;
    } else if (ret_val == 0) {
        printf("The stack is over-flow\n");
        return 0;
    } else if (ret_val == 1) {
        printf("The stack is under-flow\n");
        return 0;
    }
    printf("%s", postfix);
    ret_val = postfix_evaluate(postfix, &result);
    if (ret_val == -1) {
        printf("Memory not available in heap\n");
        return 0;
    } else if (ret_val == 0) {
        printf("The stack is over-flow\n");
        return 0;
    } else if (ret_val == 1) {
        printf("The stack is under-flow\n");
        return 0;
    }
    printf("\n%d\n", result);
}

评论