2016-04-14 20 views
1

Aşağıdaki C kodunda item=infix_exp[i++]; ile ne anlama geliyor? Satır no 21. Bu, postfix dönüştürme için infix içindir. Bildiğim kadarıyla, burada i dizi dizini. Ama neden döngü olmadan artıyor? Aşağıdaki C kodunda "item = infix_exp [i ++];" ile ne anlama geliyor?

Bu

#include<stdio.h> 
#include<conio.h> 
#define SIZE 100 
int top = -1; 
char stack[SIZE]; 
void push(char item); 
char pop(); 
int is_operator(char symbol); 
int precedence(char symbol); 
void main() 
{ 
    int i; 
    int j; 
    char infix_exp[SIZE], postfix_exp[SIZE]; 
    char item; 
    char x; 
    printf("\nEnter Infix expression in parentheses: \n"); 
    gets(infix_exp); 
    i=0; 
    j=0; 
    item=infix_exp[i++]; /* HERE */ 
    while(item != '\0') 
    { 
     if(item == '(') 
     { 
      push(item); 
     } 
     else if((item >= 'A' && item <= 'Z') || 
       (item >= 'a' && item <= 'z')) 
     { 
      postfix_exp[j++] = item; 
     } 
     else if(is_operator(item) == 1) 
     { 
      x=pop(); 
      while(is_operator(x) == 1 && precedence(x) 
        >= precedence(item)) 
      { 
       postfix_exp[j++] = x; 
       x = pop(); 
      } 
      push(x); 
      push(item); 
     } 
     else if(item == ')') 
     { 
      x = pop(); 
      while(x != '(') 
      { 
       postfix_exp[j++] = x; 
       x = pop(); 
      } 
     } 
     else 
     { 
      printf("\nInvalid Arithmetic Expression.\n"); 
      getch(); 
     } 
     item = infix_exp[i++]; 
    } 
    postfix_exp[j++] = '\0'; 
    printf("\nArithmetic expression in Postfix notation: "); 
    puts(postfix_exp); 
    getch(); 
} 
void push(char item) 
{ 
    if(top >= SIZE-1) 
    { 
     printf("\nStack Overflow. Push not possible.\n"); 
    } 
    else 
    { 
     top = top+1; 
     stack[top] = item; 
    } 
} 
char pop() 
{ 
    char item = NULL; 
    if(top <= -1) 
    { 
     printf("\nStack Underflow. Pop not possible.\n"); 
    } 
    else 
    { 
     item = stack[top]; 
     stack[top] = NULL; 
     top = top-1; 
    } 
    return(item); 
} 
int is_operator(char symbol) 
{ 
    if(symbol == '^' || symbol == '*' || symbol == '/' || 
     symbol == '+' || symbol == '-') 
    { 
     return 1; 
    } 
    else 
    { 
     return 0; 
    } 
} 
int precedence(char symbol) 
{ 
    if(symbol == '^') 
    { 
     return(3); 
    } 
    else if(symbol == '*' || symbol == '/') 
    { 
     return(2); 
    } 
    else if(symbol == '+' || symbol == '-') 
    { 
     return(1); 
    } 
    else 
    { 
     return(0); 
    } 
} 

cevap

2

item=infix_exp[i++]; sonra, item için dizinin i inci elemanı getir O çizgiyi görünüyor 1.

tarafından i artırmak için anlamı Kod var, çünkü kod yazar'u kullanmak için item=infix_exp[i++];'u (diğeri 59 satırında) iki kez yazmayı tercih etti. bu ikinci bir daha sequence point sahip dışında

1

item=infix_exp[i++];

item=infix_exp[i]; 
i++; 

eşdeğerdir.

1

O i değeri bu çizgide sonra 0 ise

item=infix_exp[i]; 
i = i + 1; 
1

sen bocalama vardır deyimi

item=infix_exp[i++]

ifadenin bu hat önce sadece aynı gibidir i değeri de 0'dir, ancak sonraki satırdaki i değeri 1'dur. Bu ifade döngüde değil, i değeri bir döngüde kullanılıyor. Döngü her yinelendiğinde i değeri bir artırılır. Ayrıca, i++'un loop ile ilişkisi yoktur. Bir sonraki satırda arttırma etkisini yapmak istiyorsanız bunu yapabilirsiniz.