c - 在哪里放置 printf 以避免在我的 rpn 计算器中多次打印输出?

标签 c printf calculator rpn

我的 rpn 计算器可以工作,但有问题。问题在于它会打印出一路上进行的每个连续计算。

我尝试了不同的方法来解决这个问题,最新的一种方法是添加一个每次计算都会增加的整数,以及一个 printf,如果该整数大于 0 并且堆栈上只有一个数字,则打印该整数。

但是,当进行多个计算时(例如写入 5 5 + 10 5 * *),这会导致问题,将导致打印出 10 500,因为第一次计算后堆栈上只有一个项目。

我该如何解决这个问题?

#define MAX_STACK_SIZE 100
#define MAX_BUFFER_SIZE 100

char buff[MAX_BUFFER_SIZE];
     double x, stack[MAX_STACK_SIZE];
     double t;
     int i, k=0, num_operand=0;


int main(void) {

while(x != 'q') {
    if( scanf("%s", buff) < 1 )
             { return 0;
}
if(isdigit(buff[0]) || isdigit(buff[1])) {
    sscanf(buff, "%lf", &x);

if (num_operand < MAX_STACK_SIZE)
                     {
                             stack[num_operand]=x;
                             num_operand ++;
                     } else { printf("Make stack bigger\n");}

} else {
switch(buff[0]) {
    case '+': stack[num_operand - 2] = stack[num_operand - 1] + stack[num_operand - 2];
                                                num_operand --;
                                                num_operand --;
                                                t = stack[num_operand];
                                                k++;
                                                num_operand ++;
                                                break;
    case '-': stack[num_operand - 2] = stack[num_operand - 2] - stack[num_operand - 1];
                                                num_operand --;
                                                num_operand --;
                                                t = stack[num_operand];
                                                k++;
                                                num_operand ++;
                                                break;
    case '/': stack[num_operand - 2] = stack[num_operand - 2] / stack[num_operand - 1];
                                                num_operand --;
                                                num_operand --;
                                                t = stack[num_operand];
                                                k++;
                                                num_operand ++;
                                                break;
    case '*': stack[num_operand - 2] = stack[num_operand - 1] * stack[num_operand - 2];
                                                num_operand --;
                                                num_operand --;
                                                t = stack[num_operand];
                                                k++;
                                                num_operand ++;
                                                break;
    } }
    if (num_operand == 1 && k !=0) {
        k = 0;
        printf("%lf \n", t); }
}
}

最佳答案

"%s" 消耗像 ' ''\n' 这样的前导空格 - 因此行尾之间的区别并且空格分隔符丢失。

要区分输入的,请使用fgets()并处理该行。然后打印结果。 @molbdnilo

它测试q,代码需要测试该行的文本内容,而不是double x@BLUEPIXY

int main(void) {
  char line[MAX_BUFFER_SIZE];

  // read line
  while (fgets(line, sizeof line, stdin) && line[0] != 'q') {
    // Set up these variables per each line.
    double x, stack[MAX_STACK_SIZE];
    double t;
    int i, k = 0, num_operand = 0;

    const char *p = line;
    char buff[MAX_BUFFER_SIZE];
    int n;  // # of characters scanned

    // process tokens
    while (sscanf(p, "%s %n", buff, &n) == 1) {
      ...

      // next token
      p += n;
    } // endwhile

    // print
    if (num_operand == 1 && k != 0) {
      k = 0;
      printf("%lf \n", t);
      fflush(stdout);
    }
  } // endwhile

关于c - 在哪里放置 printf 以避免在我的 rpn 计算器中多次打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46179091/

相关文章:

c - 读取文件和存储数据链表错误c

编译器在打印变量地址时发出警告

c++ - 减法给了我积极的结果 C++

Java中缀计算器逻辑

c - 为什么我的 C Yacc/lex 计算器无法正确解析?

c - sprintf 的奇怪工作

iphone - 从一个字符串中删除另一字符串中存在的字符

c++ - NetBeans 使用什么默认 make 工具

c - 为什么 printf 无法打印 Jack 和 George 的名字?

perl - 为什么 sprintf 以不同的方式舍入 5.555 和 0.555 的小数部分?