2017-06-07 20 views
0

二次式を使って関数の根を見つける私のプログラムは以下の通りです。それは完全に動作します。しかし、3つの変数をグローバルに定義することなく動作させることができませんでした。私のプロジェクトの記述によると、私はそうするべきではない。ローカル変数へのグローバル変数

ローカルで定義する方法や印刷結果の機能が失われないようにするための計算方法についての提案や変更はありますか?

#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

これらをメインで定義し、値ではなく参照渡しします。 –

+2

'checkComplex(a == 2、b == 2、c == 2)' ....ねえ? – 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; 
} 
関連する問題