0

3 つの係数 a、b、c を使用してデルタを解くプログラムを C で作成する必要があります。次に、Delta を受け取り、どの関数を送信して出力を決定するかを決定します。

  /* 
  *Program Name: COP 2220-10018 Project 4
  *
  * Author: Nathan Gamble
  * 
  * Description: Find Delta, solve for roots.
  *
  * Input: Coefficients a, b, c.
  *
  * Output: Roots
  */
 #include <stdio.h>
 #include <math.h>

 int main (void)                        


    {
//Local Declarations
float a;
float b;
float c;
float delta;


//Statements
printf("Input coefficient a.\n");
scanf("%.2f", &a);
printf("Input coefficient b.\n");
scanf("%.2f", &b);
printf("Input coefficient c.\n");
scanf("%.2f", &c);
printf("%fx^2 + %fx + %f\n", &a, &b, &c);

//Process
delta = (b * b) - (4 * a * c);

if (delta > 0) twoRoots(a, b, c, delta);
else if (delta = 0) oneRoot(a, b, c, delta);
else if (delta < 0) noRoots();

return;
} // End main


/* 
 *Program Name: COP 2220-10018 Project 4
 *
 * Author: Nathan Gamble
 * 
 * Description: To solve for the two roots.
 *
 * Input: None
 *
 * Output: Root one, Root two.
 */
#include <stdio.h>
#include <math.h>

int twoRoots ()
{
//Local Declarations
float xOne;
float xTwo;
float delta;
float deltaRoot;
float a;
float b;

printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", &xOne);
printf("%.2f", &xTwo);

return;
} // End twoRoots


/* 
 *Program Name: COP 2220-10018 Project 4
 *
 * Author: Nathan Gamble
 * 
 * Description: To solve for the one root.
 *
 * Input: None
 *
 * Output: Root one.
 */
#include <stdio.h>
#include <math.h>

int oneRoot ()
{
//Local Declarations
float xOne;
float xTwo;
float deltaRoot;
float a;
float b;

printf("There is exactly one distinct root./n");
xOne = -b / (2*a);
printf("%.2f", &xOne);


return;
} // End oneRoot


/* 
 *Program Name: COP 2220-10018 Project 4
 *
 * Author: Nathan Gamble
 * 
 * Description: To inform the roots are complex.
 *
 * Input: None
 *
 * Output: Statement.
 */
#include <stdio.h>
#include <math.h>

int noRoots ()
{
//Local Declarations

printf("There are two distinct complex roots./n");

return;
} // End noRoots

実行すると、次の出力が得られます。

Input coefficient a.
1
Input coefficient b.
Input coefficient c.
0.000000x^2 + 882156984598706310000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000.000000x + 0.000000

Process returned 16384 (0x4000)   execution time : 10.641 s
Press any key to continue.

a に 1 を入力するだけで、main メソッドの残りの部分が吐き出されます。

4

3 に答える 3

0

最初に飛び出すいくつかのこと:

printf("Input coefficient a.\n");
scanf("%f", &a);     // you were scanning for 0.2f .. any reason why?
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);

あなたprintfも間違っています..これに変更してください:

printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c); // you were printing the addresses of a,b,c .. printf just needs the name of variables not their addresses

上記の変更を行った後の出力:

$ ./test
Input coefficient a.
1.5
Input coefficient b.
2.5
Input coefficient c.
3.5
1.50x^2 + 2.50x + 3.50

修正コード: (何か質問があれば聞いてください)

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

// function declarations

void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);

 int main (void)                        
    {
    //Local Declarations
    float a;
    float b;
    float c;
    float delta;

    float solution;

    printf("Input coefficient a.\n");
    scanf("%f", &a);
    printf("Input coefficient b.\n");
    scanf("%f", &b);
    printf("Input coefficient c.\n");
    scanf("%f", &c);
    printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);

    delta = (float)(b*b) - (float)(4.0 * a * c);

    printf("delta = %0.2f\n",delta);

    if (delta > 0){
         twoRoots(a,b,delta);
    }else if (delta == 0) {
         oneRoot(a,b,delta);
    }else if (delta < 0.0){
         printf("There are no real roots\n");
    }

    return 0;
} 

void twoRoots (float a,float b,float delta)
{

    float xOne;
    float xTwo;

    float deltaRoot;

    printf("There are two distinct roots.\n");
    deltaRoot = sqrt(delta);
    xOne = (-b + deltaRoot) / (2*a);
    xTwo = (-b - deltaRoot) / (2*a);
    printf("%.2f", xOne);
    printf("%.2f", xTwo);
} 



void oneRoot(float a,float b,float delta)
{

    float xOne;
    float xTwo;
    float deltaRoot;

    printf("There is exactly one distinct root\n");
    xOne = -b / (2*a);
    printf("%.2f", xOne);

}

出力 1:

$ ./test
Input coefficient a.
1.1
Input coefficient b.
5.5
Input coefficient c.
2.2
1.10x^2 + 5.50x + 2.20
delta = 20.57
There are two distinct roots.
-0.44-4.56

出力 2:

$ ./test
Input coefficient a.
1
Input coefficient b.
4
Input coefficient c.
4
1.00x^2 + 4.00x + 4.00
delta = 0.00
There is exactly one distinct root
-2.00

出力 3:

$ ./test
Input coefficient a.
1
Input coefficient b.
3
Input coefficient c.
9
1.00x^2 + 3.00x + 9.00
delta = -27.00
There are no real roots

私はコードを最適化し、ここでそれをより効率的にしました:

http://pastebin.com/GS65PvH6

于 2013-11-07T01:47:01.900 に答える
0

あなたの差し迫った問題はここにあります:

scanf ("%.2f", &a);

スキャンする値に長さリミッターを設定できますが、とにかく入力内容を制限しようとするべきではありません。

いずれにせよ、.2使用しているオプションは には無効です。これは、出力の精度を制御scanfするものです。printf

ISO 標準では、「最大フィールド幅 (文字数) を指定するゼロより大きいscanfオプションの 10 進整数」が必要であると述べられています。したがって、小数点以下の桁数を制限するためだけに を使用する方法はありません。 scanf

代わりにこれを使用してください:

scanf ("%f", &a);

他のscanf呼び出しを含みます。


さらに問題がいくつかありますが、そのうちのいくつかを以下に示します。あなたの質問に固有の問題はscanfフォーマット文字列であるため、完全なリストは提供していません。

まず、これらの変数のアドレスではなく値を出力します。

printf ("%fx^2 + %fx + %f\n", a, b, c);

次に、変数a/b/c/deltaを関数に渡しますが、変数を受け取りません。次のように宣言する必要があります。

int twoRoots (float a, float b, float c, float delta)

また、これらの名前のローカル変数宣言をすべて削除して、渡されたものを非表示にしないようにします (またはコンパイル エラーを引き起こします)。

于 2013-11-07T01:47:33.767 に答える
0

問題は、1 が float ではなく int として認識されることにあると思います。

%.2fscanfを記述するとき、float を入力する必要があります。何か他のものを検出すると、失敗し、man ページで指定されている他の scanf 要求を読み取りません。

于 2013-11-07T01:48:59.797 に答える