当中缀或后缀遇到" - "签署它似乎给我随机值

时间:2014-11-10 22:46:37

标签: c stack postfix-notation infix-notation

在我的程序中,当我有一个后缀或中缀表示法来转换和评估不包含“ - ”符号时它似乎与我的第一个输入文本一起工作,但是当我尝试使用我的第二个输入文本运行它时当遇到“ - ”并打印垃圾时,它会做一些有趣的事情。任何帮助将不胜感激。这是两个文本文件,第一个工作,第二个给出错误。谢谢S.O!

它应该运行直到读取0,如果选择为1,求解中缀,如果为2,则解决后缀。 第一个输入文件(输出正确)

我正在尝试输入我的输入文件,但它一直在说它的代码...... 所以我把它作为代码的标题。任何其他信息只需要询问。

//1st input file, correct


//1 (5 + 4)
//2 3 4 +
//1 2 * 2 / (1+3)
//0

//second input file (incorrect)


//2 7 5 - 2 +
//1 3 * (5 – 2 )
//1 5/1 + 3
//0

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define BLANK ' '
#define TAB '\t'
#define MAX 50

void push(long int symbol);
long int pop();
void infix_to_postfix();
int priority(char symbol);
int isEmpty();
int white_space(char);
int eval_post();

char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;

int main(int argc, char *argv[])
{
    FILE* fp;
    FILE* fpw;

    fp = fopen("info.txt", "r" );
    fpw = fopen("write.txt ", "w");

    int choice;
    int ans;

    fscanf(fp, "%d", &choice);
    printf("first choice selected: %d \n", choice);


    while(choice != 0)
        {

            if(choice ==1)
            {
            printf("selection was infix \n");
            fscanf(fp, "%[^\n]%*c", infix );
            printf("%s \n", infix);
            infix_to_postfix();
            fprintf(fpw," %s = ", postfix);
            ans = eval_post();
            fprintf(fpw, " %d \n", ans);


            fscanf(fp, "%d", &choice);
            printf("next choice selection was: %d \n", choice);
            }
            else if(choice == 2)
            {
                printf("selection was postfix \n");
                fscanf(fp, "%[^\n]%*c", infix );
                fprintf(fpw,"%s = ", infix);
                ans = eval_post();
                fprintf(fpw,"%d \n", ans);

                fscanf(fp, "%d", &choice);
                printf("next choice selection was: %d \n", choice);
            }

        }
    printf("Hello world!\n");
    return 0;
}

void infix_to_postfix()
{
    unsigned int i,p=0;
    char next;
    char symbol;
    for(i=0;i<strlen(infix);i++)
    {
        symbol=infix[i];
        if(!white_space(symbol))
        {
            switch(symbol)
            {
            case '(':
                push(symbol);
                break;
            case ')':
                while((next=pop())!='(')
                    postfix[p++] = next;
                break;
            case '+':
            case '-':
            case '*':
            case '/':
            case '%':
            case '^':
                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )
                    postfix[p++]=pop();
                push(symbol);
                break;
            default: /*if an operand comes*/
                 postfix[p++]=symbol;
            }
        }
    }
    while(!isEmpty( ))
        postfix[p++]=pop();
    postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}

/*This function returns the priority of the operator*/
int priority(char symbol)
{
    switch(symbol)
    {
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
    case '%':
        return 2;
    case '^':
        return 3;
    default :
        return 0;
    }
}

void push(long int symbol)
{
    if(top>MAX)
    {
        printf("Stack overflow\n");
        exit(1);
    }
    stack[++top]=symbol;
}

long int pop()
{
    if( isEmpty() )
    {
        printf("Stack underflow\n");
        exit(1);
    }
    return (stack[top--]);
}
int isEmpty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int white_space(char symbol)
{
    if( symbol == BLANK || symbol == TAB )
        return 1;
    else
        return 0;
}

int eval_post()
{
    int a,b,temp,result;
    unsigned int i;

    for(i=0;i<strlen(postfix);i++)
    {
        if(postfix[i]<='9' && postfix[i]>='0')
            push(postfix[i]-'0');
        else
        {
            a=pop();
            b=pop();
            switch(postfix[i])
            {
            case '+':
                temp=b+a; break;
            case '-':
                temp=b-a;break;
            case '*':
                temp=b*a;break;
            case '/':
                temp=b/a;break;
            case '%':
                temp=b%a;break;
            case '^':
                temp=pow(b,a);
            }
            push(temp);
        }
    }
    result=pop();
    return result;
}

1 个答案:

答案 0 :(得分:0)

您有choice == 2的代码:

printf("selection was postfix \n");
fscanf(fp, "%[^\n]%*c", infix );
fprintf(fpw,"%s = ", infix);
ans = eval_post();
...

eval_post()确实:

for(i = 0; i < strlen(postfix); i++)

postfix永远不会被初始化...您将字符串复制到infix。可能你打算这样做:

else if (choice == 2)
{
    printf("selection was postfix \n");
    fscanf(fp, "%[^\n]%*c", postfix);
    fprintf(fpw,"%s = ", postfix);
    ans = eval_post();

有可能你的第一组输入实际上没有正常工作,或者恰好看起来像运气一样。

修改 实际上,如果您只是传递postfix字符串"7 5 - 2 +",由于字符串中的空格,它会在eval_post()中失败。您可以手动解析空格,也可以在eval_post()中忽略它们,如:

    if (white_space(postfix[i]))
    { // Do nothing 
    }
    else if(postfix[i]<='9' && postfix[i]>='0')
        push(postfix[i]-'0');
    ...