我需要更好地理解使用该程序的函数定义,声明和正确的调用。我真的需要了解如何使用它们。你能告诉我用这三个正确的解释来编写这个程序的正确方法吗?
#include <stdio.h>
#include <math.h>
quad_equation(float a, float b, float c);
int main()
{
float a, b, c, determinant, r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) { r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else (determinant<0);
{
printf("Both roots are complex");
}
return 0;
答案 0 :(得分:0)
我刚刚在这里解决了这个问题:(我想这是作业的一部分)
https://stackoverflow.com/a/19826495/1253932
同时查看你的代码..你从不使用函数quad方程..你还没有定义函数的类型(int / void / float / char)等。
为了方便:(这是整个代码) - 问我是否有任何理解
#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);
}
修改强>
我使用上面提到的代码编写的稍微优化且功能更强的代码:
<强> EDIT2:强>
根据您的评论,您尝试这样做:
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
如果您输入以下内容,则会失败:121
Beacuse scanf将整个121
读入a
,而b
,c
没有任何内容(而是将\ n(输入)放入b和未定义为c)
所以使用scanf就像我在代码中使用它一样
答案 1 :(得分:0)
好的 - 这充满了问题!我试图指出它们,并展示“更好”的样子。我希望这会有所帮助。
quad_equation(float a, float b, float c);
这可能是一个“功能原型”。原型告诉编译器“我稍后将使用此函数,这是它需要调用的方式,以及它返回的类型”。您没有指定返回类型;可能你想用int
来说明你是否找到了根,并在函数中打印出结果。更好的方法是将两个返回值的空间作为参数传递:
int quad_equation(float a, float b, float c, float* x1, float* x2);
现在我们可以使用main
程序来获取输入/输出,让函数解决问题:
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}