0
我正在開發一個項目,當我嘗試向一個整數堆棧中添加一個多位數字時,只記錄第一個數字我很確定問題出在方法上我在這裏閱讀他們,但我不明白是什麼導致了這種情況發生。如果你能指出我正確的方向,我將不勝感激。添加一個整數時只記錄第一個數字
int main(int argc,const char *argv[])
{
FILE *src = input_from_args(argc, argv);
if (src == NULL)
{
printf("%s", "Invalid Source");
exit(EXIT_FAILURE);
}
else
{
struct stack * equation = NULL;
equation = (struct stack*)malloc(sizeof(struct stack));
equation -> top = -1;
int i;
int c;
while(argv[i] != NULL)
{
c = *argv[i];
i++;
push(equation,c);
}
if(strcmp(argv[1],"-e") == 0)
{
evaluate(equation);
printf("%i \n", pop(equation));
}
else if(strcmp(argv[1],"-c") == 0)
{
convert(equation);
}
else if(strcmp(argv[1],"-g") == 0)
{
other(equation);
}
}
return EXIT_SUCCESS;
}
struct stack
{
int arr[100];
int top;
};
void push(struct stack *st, int c)
{
if (st->top == 99)
{
printf("Stack is full");
return ;
}
st->top++;
st->arr[st->top] = c;
}
此行'while(argv [i]!= NULL)'使用未初始化的'i'。 –
這不會編譯。你能提供一個可編輯例子的必要條件嗎? – ryyker
'c = * argv [i];'您是否啓用並記下所有編譯器警告?這也會揭示我之前評論過的錯誤。 –