【課題2−2】

 

(プログラム例)

#include <stdio.h>

int main(void) {

         int n, m, result;

         char op;        /* 演算子の入力を受け取る */

 

         printf("Input two numbers and an operator (e.g., 10 * 15): ");

         scanf("%d %c %d", &n, &op, &m);

        

         /* 入力された演算子で多項分岐 */

         switch (op) {  case '+':       result = n + m;

                          break;

         case '-':       result = n - m;

                          break;

         case '*':       result = n * m;

                          break;

         case '/':       result = n / m;

                          break;

         case '%':       result = n % m;

                          break;

         default:        printf("Unknow operator \"%c\"\n", op);

                          return 1;

         }

                         

         printf("Answer: %d\n", result);

 

         return 0;

        

}

 

(実行例)

Input two numbers and an operator (e.g., 10 * 15): 57 % 7

Answer: 1