所以由于某种原因,这个函数似乎将所有相同的值推入堆栈(或者只是打印相同的值;我想它实际上可能与我也包含的 printAll 函数有关。)?问题是,对于元素数组(它是一个整数数组), printAll 适当地循环遍历这些值。但是关于 charElement 数组的任何事情,printAll 函数只打印 charElements 函数中每个值的最新值。知道为什么会这样吗?
void pushString(Stack *S, char element[])
{
/* If the stack is full, we cannot push an element into it as there is no space for it.*/
if(S->size == S->capacity)
{
printf("Stack is Full\n");
}
else
{
/* Push an element on the top of it and increase its size by one*/
S->charElements[S->size++] = element;
S->elements[S->size-1] = '\n';
}
return;
}
void printAll(Stack *S)
// NOTE: Called w/ user input 'f'
// PRE: Stack S is initialized
// POST: Print all values from the stack
// Do NOT alter anything
{
int i;
for(i = 0; i < S->size; i++)
{
printf("%d \n", i);
if(S->charElements[i] == "\n")
{
printf("%.2f \n", (float)S->elements[i]);
}
else if(S->elements[i] == '\n')
{
printf("%s \n", S->charElements[i]);
}
}
}