我正在编写一个程序,当给定一个表达式时,它会无视 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.
}
}