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

标签 c file function

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

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

最佳答案

您需要一个包含 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[] */
}

关于在函数中调用文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449318/

相关文章:

c - 如何从 gcc 限制 C 指令集

javascript - 字符串中存在的实现函数

c - 为什么当头文件更改时 makefile 会重新编译所有内容?

c++ - 动态设置 for 循环的初始化、条件和 in/decrementation

c - 为什么 posix c 中的 regexec() 总是返回第一个匹配项,它如何返回所有匹配位置只运行一次?

c++ - 如何修复错误 "was not declared in this scope"?

function - Lisp 函数的返回参数

c++ - 如何获取与某个扩展关联的程序列表?

perl - 如何使用 Perl 的 File::Copy 复制名称中带有特殊字符的文件?

c - 为什么 BACKSPACE (\b) 不能按预期处理文件?