在函数中调用文件中的变量

时间:2013-12-08 02:52:32

标签: c file function

好的,这个问题可能太长了,但是我已经达到了一个我真的不确定如何完成它的地步。我已经完成了大部分工作,但我无法弄清楚如何在函数中调用变量。

fraction.c:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h> 
#include "Fraction.h"

int gcd(int a, int b)
{
   if (a == 0 && b == 0) {
      printf("Illegal args to gcd: %d, %d\n",a,b);
      exit(1);
   }
   int aa = abs(a);
   int bb = abs(b);
   if (aa == 0)
      return bb;
   if (bb == 0)
      return aa;
   return gcd(bb,aa%bb);
}

Fraction string_to_fraction(const char *S)
{
Fraction result = {0,1};
/* Here I'm not sure how to initialize the fraction result as specified in the
string S. I know I'm supposed to use sscanf but I'm not sure how. */
return result;
}

void fraction_to_string(Fraction R,char repr[])
{
repr[0] = 0;
/* I want to place the string representation of the Fraction R in the character
array repr using sprintf, but nothing I try is working */
}


return result;
}

fraction.h:

#include <stdio.h>

#ifndef __FRACTIONH__
#define __FRACTIONH__

typedef struct fraction {
      int numer;
      int denom;
} Fraction;

 /* 
 * Declares a Fraction and, using the string as if it were stdin or a file
 * inputs the data to initialize the Fractions.  It returns that Fraction
 */
Fraction string_to_fraction(const char *S);


 /*Fills repr with the string that would be used to print the Fraction */
void fraction_to_string(Fraction R, char repr[]);

#endif

main.c中:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tests.h"

int getChoice()
{
   int n;

   printf("Please enter a function testing choice\n");
   printf("Enter 0 to test all of the functions\n");
   printf("Enter 1 to test string_to_fraction\n");
   printf("Enter 2 to test fraction_to_string\n");
   printf("Enter 3 to test compare_fractions\n");
   printf("Enter 4 to test reduce_fraction\n");
   printf("Enter 5 to test add_fraction\n");

   scanf("%d",&n);
   printf("\n");
   return n;
}

int main()
{
   FILE *errF = NULL;
   FILE *reportF = NULL;
   char rnom[20] = "TestResults_";
   char enom[20] = "ErrorFile_";
   char errBuffer[2000] = "\nFraction Test Errors\n";
   int choice;
   const char *choice_str[6] ={"all","stof","ftos","compare","reduce","add"};

   printf("\nWelcome to the Fractions Module Test Program\n\n");
   printf("Tests results can be found in file TestResults\n");
   printf("and error reports in file errorFile\n\n");

   choice = getChoice();
   while(choice < 0 || choice > 5) {
     printf("\nInvalid choice; must be between 0 and 5, inclusive\n");
     choice = getChoice();
   }

   strcat(rnom,choice_str[choice]);
   strcat(enom,choice_str[choice]);

   if ((reportF = fopen(rnom,"w")) == NULL) {
      printf("Unable to open TestResults for writing\n");
      exit(1);
   }

   fprintf(reportF,"\nFractions Test Report\n");

   if (choice == 1 || choice == 0)
      fprintf(reportF,"string_to_fraction: %s\n",test_stof(errBuffer)? "pass" : "fail");
   if (choice == 2 || choice == 0)
      fprintf(reportF,"fraction_to_string: %s\n",test_ftos(errBuffer)? "pass" : "fail");
   if (choice == 3 || choice == 0)
      fprintf(reportF,"compare__fractions: %s\n",test_compare(errBuffer)? "pass" : "fail");
   if (choice == 4 || choice == 0)
      fprintf(reportF,"reduce_fraction: %s\n",test_reduce(errBuffer)? "pass" : "fail");
   if (choice == 5 || choice == 0)
      fprintf(reportF,"add_fraction: %s\n",test_add(errBuffer)? "pass" : "fail");

   fclose(reportF);

   if (strcmp(errBuffer,"\nFraction Test Errors\n") != 0) {
      printf("Errors found; consult TestResults and Error file\n");
      if ((errF = fopen(enom,"w")) == NULL) {
     printf("Unable to open errorFile for writing\n");
      }
      fprintf(errF,"%s",errBuffer);
      fclose(errF);
   } else 
      printf("No errors detected\n\n");

   printf("Normal termination\n\n");

   return 0;
}

tests.h:

#ifndef _TESTSH_
#define _TESTSH_

#include "Fraction.h"

int test_stof( char *);
int test_ftos( char *);
int test_compare( char *);
int test_reduce( char *);
int test_add( char *);

#endif

1 个答案:

答案 0 :(得分:0)

您需要一个包含main fraction.h的{​​{1}}文件。这是一个例子:

#include "fraction.h"

int main(int argc, char *argv[])
{
    Fraction fraction;

    fraction = string_to_fraction("3.14");

    return 0;
}

您的功能可以这样定义:

Fraction string_to_fraction(const char *S)
{
    Fraction result;
    float fl = strtof(S, NULL); // Convert string to float

    /* Convert decimal to fraction and assign to result */

    return result;
}

void fraction_to_string(Fraction R,char repr[])
{
    float fl = (float)R.numer / R.denom;

    /* Convert fl to string and copy to repr[] */
}