私は、式が与えられると、PEMDAS ルールを無視し、厳密な左から右への回答 (例: 5+4*4/6 = 6) を吐き出すプログラムを書いています。完成に近づいていますが、もう出力を得ることができません。出力が得られたら、変更を加えました。でも今は何もくれない。前後のショットを見てください!コードの最初のビットは、Enter キーを押してランダムな int 値を指定した後にのみ機能します。設定した while ループを終了するには、別の int 値が必要です。2 つ目は機能しません。私が行った変更は、値 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.
}
}