【发布时间】: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