C “我的”没有结果;不理会彭达斯;数字计算器

C “我的”没有结果;不理会彭达斯;数字计算器,c,C,我正在编写一个程序,当给出一个表达式时,它无视PEMDAS规则,只给出一个严格的从左到右的答案(例如5+4*4/6=6)。我很快就要完成了,但我再也不能得到输出了。我已经做了更改,从一次我可以得到一个输出。但现在它什么都给不了我。看看我的前后镜头! 只有在按enter键并给它一个随机的int值后,代码的第一位才起作用。。。它需要另一个int值来完成我设置的while循环。第二个不起作用。我所做的更改是我设置了一个值ck,这个值检查汽车是否有一个值,aka=1。如果没有,则循环完成。让我知道你的想

我正在编写一个程序,当给出一个表达式时,它无视PEMDAS规则,只给出一个严格的从左到右的答案(例如5+4*4/6=6)。我很快就要完成了,但我再也不能得到输出了。我已经做了更改,从一次我可以得到一个输出。但现在它什么都给不了我。看看我的前后镜头! 只有在按enter键并给它一个随机的int值后,代码的第一位才起作用。。。它需要另一个int值来完成我设置的while循环。第二个不起作用。我所做的更改是我设置了一个值ck,这个值检查汽车是否有一个值,aka=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):
   }    
 }
}
#包括
#包括
内部主(空){
int num,总计;
炭车;
//设置整数和字符值
printf(“请输入要计算的表达式:\n”);
scanf(“%d”,&total);//用户提示,获取第一个值。总共存储。
而(汽车!='\n'){
scanf(“%c”和“car”);
scanf(“%d”和&num);
如果(汽车=='*'){
总数=(总数*num);
}//如果输入“*”,则乘以总数和新的数值
否则,如果(汽车=='/'){
总数=(总数/个);
}//如果输入了“/”,则除以总数值和新数值
else如果(car=='+'){
总数=(总数+数量);
}//如果输入“+”,则添加总数和新的数值
否则,如果(汽车=='-'){
总数=(总数);
}//如果输入“-”,则减去总数值和新数值
否则如果(car='\n'){
printf(“%d\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);
   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.
 }
}
#包括
#包括
内部主(空){
int num,总计;
炭车;
int ck=1;//设置整数和字符值
printf(“请输入要计算的表达式:\n”);
scanf(“%d”,&total);//用户提示,获取第一个值。总共存储。
while(ck==1){
scanf(“%c”和“car”);
scanf(“%d”和&num);
ck=scanf(“%c”和“car”);
如果(ck!=1){
printf(“%d\n”,总计);
}//最新代码输入
如果(汽车=='*'){
总数=(总数*num);
}//如果输入“*”,则乘以总数和新的数值
否则,如果(汽车=='/'){
总数=(总数/个);
}//如果输入了“/”,则除以总数值和新数值
else如果(car=='+'){
总数=(总数+数量);
}//如果输入“+”,则添加总数和新的数值
否则,如果(汽车=='-'){
总数=(总数);
}//如果输入“-”,则减去总数值和新数值
//否则如果(car='\n'){
//printf(“%d\n”,总计):
//   }
//if语句块的旧端,给出输出。但仅在另一个int之后
//是放进去的。
}
}

这里的一个问题是,您同时请求操作员(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);
}
#包括
#包括
内部主(空){
int num,总计;
炭车;
int ck=1;//设置整数和字符值
printf(“请输入要计算的表达式:\n”);
scanf(“%d”,&total);//用户提示,获取第一个值。总共存储。
while(ck==1){
scanf(“%c”和“car”);
如果(车!='\n'){
scanf(“%d”和&num);
如果(汽车=='*'){
总数=(总数*num);
}//如果输入“*”,则乘以总数和新的数值
否则,如果(汽车=='/'){
总数=(总数/个);
}//如果输入了“/”,则除以总数值和新数值
else如果(car=='+'){
总数=(总数+数量);
}//如果输入“+”,则添加总数和新的数值
否则,如果(汽车=='-'){
总数=(总数);
}//如果输入“-”,则减去总数值和新数值
}否则{
ck=0;//中断while循环
}
}
printf(“%d\n”,总计);
}

只需使用
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;
}
#包括
#包括
内部主(空){
int num,总计;
炭车;
printf(“请输入要计算的表达式:\n”);
scanf(“%d”,&total);//用户提示,获取第一个值。总共存储。
scanf(“%c”和“car”);
而(汽车!='\n'){
scanf(“%d”,&num);//最新的代码输入
如果(汽车=='*'){
总数=(总数*num);
}//如果输入“*”,则乘以总数和新的数值
否则,如果(汽车=='/'){
总数=(总数/个);
}//如果输入了“/”,则除以总数值和新数值
else如果(car=='+'){
总数=(总数+数量);
}//如果输入“+”,则添加总数和新的数值
否则,如果(汽车=='-'){
总数=(总数);
}
scanf(“%c”和“car”);
} 
printf(“%d”,总计);
返回0;
}

这非常有效!但是当我在Unix.mmm中按enter(\n)直到ctrl-D时它才返回值,可能尝试使用:if(car='\n'&&car!='\r')而不是:if(car!='\n'),这可能不会有什么区别,但可能您在EnterTypo中得到了一个'\r':如果(car!='\n'&&car!='\r'),则尝试使用此选项效果非常好!但它仍然只在我使用ctrl-D强制EOF进入程序时输出,而不是在我只按enter键时输出