2017-06-07 350 views
0

我使用二次公式找到函數根的程序如下。它完美的作品。但是,如果不在全球範圍內定義三個變量,我就無法實現它。根據我的項目描述,我不應該這樣做。局部變量的全局變量

有關如何在本地定義它們以及在打印結果函數能夠這樣做之前計算不會丟失的任何建議或更改?

#include <stdio.h> 
#include <math.h> 

double discriminant; 
double root_one = 0, root_two = 0; 
double a = 0, b = 0, c = 0; 

int checkComplex(double a, double b, double c) 
{ 
    discriminant = (b * b) - 4 * (a * c); 

    if (discriminant == 0) 
     return 2; 

    else if (discriminant > 0) 
     return 1; 

    else 
     return 0; 
}// end checkComplex 

void calculateRoots(double a, double b, double c) 
{ 
    root_one = (-b + sqrt(discriminant))/(2 * a); 
    root_two = (-b - sqrt(discriminant))/(2 * a); 
} // end calculateRoots 

void getData() 
{ 
    printf("Enter a: "); 
    scanf("%lf", &a); 

    printf("\nEnter b: "); 
    scanf("%lf", &b); 

    printf("\nEnter c: "); 
    scanf("%lf", &c); 
}// end getData 

void printResults() 
{ 
    if (checkComplex(a, b, c) == 1) 
    { 
     calculateRoots(a, b, c); 
     printf("\n\n-----------------------------------------\n"); 
     printf("\nThe quantity (b^2-4ac) is %.2lf", discriminant); 
     printf("\n\nfirst root = %.2lf\nsecond root = %.2lf\n\n", root_one, 
       root_two); 
    }// if discriminant is 1 


    else if (checkComplex(a, b, c) == 0) 
    { 
     printf("\n\n-----------------------------------------\n"); 
     printf("The discriminant (b^2-4ac) is negative (imaginary)"); 
     printf("\nTherefore, the roots are complex\n"); 
    } // if discriminant is 0 

    else if (checkComplex(a == 2, b == 2, c == 2)) 
    { 
     calculateRoots(a, b, c); 
     printf("\n\n-----------------------------------------\n"); 
     printf("\nThe quantity (b^2-4ac) is %.2lf", discriminant); 
     printf("\n\nfirst root = %.2lf\nsecond root = %.2lf\n\n", root_one, 
       root_two); 
    }// if discriminant is greater than 1 
} // end printResults 

int main() 
{ 
    getData(); 
    printResults(); 
    return 0; 
} // End program 
+0

在main中定義它們,然後通過引用而不是按值傳遞。 –

+2

'checkComplex(a == 2,b == 2,c == 2)'.... huh? – ikegami

+0

可能重複的[調用程序中看不到的變量變量?](https://stackoverflow.com/questions/27320240/variable-changed-in-function-not-seen-by-caller) – Sebivor

回答

2

如果您需要返回多個值,您可以返回一個結構,或者您可以接受指向結果存儲位置的指針。我在以下解決方案中使用了兩種方法:

#include <stdio.h> 
#include <math.h> 

typedef struct { 
    double discriminant; 
    char num_roots; 
    double roots[2]; 
} roots_t; 

void getData(double* ap, double* bp, double* cp) { 
    printf("Enter a: "); scanf("%lf", ap); 
    printf("Enter b: "); scanf("%lf", bp); 
    printf("Enter c: "); scanf("%lf", cp); 
} 

roots_t calculateRoots(double a, double b, double c) { 
    roots_t roots; 
    roots.discriminant = (b*b) - 4 * (a*c); 
    roots.num_roots = 0; 
    if (roots.discriminant >= 0) { 
     roots.roots[roots.num_roots++] = (-b + sqrt(roots.discriminant))/(2 * a); 
     if (roots.discriminant > 0) 
     roots.roots[roots.num_roots++] = (-b - sqrt(roots.discriminant))/(2 * a); 
     } 
    } 

    return roots; 
} 

void printResults(double a, double b, double c, roots_t roots) { 
    if (roots.num_roots == 2) { 
     printf("The quantity (b^2-4ac) is %.2lf\n", roots.discriminant); 
     printf("roots = %.2lf, %.2lf\n", roots.roots[0], roots.roots[1]); 
    } 
    else if (roots.num_roots == 1) { 
     printf("The quantity (b^2-4ac) is %.2lf\n", roots.discriminant); 
     printf("roots = %.2lf\n", roots.roots[0]); 
    } 
    else { 
     printf("The discriminant (b^2-4ac) is negative\n"); 
     printf("roots = <complex>\n"); 
    } 
} 

int main(void) { 
    double a, b, c; 
    getData(&a, &b, &c); 
    printResults(a, b, c, calculateRoots(a, b, c)); 
    return 0; 
}