我正在编写一个程序,当给出表达式时,忽略PEMDAS规则,并且只是吐出严格的从左到右的答案(例如5 + 4 * 4/6 = 6)。我已接近完成它,但我再也无法获得输出了。我从一次获得输出后就做出了改变。但现在它不会给我任何东西。看看我之前和之后的镜头! 第一部分代码工作,只有在你按Enter键并给它一个随机的int值...它需要另一个int值来完成我设置的while循环。第二个不起作用。我所做的改变是我设置了一个值ck,这会检查汽车是否有值,即= 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.
}
}
答案 0 :(得分:1)
只需将scanf("%c", &car);
带到while
,然后在while
结束前再次放置,即可修改您的第一个代码。将printf
移出while
。这是您修改后的代码
#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;
}
答案 1 :(得分:0)
这里的问题是您同时要求操作员(汽车)和号码( 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);
}