协助C计划:解决Delta问题

时间:2013-11-07 01:38:49

标签: c output

我需要在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.

我只输入1,为a,然后它吐出了主要方法的剩余部分。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

并确保删除这些名称的任何局部变量声明,以便它们不会隐藏传入的声明(或导致编译错误)。

答案 2 :(得分:0)

我认为问题来自于1被识别为int而不是浮点数。

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