我需要用 C 语言编写一个程序,它需要 3 个系数 a、b、c,然后求解 Delta。然后它需要 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 方法的其余部分。