【问题标题】:Calling variables from file in functions在函数中从文件中调用变量
【发布时间】:2013-12-08 02:52:32
【问题描述】:

好吧,这个问题可能太长了,但我已经到了一个我真的不知道如何完成它的地步。我已经记下了大部分内容,但我不知道如何调用函数中的变量。

分数.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;
}

分数.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;
}

测试.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

【问题讨论】:

  • 这里其实没有问题。
  • 您说“我尝试的任何方法都不起作用。”向我们展示您的尝试。

标签: c file function


【解决方案1】:

您需要一个包含main 的文件,其中包含fraction.h。这是一个例子:

#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[] */
}

【讨论】:

  • 抱歉,我添加了 main.c。其余代码包括其他函数,例如比较、归约和添加,但我已经解决了它们,所以我从函数文件中删除了它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
相关资源
最近更新 更多