全局变量到局部变量

时间:2017-06-07 04:12:23

标签: c algorithm computer-science

我使用二次公式查找函数根的程序如下。它完美地运作。但是,如果不在全球范围内定义三个变量,我无法使其工作;根据我的项目描述,我不应该这样做。

有关如何在本地定义它们并且在打印结果功能能够执行之前计算不会丢失的任何建议或更改?

#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

1 个答案:

答案 0 :(得分: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;
}