gpt4 book ai didi

c - for循环的退出条件是什么?

转载 作者:行者123 更新时间:2023-11-30 18:34:38 26 4
gpt4 key购买 nike

我的下面的程序工作正常,但我不明白如何使用 exp[i] 作为标记位置的循环终止条件。为什么以及在什么情况下这会导致循环退出?

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

// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};

// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

if (!stack)
return NULL;

stack->top = -1;
stack->capacity = capacity;

stack->array = (int*) malloc(stack->capacity * sizeof(int));

if (!stack->array)
return NULL;
return stack;
}

int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}

char peek(struct Stack* stack)
{
return stack->array[stack->top];
}

char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}

void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}


// A utility function to check if the given character is operand
int isOperand(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

// A utility function to return precedence of a given operator
// Higher returned value means higher precedence
int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}


// The main function that converts given infix expression
// to postfix expression.
int infixToPostfix(char* exp)
{
int i, k;

// Create a stack of capacity equal to expression size
struct Stack* stack = createStack(strlen(exp));
if(!stack) // See if stack was created successfully
return -1 ;

下面这个for循环的退出条件是什么?

for (i = 0, k = -1; exp[i]; ++i) 
{
// If the scanned character is an operand, add it to output.
if (isOperand(exp[i]))
exp[++k] = exp[i];

// If the scanned character is an ‘(‘, push it to the stack.
else if (exp[i] == '(')
push(stack, exp[i]);

// If the scanned character is an ‘)’, pop and output from the stack
// until an ‘(‘ is encountered.
else if (exp[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
exp[++k] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else // an operator is encountered
{
while (!isEmpty(stack) && Prec(exp[i]) <= Prec(peek(stack)))
exp[++k] = pop(stack);
push(stack, exp[i]);
}

}

// pop all the operators from the stack
while (!isEmpty(stack))
exp[++k] = pop(stack );

exp[++k] = '\0';
printf( "%sn", exp );
}

// Driver program to test above functions
int main()
{
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}

最佳答案

只要 exp[i] 计算结果为 true,循环就会继续。

exp[i] 在没有任何其他上下文的情况下,计算结果为 char。可以在需要 bool 值的地方使用char。如果 char 的值为 0,则 bool 值将为 false,否则为 true

在您的使用中,循环将继续执行 exp 的每个字符(终止空字符除外)。当遇到终止空字符时,循环将停止。

更易读的形式是使用 exp[i] != '\0'

for (i = 0, k = -1; exp[i] != '\0'; ++i) { ... }

关于c - for循环的退出条件是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com