简体   繁体   English

声明,定义和调用

[英]Declarations, Definitions and Calls

i need to gain a better understanding of function definition, declarations and proper calls using this program. 我需要使用此程序更好地理解函数定义,声明和正确的调用。 I really need the understanding of how to use them. 我真的需要了解如何使用它们。 Could you show me the proper way to write this program with all three correct and explained? 您能告诉我正确编写和解释这三个程序的正确方法吗?

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

I just solved this exact question here : (I guess this is a part of an assignment ) 我只是在这里解决了这个确切的问题:(我想这是作业的一部分)

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

Also looking at your code .. you never use the function quad equation .. also you haven't defined the type of the function ( int/void/float/char) etc. 还要看代码..您永远不会使用函数四边形方程..也没有定义函数的类型(int / void / float / char)等

For ease: ( here is the entire code ) -- ask me if you don't understand anything 为简单起见:(以下是整个代码)-问我是否不懂

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

}

EDIT: 编辑:

A slightly more optimized and better functioning code that I made from the above mentioned code: 我从上述代码中获得了稍微优化和更好的功能的代码:

http://pastebin.com/GS65PvH6 http://pastebin.com/GS65PvH6

Edit2: 编辑2:

From your comments you try to do this: 从您的评论中,您尝试执行以下操作:

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

This will fail if you input something like this: 121 如果您输入以下内容,则将失败: 121

Beacuse scanf will read the whole 121 into a and it will have nothing for b , c ( rather it will put \\n(enter) into b and undefined into c ) 怎么一回事,因为scanf函数将读取整个121a ,它会什么都没有了bc (而它会把\\ n(进入)进入B和未定义为C)

So use the scanf the way I have used it in my code 所以以我在代码中使用过的方式使用scanf

OK - this is full of problems! 好的-这充满了问题! I attempt to point them out, and show what "better" looks like. 我试图指出它们,并显示“更好”的外观。 I hope this helps. 我希望这有帮助。

quad_equation(float a, float b, float c);

This is probably intended to be a "function prototype". 这可能打算成为“功能原型”。 A prototype tells the compiler "I am going to use this function later, and this is how it needs to be called, and the type it returns". 原型告诉编译器“我稍后将使用此函数,这就是调用它的方式以及返回的类型”。 You did not specify a return type; 您未指定返回类型; probably you want to use int to say whether you found roots or not, and print out the result in the function. 可能您想使用int表示是否找到了根,然后在函数中打印出结果。 Better would be to pass space for two return values as a parameter: 最好将两个返回值的空间作为参数传递:

int quad_equation(float a, float b, float c, float* x1, float* x2);

Now we can use the main program to get input/output, and let the function solve the problem: 现在我们可以使用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