C/C++の宿題を片付けます 116代目at TECH
C/C++の宿題を片付けます 116代目 - 暇つぶし2ch267:デフォルトの名無しさん
08/10/13 22:17:21
>>258#include <stdio.h>
#include <math.h>
int main() {
  double a, b, c, d, x;
  printf("a^2 * x + b * x + c = 0となるa, b, cを入力してください\n");
  scanf("%lf %lf %lf", &a ,&b, &c);
  if (a == 0) {
    puts("2次方程式ではありません");
    return 1;
  }
  b /= a;
  if (c == 0) {
    printf("x = %g, 0\n", -b);
  } else {
    c /= a;
    b /= 2; /* x^2 + 2b'x + c = 0の形へ */
    d = b * b - c; /* 判別式 */
    if (d > 0) {
      if (b > 0) {
        x = -b - sqrt(d);
      } else {
        x = -b + sqrt(d);
      }
      printf("x = %g, %g\n", x, c / x);
    } else if (d < 0) {
      printf("x = %g ± %gi\n", -b, sqrt(-d));
    } else {
      printf("x = %g\n", -b);
    }
  }
  return 0;
}


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch