中缀到后缀函数错误:控件可能达到非空函数的结束

时间:2022-06-28 09:48:12

/* This is a code that changes infix notation to postfix notation. I used FILE/IO to get infix notations and the infix.txt file looks like

/ *这是将中缀表示法更改为后缀表示法的代码。我用FILE / IO来获取中缀符号,infix.txt文件看起来像

 3   
 2+4*2-1;  
 9+3^2^(3-1)*2;   
 2*((7-2)/3+4)^2%3;

My question is I get error saying "control may reach end of non-void function" on the last two functions, icp and isp. How can I fix this? */

我的问题是我在最后两个函数icp和isp上得到错误说“控制可能达到非空函数的结束”。我怎样才能解决这个问题? * /

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>



#define MAX_SIZE 100
#define ENTER 0x000d

void infixtopostfix(char expression[]);
char get_token(char expression[], int *index);
void push(int *top, char token);
int pop(int *top);
int icp(char op);
int isp(char op);
char stack[MAX_SIZE];



int main(void) {
    int i, num;
    FILE *file;
    char expression[MAX_SIZE];
    if((file=fopen("infix.txt","r")) == NULL) {
        printf("No file.\n");
    }
    fgets(expression, MAX_SIZE, file);
    num = atoi(expression);
    for(i=0; i < num; i++) {
        fgets(expression, MAX_SIZE, file);
        printf("%s", expression);
        infixtopostfix(expression);
    }
    fclose(file);
}



void infixtopostfix(char expression[]) {
    char token;
    char element;
    int top=0;
    int index=0;

    for(token=get_token(expression, &index); token!=';'; token=get_token(expression, &index)) {
        if(isdigit(token)) printf("%c", token);
        else if(token == ')') {
            while (stack[top] != '(') {
                element = pop(&top);
                printf("%c", element);
            }
            pop(&top);
        }
        else {
            while (isp(stack[top])>=icp(token)) {
                element = pop(&top);
                printf("%c", element);
            }
            push(&top, token);
        }
    }
    while((token=pop(&top))!=0) printf("%c", token);
    printf("\n");
}



char get_token(char expression[], int *index) {
    char token=expression[(*index)++];
    return token;
}



void push(int *top, char data) {
    if(*top < (MAX_SIZE-1)) stack[++(*top)] = data;
}



int pop(int *top) {
    if(*top > -1) return stack[(*top)--];
    else return 0;
}



int icp(char op) {
    switch (op){
        case '(' : return 20; break;
        case '+' : return 12; break;
        case '-' : return 12; break;
        case '*' : return 13; break;
        case '%' : return 13; break;
        case ';' : return 0;
    }
}



int isp(char op) {
    switch (op){
        case '(' : return 0; break;
        case '+' : return 12; break;
        case '-' : return 12; break;
        case '*' : return 13; break;
        case '%' : return 13; break;
        case ';' : return 0;
    }
}

2 个解决方案

#1


2  

While you may believe that your methods will only ever get one of these 6 characters, the compiler has to assume that any character can be passed in. At the moment, these methods will not return anything if you pass in, for example, 'x'.

虽然您可能认为您的方法只能获得这6个字符中的一个,但编译器必须假定可以传入任何字符。目前,如果传入,这些方法将不会返回任何内容,例如,'x ”。

You need to specify a default action for all characters not otherwise covered by your switch. Whether this is returning a default value or throwing an exception, that is up to you. But it's nonetheless a possibility your code needs to handle.

您需要为交换机未覆盖的所有字符指定默认操作。无论是返回默认值还是抛出异常,都取决于您。但它仍然是您的代码需要处理的可能性。

#2


0  

like this:

喜欢这个:

}//end switch
fprintf(stderr, "\nunrecognized OP(%c) is specified.\n", op);// '/', '^'...
return -1;//or exit(-1);

#1


2  

While you may believe that your methods will only ever get one of these 6 characters, the compiler has to assume that any character can be passed in. At the moment, these methods will not return anything if you pass in, for example, 'x'.

虽然您可能认为您的方法只能获得这6个字符中的一个,但编译器必须假定可以传入任何字符。目前,如果传入,这些方法将不会返回任何内容,例如,'x ”。

You need to specify a default action for all characters not otherwise covered by your switch. Whether this is returning a default value or throwing an exception, that is up to you. But it's nonetheless a possibility your code needs to handle.

您需要为交换机未覆盖的所有字符指定默认操作。无论是返回默认值还是抛出异常,都取决于您。但它仍然是您的代码需要处理的可能性。

#2


0  

like this:

喜欢这个:

}//end switch
fprintf(stderr, "\nunrecognized OP(%c) is specified.\n", op);// '/', '^'...
return -1;//or exit(-1);