2020年1月10日 星期五

算式比較

題目:
撰寫1隻程式,首先輸入「題號」選擇題目後輸出題目,並依序填入參數後,輸出參數是否符合算式。
題號:
1. a + b = c
2. a + b / c = d
3. a * b >= c

使用者可能輸入整數或浮點數,輸出結果取到小數第二位。

範例輸出:









解題思路:
注意輸出表達式時,「"」前面要加上「/」。


程式碼:

#include <stdio.h>

int main()
{
 int n;
 float a, b, c, d;
 printf("Please choose the expression: ");
 scanf_s("%d", &n);
 if (n == 1)
 {
  printf("The expression is \"a+b=c\"\n");
  printf("a= ");
  scanf_s("%f", &a);
  printf("b= ");
  scanf_s("%f", &b);
  printf("c= ");
  scanf_s("%f",&c);
  if (a + b == c)
   printf("\"%.2f + %.2f = %.2f\" is correct\n", a, b, c);
  else
   printf("\"%.2f + %.2f = %.2f\" is incorrect\n", a, b, c);
 }
 else if (n == 2)
 {
  printf("The expression is \"a+b/c=d\"\n");
  printf("a= ");
  scanf_s("%f", &a);
  printf("b= ");
  scanf_s("%f", &b);
  printf("c= ");
  scanf_s("%f", &c);
  printf("d= ");
  scanf_s("%f", &d);
  if ((a + b / c) == d)
   printf("\"%.2f + %.2f/%.2f = %.2f\" is correct\n", a, b, c, d);
  else
   printf("\"%.2f + %.2f/%.2f = %.2f\" is incorrect\n",a, b, c, d);
 }
 else if (n == 3)
 {
  printf("The expression is \"a*b>=c\"\n");
  printf("a= ");
  scanf_s("%f", &a);
  printf("b= ");
  scanf_s("%f", &b);
  printf("c= ");
  scanf_s("%f", &c);
  if (a *b >= c)
   printf("\"%.2f * %.2f >= %.2f\" is correct\n", a, b, c);
  else
   printf("\"%.2f * %.2f >= %.2f\" is incorrect\n", a, b, c);
 }
 return 0;
}

沒有留言:

張貼留言