gpt4 book ai didi

c - C 程序协助 : Solving for Delta

转载 作者:行者123 更新时间:2023-11-30 17:44:50 34 4
gpt4 key购买 nike

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

最佳答案

首先跳出的几件事:

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

关于c - C 程序协助 : Solving for Delta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826410/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com