簡體   English   中英

從C中的文件讀取的行中獲取多個單獨的輸入

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

基本上我有一些問題。

我要解決的問題涉及一個“銀行程序”,該程序從文本文件獲取其信息。 它讀取代碼的前五行(均為浮點數),並將每行用作起始余額。

接下來,下面的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;

}

這些是錯誤的

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

....

你的意思是

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

您可以使用數組提取類似的帳戶:

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中包含400,並且您從第一個帳戶開始,即帳戶0,開始,則您可以將帳戶號和金額傳遞給交易功能,例如“ 2 W 200”:

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

注意:最好每個帳戶只有一個結余,除非您(如前所述)在開始之前需要知道結余是多少。 因為對於同一帳戶上的任何其他交易,您都希望使用newAcnts [accountNum]來擁有正確的帳戶余額。 或者,在開始任何交易之前,將原始余額復制到新帳戶陣列中,然后僅在newAcnts陣列上進行交易。 希望這是有道理的:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM