简体   繁体   English

从C中的文件读取的行中获取多个单独的输入

[英]Take multiple separate inputs from lines read in from file in C

Basically I have a few problems. 基本上我有一些问题。

The problem I am trying to solve involves a "banking program" that gets its info from a text file. 我要解决的问题涉及一个“银行程序”,该程序从文本文件获取其信息。 It reads the first five lines of code (which are all floats) and uses each as the starting balance. 它读取代码的前五行(均为浮点数),并将每行用作起始余额。

Next, the following 1-5 lines of code each begin with a single character indicating the account that it affects. 接下来,下面的1-5行代码均以一个字符开头,表示该代码所影响的帐户。 Next is a single letter indicating either withdraw or deposit, etc. Following that is the amount that will be used in the action. 接下来是单个字母,指示提款或存款等。其后是该操作将使用的金额。 I have to use multiple functions. 我必须使用多种功能。

Here's what I have so far, I know some spaces have been left in pseudocode while I'm working on it. 到目前为止,这就是我所知道的,我知道在进行伪代码处理时,在伪代码中还留有一些空格。

  float balance();
float withdraw();
float deposit();
float update();

#define INTEREST 3.5;
int main(){

//initializing variables
int count =0,count2=0,acctNum=1,acct1,acct2,acct3,acct4,acct5,i=0;
float orgBalance, balance;
char activity,B,W,U,D;

//opening files
FILE *input;
input = fopen("bankfile.txt","r");

//error checking that file opened correctly
if (input == NULL){
    fprintf(stderr, "Can't open input file!\n");
    exit(1);
}   
printf("Account          Balance\n");
printf("------------------------\n");
while(count<5){
    fscanf(input,"%f",&orgBalance);

    //printf("%d               %.2f\n",acctNum,orgBalance);
    //acctNum++;
    i++;
    count++;
    printf("%d balance is %.2f\n",i,orgBalance);
}
printf("------------------------\n");


while(fscanf(input, "%d ",&i)!=EOF){
//while(count2<5)
    fgetc(i);   


    switch (activity){
        case 'B':
            balance;
            ;
            break;
        case 'W':
            withdraw;
            ;
            break;
        case 'D':
            deposit;
            ;
            break;
        case'U':
            update;
            ;
            break;


    }
    count2++;
}


system("pause");
return 0;
}


 float balance(){
    float bal;
    return bal; 
  }
  float withdraw(){
    float bal1, bal2;
    if(bal1>bal2){
        return bal1 - bal2;
    }
    else{
        printf("Sorry, you can't withdraw that much");
        return bal1; 


}
}
float deposit(){
    float bal1,bal2;
    return bal1 += bal2;
}
float update(){
    float bal;
    return bal*= INTEREST;

}

these are wrong 这些是错误的

switch (activity){
    case 'B':
        balance;
        ;

.... ....

you mean 你的意思是

switch (activity){
    case 'B':
        balance(); <<<======
        ;

You can extract the accounts like this using an array: 您可以使用数组提取类似的帐户:

float ogAcnts[5] = {0,0,0,0,0}; // original account balances initialized to zero
fscanf(input, "%f%f%f%f%f", &ogAcnts[0], &ogAcnts[1], &ogAcnts[2], &ogAcnts[3], &ogAcnts[4]);

You can have a second array with the new balances if you need to keep the original balances for future reference: 如果需要保留原始余额以供将来参考,则可以使用第二组数组来存储新余额:

float newAcnts[5] = {0,0,0,0,0}; // initialized to zero, could also be replaced with the original balances

To display the original account balances: 要显示原始帐户余额:

// you can fill in the rest
for(int i=0; i<=4; i++)
{
    printf("%d balance is %.2f\n",i,ogAcnts[i]);
}  

You can skip the account balances in the file when scanning, like this: 您可以在扫描时跳过文件中的帐户余额,如下所示:

fscanf(input+5, %d%c%f, &acountNum, &transactionType, &amount); // note these variables don't exist in your code but it's an example of how you could use them

You can then follow this same method to get the remaining transactions. 然后,您可以按照相同的方法来获取剩余的交易记录。 Get the transaction parameters and act on them. 获取交易参数并对其执行操作。 Use the arrays to access the balances instead of the file. 使用数组访问余额而不是文件。 The first account will be element 0 and ends with account number 5 being in index 4 (zero based). 第一个帐户将是元素0,并以索引号4(从零开始)结束的帐户号5结束。 So you will have to follow this pattern. 因此,您将必须遵循这种模式。 I say that because of how the transactions are labeled with the account number (either starts with 0 or 1). 我之所以这样说,是因为交易是如何用帐号标记的(以0或1开头)。 if you are using zero based then it makes it easier to access that account balance in the array. 如果您使用基于零的值,则可以更轻松地访问数组中的帐户余额。 Like this example "2 W 200" if account #2 has 400 in it and you are starting with the first account being account 0, you can pass the account number and amount to your transaction functions like this: 像此示例一样,如果帐户#2中包含400,并且您从第一个帐户开始,即帐户0,开始,则您可以将帐户号和金额传递给交易功能,例如“ 2 W 200”:

...
newAcnts[accountNum] = ogAcnts[accountNum]-amount; // withdraw example using the variables from above

Note: It would probably be better to just have one running balance for each account unless (like mentioned) you need to know what it was before you started. 注意:最好每个帐户只有一个结余,除非您(如前所述)在开始之前需要知道结余是多少。 Because for any additional transactions on the same account you'd want to use newAcnts[accountNum] to have the correct balance of the account. 因为对于同一帐户上的任何其他交易,您都希望使用newAcnts [accountNum]来拥有正确的帐户余额。 Or before starting any transactions copy the original balances into the new accounts array and only do your transactions on the newAcnts array. 或者,在开始任何交易之前,将原始余额复制到新帐户阵列中,然后仅在newAcnts阵列上进行交易。 Hopefully that makes sense :) 希望这是有道理的:)

newAcnts[accountNum] = newAcnts[accountNum]-amount; // withdraw example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM