0

私は、式が与えられると、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.
 }
}
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' でないかどうかを確認してから、番号を要求する必要があります。これを行う2番目のバージョンの変更は次のとおりです。

#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 に答える