簡體   English   中英

評估后綴表達式時不需要的符號結果

[英]Unwanted symbol result when evaluating postfix expression

我正在嘗試制作一個C程序來評估后綴表達式,並且這樣做時,在輸入45+的屏幕上打印了不需要的符號。 附言:請告訴我這個錯誤(除了我正在研究的get()之外,現在如何使用fgets())

// to Evaluate a postfix expression
#include<stdio.h>
#include<conio.h>
int is_operator(char);
void answer();
char stack[100];
int top =-1;
void push(char);
char pop();
void main()
{
char postfix[100],item;
int i=0;
clrscr();
printf("Enter Postfix Expression");
gets(postfix);
while(postfix[i]!='\0')
    {
    item=postfix[i];
    if(is_operator(item)==2)
        {
        push(item);
        }
    if(is_operator(item)==1)
        {
        char op;
        int n1,n2,n3;
        op=item;
        n1=pop();
        n2=pop();
        switch(op)
        {
            case '+':
            n3=n1+n2;
            case '-':
            n3=n1-n2;
            case '*':
            n3=n1*n2;
            case '/':
            n3=n1/n2;
        }
        push(n3);
        }
    i++;
    }//end while
answer();
getch();
}
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return(c);
}
int is_operator(char i)
{
char ch=i;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
    {
    return(1);
    }
else
    {
    return(2);
    }
}
void answer()
{
char ans;
ans=stack[top];
printf("Answere is %c",ans);
}

您的代碼中有很多錯誤。請嘗試正確鍵入cast。

通過評論來了解錯誤。

通過這篇文章了解字符指針和數組。

//評估后綴表達式

#include<stdio.h>



int is_operator(char);
void answer();
int stack[100];//Use integer array since operands are integer
int top =-1;
void push(int);//Arguments changed to integer type since the stack is integer 
int pop(); //Return type to integer
void main()
{
char* postfix;//Use character pointer for iterating through loop smoothly
 int item;
int i=0;

printf("Enter Postfix Expression");
gets(postfix);
char c;

while(*postfix!='\0')
    {
    c=*postfix;
    if(is_operator(c)==2)
        {
        push((c-'0')); //Converting char to int before pushing it into the stack
        }
    if(is_operator(c)==1)
        {


        char op;
        int n1,n2,n3;
        op=*postfix;
        n1=pop();
        n2=pop();
        switch(op)
        {
            case '+':
            n3=n1+n2;
            break;
            case '-':
            n3=n1-n2;
            break;
            case '*':
            n3=n1*n2;
            break;
            case '/':
            n3=n1/n2;
            break;
        }
        push(n3);
        }
    postfix++;
    }//end while
answer();

}
void push(int c)
{
top++;
stack[top]=c;
}
int pop()
{
int c;
c=stack[top];
top--;
return(c);
}
int is_operator(char i)
{
char ch=i;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
    {
    return(1);
    }
else
    {
    return(2);
    }
}
void answer()
{
char ans;
ans=stack[top];
printf("Answere is %d",ans);
}  

希望對您有所幫助。

我在您的代碼中看到的問題是:您的switch()在單個case子句上缺少break語句( default情況下也可能很好); 當您將非運算符(也稱為一位數字)壓入堆棧時,您將它們作為字符代碼壓入而不是將它們轉換為數字,這樣數學就沒有意義了; 您沒有正確處理非相減運算的順序,例如減法和除法(使用Unix dc命令作為比較工具); 最后,不要重塑布爾值。 以下是對代碼的重做,其中包含上述更改和一些樣式調整:

// Evaluate a postfix expression

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

char stack[100];
int top = -1;

void push(char);
char pop(void);
bool is_operator(char);
void answer(void);

void push(char c)
{
    stack[++top] = c;
}

char pop()
{
    return stack[top--];
}

bool is_operator(char op)
{
    return (op == '+' || op == '-' || op == '*' || op == '/');
}

void answer()
{
    printf("Answer is %d\n", stack[top]);
}

int main()
{
    char item, postfix[100];
    int i = 0;

    printf("Enter Postfix Expression: ");

    gets(postfix);

    while ((item = postfix[i++]) != '\0')
    {
        if (is_operator(item))
        {
            char n1 = pop();
            char n2 = pop();
            char n3 = 0;

            switch (item)
            {
                case '+':
                    n3 = n1 + n2;
                    break;
                case '-':
                    n3 = n2 - n1;
                    break;
                case '*':
                    n3 = n1 * n2;
                    break;
                case '/':
                    n3 = n2 / n1;
                    break;
            }

            push(n3);
        } else if (isdigit(item)) {
            push(item - '0');
        }
    } // end while
    answer();

    return 0;
}

示例(請注意,此評估程序僅對一位數字起作用):

> ./a.out
Enter Postfix Expression: 6 4 - 7 * 1 +
Answer is 15
> dc
6 4 - 7 * 1 + p
15

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM