簡體   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