0

我需要用 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 方法的其余部分。

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的可选十进制整数,指定最大字段宽度(以字符为单位)”。所以没有办法使用限制小数点后允许的位数。 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 被识别为 int 而不是 float 的事实。

当您编写%.2fscanf 时,希望您输入一个浮点数。如果它检测到其他内容,它将失败并且不会读取手册页中指定的任何其他 scanf 请求。

于 2013-11-07T01:48:59.797 回答