2016-08-05 85 views
0

我正在嘗試製作一個C程序來評估後綴表達式,並且在輸入45+時在屏幕上打印不需要的符號。 P.S.請告訴我的錯誤(除非那得到()我現在如何用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); 
} 
+0

是否也能在此附上電流輸出和期望的輸出? (通過[編輯]你的問題) –

+1

如果你以前從未使用調試器,現在是開始的最佳時機。使用調試器,您可以逐行瀏覽代碼,同時監控變量及其值,以及它們如何變化。通過這樣做,您應該能夠輕鬆地找出發生的地點,並希望爲什麼。 –

+0

當前輸出是像smilley面的不需要的符號而通緝輸出是9 @JonnyHenly – user6547375

回答

0

我與你的代碼中看到的問題是:你switch()缺少對個人casebreak聲明條款(和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 
+0

在is_operator函數中,爲什麼返回運算符(op),而返回類型是int 在答案函數中,爲什麼您使用%d作爲堆棧數組是字符類型。 – user6547375

+0

@ user6547375,'is_operator()'不返回一個運算符,它返回* true *或* false *,就像我們所期望的以'is_'開頭的函數一樣,即詢問一個問題。爲了更加明確,我已經包含了stdbool庫並將返回類型改爲'bool'。堆棧數組由數字組成,它們恰好是被標記爲「char」的8位數字。在堆棧上打印結果時,我們需要計算得出的數字'%d',而不是映射到該結果的ASCII字符'%c'。 – cdlane

1

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

通過評論來了解錯誤。

通過this瞭解字符指針和數組。

//來評價一個後綴表達式

#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); 
} 

我希望這是很有幫助....