我正在研究一个项目,当我尝试在一堆整数中添加一个多位数时,只记录第一个数字我很确定问题出在我在这里阅读它们的方式但是我不知道了解导致这种情况发生的原因。如果你能指出我正确的方向,我将不胜感激。
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;
}
答案 0 :(得分:1)
我不知道这是你的唯一问题,但我看到了:
int i;
int c;
while( argv[i] != NULL) // i is uninitialized
{
c = *argv[i]; // c is an integer, *argv[i], if valid, is a character
i++;
push(equation,c);
}
所以,我不确定你在那之后是什么,但我认为你没有正确接近它。
查看sscanf
或atoi
。例如,
sscanf ( argv[i], "%d", &c );
或
c = atoi ( argv[i] );
可能是您正在寻找的。 p>