-10

だから私は二次方程式を解くCプログラムを作ろうとしています。私は最初からそれを書きましたが、同じエラーが表示されたので、C プログラミングの本からいくつかの変更を加えました。結果は次のとおりです。

/*
Solves any quadratic formula.
*/
#include <stdio.h>
#include <math.h>

void main()
{
 float a, b, c, rt1= 0, rt2=0, discrim;
 clrscr();
 printf("Welcome to the Quadratic Equation Solver!");
 getch();
 printf("\nYour quadratic formula should be of the form a(x*x)+bx+c = 0");
 printf("\nPlease enter a\'s value:");
 scanf("%f", &a);
 printf("Great! Now enter b\'s value:");
 scanf("%f", &b);
 printf("One more to go! Enter c\'s value:");
 scanf("%f", &c);
 discrim = b*b - 4*a*c;
 if (discrim < 0)
  printf("\nThe roots are imaginary.");
 else
 {
  rt1 = (-b + sqrt(discrim)/(2.0*a);
  rt2 = (-b - sqrt(discrim)/(2.0*a);
  printf("\nThe roots have been calculated.");
  getch();
  printf("\nThe roots are:\nRoot 1:%f\nRoot 2:%f",rt1, rt2);
  getch();
  printf("\nThank you!");
  getch();
 }
}
4

4 に答える 4

2

まず、括弧の不一致がいくつかあります。

 rt1 = (-b + sqrt(discrim)/(2.0*a);
 rt2 = (-b - sqrt(discrim)/(2.0*a);

これらの行を次のように変更する必要があります。

 rt1 = (-b + sqrt(discrim))/(2.0*a);
 rt2 = (-b - sqrt(discrim))/(2.0*a);
                        ^^^^

あなたのコンパイラはおそらくこれらの行に対してエラーメッセージを表示しました。

foo.c:25: error: expected ‘)’ before ‘;’ token

エラー メッセージを注意深く見て 25 行目を調べると、閉じ括弧が抜けていることがわかります。

于 2013-07-22T15:28:26.823 に答える
2

ここでいくつかの括弧を逃しました:

rt1 = (-b + sqrt(discrim)/(2.0*a);
rt2 = (-b - sqrt(discrim)/(2.0*a);

次のようになります。

rt1 = (-b + sqrt(discrim))/(2.0*a);
rt2 = (-b - sqrt(discrim))/(2.0*a);

コンパイラの警告を確認できます。私のもの(g ++)は次のように出力します:

file.c:24:36: error: expected ‘)’ before ‘;’ token

24 はエラーをチェックする行番号で、36 はこの行の列番号です。

于 2013-07-22T15:31:39.467 に答える
1
  rt1 = (-b + sqrt(discrim)/(2.0*a);
  rt2 = (-b - sqrt(discrim)/(2.0*a);
                          ^

右閉じ括弧がありません。

于 2013-07-22T15:28:28.683 に答える
1

gcc でコーディングしている場合は、以下のコードを使用して数学ライブラリにリンクします

#include <stdio.h>
#include <math.h>

int main()
{
 float a, b, c, rt1= 0, rt2=0, discrim;
 //clrscr();
 printf("Welcome to the Quadratic Equation Solver!");
 //getch();
 printf("\nYour quadratic formula should be of the form a(x*x)+bx+c = 0");
 printf("\nPlease enter a\'s value:");
 scanf("%f", &a);
 printf("Great! Now enter b\'s value:");
 scanf("%f", &b);
 printf("One more to go! Enter c\'s value:");
 scanf("%f", &c);
 discrim = b*b - 4*a*c;
 if (discrim < 0)
  printf("\nThe roots are imaginary.");
 else
 {
  rt1 = (-b + sqrt(discrim))/(2.0*a);
  rt2 = (-b - sqrt(discrim))/(2.0*a);
  printf("\nThe roots have been calculated.");
  // getch();
  printf("\nThe roots are:\nRoot 1:%f\nRoot 2:%f",rt1, rt2);
  // getch();
  printf("\nThank you!");
  //getch();
 }
}

このようにコンパイルします

gcc example.c -o example -lm
于 2013-07-22T15:37:00.413 に答える