繁体   English   中英

声明,定义和调用

[英]Declarations, Definitions and Calls

我需要使用此程序更好地理解函数定义,声明和正确的调用。 我真的需要了解如何使用它们。 您能告诉我正确编写和解释这三个程序的正确方法吗?

#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;

我只是在这里解决了这个确切的问题:(我想这是作业的一部分)

https://stackoverflow.com/a/19826495/1253932

还要看代码..您永远不会使用函数四边形方程..也没有定义函数的类型(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);

}

编辑:

我从上述代码中获得了稍微优化和更好的功能的代码:

http://pastebin.com/GS65PvH6

编辑2:

从您的评论中,您尝试执行以下操作:

printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);

如果您输入以下内容,则将失败: 121

怎么一回事,因为scanf函数将读取整个121a ,它会什么都没有了bc (而它会把\\ n(进入)进入B和未定义为C)

所以以我在代码中使用过的方式使用scanf

好的-这充满了问题! 我试图指出它们,并显示“更好”的外观。 我希望这有帮助。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM