货币转换数据库结构来自文本文件

时间:2014-06-08 01:08:07

标签: c

我一直在研究一个读取不同货币文件的数据结构程序,然后我将其用于转换。我已经连续几天跑了过来,并尝试了fgetsfscanfs等等,而我现在只是因为我对编程很陌生而迷失了。

数据。文件在如下的单独行中列出:

dollar 1.00

yen 0.0078

franc 0.20

mark 0.68

pound 1.96

到目前为止我的代码:

  typedef struct {
   string currency;
   double rate;
   } currencyT;

  typedef struct {
   currencyT cur[MaxCurrencyTypes];
   int nCurrency;
   } *currencyDB;


  static currencyDB ReadDataBase(void);  
  static ReadOneLine(FILE *infile, currencyDB db);  

  static currencyDB ReadDataBase(void)
 {
   FILE *infile;
   currencyDB db;
      int nCurrency;

   db = New(currencyDB);
   infile = fopen(ExchangeFile, "r");
       while (ReadOneLine(infile, db));
   fclose(infile);
   return(db);
 }

  static ReadOneLine(FILE *infile, currencyDB db) 
 {
   currencyT cur;
   char termch, currency;
   double rate;
   int nscan, nCurrency;


   nCurrency = 0;

   while(1) {
       nscan = fscanf(infile, "%20s %f%c", db->cur[nCurrency].currency, 
  &db->cur[nCurrency].rate, &termch);
           if(nscan = EOF) break;
           if(nscan != 3 || termch != '\n') {
               Error("Improper file format");
               }
           nCurrency++;
           }
       db->nCurrency = nCurrency;  
   }


 static void ProcessExchange(currencyDB db)
 {

 }



 main()
 {
   currencyDB currencies;

   currencies = ReadDataBase();

   ProcessExchange(currencies);


 }

1 个答案:

答案 0 :(得分:0)

见8&首先是9。

  1. MaxCurrencyTypes未声明,我们假设

    #define MaxCurrencyTypes (5)
    
  2. string未声明,我们假设

    typedef char * string;
    
  3. currencyDB是指针类型,建议改为声明为结构类型

    typedef struct {
      currencyT cur[MaxCurrencyTypes];
      int nCurrency;
    } currencyDB;  // Drop *
    
  4. static ReadOneLine()应使用显式返回类型。

    static int ReadOneLine()
    
  5. int nCurrency;未在ReadDataBase()中使用。

  6. {li>

    New()db = New(currencyDB)未声明或定义。假设为*db分配未初始化的内存。

  7. ExchangeFile未声明。

  8. if (nscan = EOF) - > if (nscan == EOF)

  9. ReadOneLine()需要返回一个值。

  10. main()应明确说明返回类型和参数。

    int main(void)