0

我正在编写一个程序,当给定一个表达式时,它会无视 PEMDAS 规则,只是给出一个严格的从左到右的答案(例如 5+4*4/6 = 6)。我非常接近完成它,但我无法再获得输出。一旦我可以得到输出,我就进行了更改。但现在它不会给我任何东西。看看我之前和之后的拍摄!只有在你按下回车键并给它一个随机的 int 值之后,第一段代码才有效……它需要另一个 int 值来完成我设置的 while 循环。第二个不行。我所做的更改是我设置了一个值 ck,这将检查 car 中是否有值,也就是 = 1。如果没有,则循环结束。让我知道你的想法。非常感谢您的帮助。

#include <stdio.h>
#include <stdlib.h>

int main(void){
  int num, total;
  char car;
  //Setting up integers and a char value

  printf("Please enter an expression to be evaluated: \n");
  scanf("%d", &total); // User prompt, grabs the first value. Stores in total.

   while(car != '\n'){

   scanf(" %c", &car);
   scanf("%d", &num);

    if(car == '*'){
    total = (total*num);
    } // Multiplies total and new number value if '*' is enterd

   else if(car == '/'){
     total = (total/num);
   } // Divides total and new number value if '/' is entered

   else if(car == '+'){
     total = (total+num);
   } // Adds total and new number value if '+' is entered

   else if(car == '-'){
     total = (total-num);
   } // Subtracts total and new number value if '-' is entered

       else if(car == '\n'){
       printf("%d\n", total):
   }    
 }
}

==================================================== =========================================

#include <stdio.h>
#include <stdlib.h>

int main(void){
  int num, total;
  char car;
  int ck = 1; //Setting up integers and a char value

  printf("Please enter an expression to be evaluated: \n");
  scanf("%d", &total); // User prompt, grabs the first value. Stores in total.

   while(ck == 1 ){

   scanf(" %c", &car);
   scanf("%d", &num);

   ck = scanf(" %c", &car);
   if(ck != 1){
     printf("%d\n", total);
   }//Newest code input

    if(car == '*'){
    total = (total*num);
    } // Multiplies total and new number value if '*' is enterd

   else if(car == '/'){
     total = (total/num);
   } // Divides total and new number value if '/' is entered

   else if(car == '+'){
     total = (total+num);
   } // Adds total and new number value if '+' is entered

   else if(car == '-'){
     total = (total-num);
   } // Subtracts total and new number value if '-' is entered

    //   else if(car == '\n'){
    //   printf("%d\n", total):
    //   }
    // Old end to if statement block, gives output. But only after another int
    // is put in.
 }
}
4

2 回答 2

1

只需编辑您的第一个代码,将其scanf("%c", &car);取出while并再次放置在while. 搬出printfwhile这是您修改后的代码

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int num, total;
    char car;

    printf("Please enter an expression to be evaluated: \n");
    scanf("%d", &total); // User prompt, grabs the first value. Stores in total.
    scanf("%c", &car);
    while(car != '\n'){
        scanf("%d", &num); //Newest code input

        if(car == '*'){
            total = (total*num);
        } // Multiplies total and new number value if '*' is enterd

        else if(car == '/'){
              total = (total/num);
        } // Divides total and new number value if '/' is entered

        else if(car == '+'){
             total = (total+num);
        } // Adds total and new number value if '+' is entered

        else if(car == '-'){
             total = (total-num);
        }
        scanf("%c", &car);
    } 
    printf ("%d", total);
    return 0;
}
于 2013-10-04T20:59:01.553 回答
0

这里的一个问题是您同时要求一个运算符(car)和一个数字(num)。您应该先询问car,然后检查它是否不是 '\n',然后询问号码。这是对您的第二个版本的修改:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int num, total;
    char car;
    int ck = 1; //Setting up integers and a char value
    printf("Please enter an expression to be evaluated: \n");
    scanf("%d", &total); // User prompt, grabs the first value. Stores in total.
    while(ck == 1 ) {
        scanf("%c", &car);
        if (car !='\n') {
            scanf("%d", &num);

            if(car == '*') {
                total = (total*num);
            } // Multiplies total and new number value if '*' is enterd

            else if(car == '/') {
                total = (total/num);
            } // Divides total and new number value if '/' is entered

            else if(car == '+') {
                total = (total+num);
            } // Adds total and new number value if '+' is entered

            else if(car == '-') {
                total = (total-num);
            } // Subtracts total and new number value if '-' is entered
        } else {
            ck=0; //break the while loop
        }
    }
    printf("%d\n", total);
}
于 2013-10-04T20:40:38.040 回答