C 中的 RPN 计算器
RPN Calculator in C
有人可以解释一下为什么我在 myString 上使用 strok() 后总是出现乱码吗?我的输出图片在底部。我尝试将 myString 中的每个元素初始化为 NULL 字符,但我的代码仍然不起作用:(
预期输出:
我的输出:
int main()
{
int i;
char myString[60];
char *token;
float result;
float x;
float y;
struct Stack myStack;
StackInit(&myStack);
BOARD_Init();
printf("\nWelcome to my RPN calculator.\n");
printf("Enter floats and + - / * in RPN format: \n >");
for (i = 0; i < sizeof (myString); i++) {
myString[i] = '[=10=]';
}
fgets(myString, sizeof (myString), stdin);
token = strtok(myString, " ");
while (token != NULL) {
printf("%f\n", atof(token));
token = strtok(NULL, " ");
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
} else if (*token == '+') { // if token is addition
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x + y;
StackPush(&myStack, result);
} else if (*token == '-') { // if token is subtraction
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x - y;
StackPush(&myStack, result);
} else if (*token == '*') { // if token is multiplication
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x * y;
StackPush(&myStack, result);
} else if (*token == '/') { // if token is division
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x / y;
StackPush(&myStack, result);
}
}
while (1);
}
正如@BLUEPIXY 在评论中指出的,您的代码在这里有问题:
token = strtok(myString, " "); // Here you get the first sub string
while (token != NULL) {
printf("%f\n", atof(token));
token = strtok(NULL, " "); // Here you get the second sub string
// and throw away the first substring
// So all the code below is never executed
// for the first substring.
// In other words: You never push the first
// float to the stack
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
您应该做的是将 token = strtok(NULL, " ");
移动到循环的末尾:
while (token != NULL) {
printf("%f\n", atof(token));
// Not here............. token = strtok(NULL, " ");
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
} else if (*token == '+') { // if token is addition
//...
//...
}
token = strtok(NULL, " "); // but here
}
有人可以解释一下为什么我在 myString 上使用 strok() 后总是出现乱码吗?我的输出图片在底部。我尝试将 myString 中的每个元素初始化为 NULL 字符,但我的代码仍然不起作用:(
预期输出:
我的输出:
int main()
{
int i;
char myString[60];
char *token;
float result;
float x;
float y;
struct Stack myStack;
StackInit(&myStack);
BOARD_Init();
printf("\nWelcome to my RPN calculator.\n");
printf("Enter floats and + - / * in RPN format: \n >");
for (i = 0; i < sizeof (myString); i++) {
myString[i] = '[=10=]';
}
fgets(myString, sizeof (myString), stdin);
token = strtok(myString, " ");
while (token != NULL) {
printf("%f\n", atof(token));
token = strtok(NULL, " ");
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
} else if (*token == '+') { // if token is addition
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x + y;
StackPush(&myStack, result);
} else if (*token == '-') { // if token is subtraction
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x - y;
StackPush(&myStack, result);
} else if (*token == '*') { // if token is multiplication
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x * y;
StackPush(&myStack, result);
} else if (*token == '/') { // if token is division
StackPop(&myStack, &y);
StackPop(&myStack, &x);
result = x / y;
StackPush(&myStack, result);
}
}
while (1);
}
正如@BLUEPIXY 在评论中指出的,您的代码在这里有问题:
token = strtok(myString, " "); // Here you get the first sub string
while (token != NULL) {
printf("%f\n", atof(token));
token = strtok(NULL, " "); // Here you get the second sub string
// and throw away the first substring
// So all the code below is never executed
// for the first substring.
// In other words: You never push the first
// float to the stack
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
您应该做的是将 token = strtok(NULL, " ");
移动到循环的末尾:
while (token != NULL) {
printf("%f\n", atof(token));
// Not here............. token = strtok(NULL, " ");
if (atof(token) != 0) {
StackPush(&myStack, atof(token));
} else if (*token == '+') { // if token is addition
//...
//...
}
token = strtok(NULL, " "); // but here
}