在下面的C代码中,“item = infix_exp [i ++];”是什么意思?
问题描述:
以下C代码中的item=infix_exp[i++];
是什么意思?第21行。它用于后缀转换。据我所知,这里i
是数组索引。但为什么它没有任何循环递增?在下面的C代码中,“item = infix_exp [i ++];”是什么意思?
这是代码
#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);
}
}
答
item=infix_exp[i++];
装置到所述阵列的i
个元素取到item
,然后由1
看来线递增i
在那里,因为代码的作者优选地使用while((item=infix_exp[i++]) != '\0')
两次写入item=infix_exp[i++];
(另一个在第59行)。
答
这只是一样
item=infix_exp[i];
i = i + 1;
答
,你感到困惑的语句是
item=infix_exp[i++]
此行语句之前如果i
值0
然后在这行i
的值也是0
,但下一行中的i
的值是1
。 该语句不在循环中,但在循环中使用i
的值。循环迭代每次将i
的值增加1。此外,i++
与loop
没有任何关系。如果你想在下一行做出增量效果,那么你可以这样做。