【问题标题】:Take multiple separate inputs from lines read in from file in C从 C 中从文件中读取的行中获取多个单独的输入
【发布时间】:2015-03-13 20:03:11
【问题描述】:

基本上我有几个问题。

我试图解决的问题涉及从文本文件中获取信息的“银行程序”。它读取前五行代码(都是浮点数)并将每行代码用作起始余额。

接下来,接下来的 1-5 行代码每行都以一个字符开头,表示它影响的帐户。接下来是一个字母,表示提款或存款等。接下来是将在操作中使用的金额。我必须使用多种功能。

这是我目前所拥有的,我知道在我处理它时,伪代码中留下了一些空格。

  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;

}

【问题讨论】:

  • 它是如何(不)工作的?
  • 看起来是一个好的开始。你到底要什么?
  • @BinaryJudy,我想我不确定如何接受不同类型的输入,然后执行之后的操作。
  • @Stephen Rosedale 你还在苦苦挣扎吗?我不清楚文本文件是什么样的,你能举个例子吗?您有五个余额和五个“交易”?
  • 是的,我在尝试找到解决方案时运气不佳,甚至不知道如何实施它。一个例子是“100 400 500 600 800 2 W 200 4 D 500 33 W 150 0”我很抱歉我不能让它在单行中发布。 @BinaryJudy

标签: c function


【解决方案1】:

这些都是错误的

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

....

你是说

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

【讨论】:

  • 是的,这就是我留下的伪代码。我不确定如何获取信息才能达到这一点。
【解决方案2】:

您可以像这样使用数组提取帐户:

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]);

如果您需要保留原始余额以供将来参考,您可以使用第二个数组来保存新余额:

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

显示原始账户余额:

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

扫描时可以跳过文件中的账户余额,像这样:

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

然后您可以按照相同的方法获取剩余的交易。获取交易参数并对其进行操作。使用数组而不是文件来访问余额。第一个帐户将是元素 0,并以在索引 4 中的帐户号 5 结束(从零开始)。所以你必须遵循这个模式。我这么说是因为交易是如何用帐号标记的(以 0 或 1 开头)。如果您使用从零开始,则可以更轻松地访问数组中的帐户余额。像这个例子“2 W 200”,如果账户 #2 有 400 并且您从第一个账户开始是账户 0,您可以将帐号和金额传递给您的交易函数,如下所示:

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

注意:除非(如前所述)您需要在开始之前知道它是什么,否则每个帐户只有一个流动余额可能会更好。因为对于同一帐户上的任何其他交易,您都希望使用 newAcnts[accountNum] 来获得正确的帐户余额。或者在开始任何交易之前将原始余额复制到新帐户数组中,并且只在 newAcnts 数组上进行交易。希望这是有道理的:)

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

【讨论】:

  • 希望对您有所帮助。
  • 非常感谢!!!我现在正在实施这些更改!稍后会告诉你进展如何!
  • 我添加了前两个数组,当我编译并运行时,我得到的只是“5 balance is 0.00”
  • @Stephen 好的,如果您遇到问题,请告诉我。我没有对此进行任何测试,但它应该是正确的或非常接近正确;)
  • @Stephen 看看这个link。它运行它的一部分。
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2021-05-07
  • 2011-12-23
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
相关资源
最近更新 更多