协助C程序:解决Delta [英] Assistance with C Program: Solving for Delta

查看:107
本文介绍了协助C程序:解决Delta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用C编写一个程序,该程序采用3个系数a,b,c,然后求解Delta.然后,它使用Delta并决定要发送什么函数来确定其输出.

I need to make a program in C that takes 3 coefficients, a, b, c, then solves for Delta. It then takes Delta and decides what Function to send it to determine it's output.

  /* 
  *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

运行它时,得到以下输出:

When I run it, I get the following Output:

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输入1,然后它退出主要方法的其余部分.

I only input 1, for a, and then it spits outt he remainder of the main method.

推荐答案

首先跳出的几件事:

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也是错误的..将其更改为:

your printf is also wrong .. change it to this:

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


固定代码:(请问您是否对任何部分有疑问)


Fixed code: ( ask me if you have questions about any part )

 #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

这篇关于协助C程序:解决Delta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆