銀行口座に振り込み、引き出しを計算し出力させるプログラミングです。
txtファイルを読み取って計算します。
ほぼ完成しているのですが、私のアウトプットと教科書のアウトプットの答えが少し違うのです。
具体的にアウトプットのDepositとCheckの部分が違います。
わかる方いますか?気になったところを指摘いただけたらと思います。
コード #include <stdio.h> #include <stdlib.h> FILE *fp, *csis; void outputHeaders(void); void initialBalance(double amount, double *balance, double *service, double *openBalance); void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit); void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck); void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance); int main(void) { char code; double amount, service, balance; double amtCheck, amtDeposit, openBalance, closeBalance; int numCheck, numDeposit; if (!(fp = fopen("account.txt", "r"))) { printf("account.txt could not be opened for input."); exit(1); } if (!(csis = fopen("csis.txt", "w"))) { printf("csis.txt could not be opened for output."); exit(1); } amount = 0; service = 0; balance = 0; amtCheck = 0; amtDeposit = 0; openBalance = 0; closeBalance = 0; numCheck = 0; numDeposit = 0; outputHeaders(); while (!feof(fp)) { fscanf(fp, "%c %lf\n", &code, &amount); if (code == 'I') { initialBalance(amount, &balance, &service, &openBalance); } else if (code == 'D') { deposit(amount, &balance, &service, &numDeposit, &amtDeposit); } else { check(amount, &balance, &service, &numCheck, &amtCheck); } } closeBalance = balance - service; outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance); fclose(csis); fclose(fp); return 0; } void outputHeaders() { printf("Transaction\t\t Deposit\t\t Check\t\t Balance\n"); printf("-----------\t\t -------\t\t -----\t\t -------\n"); fprintf(csis, "Transaction\t\t Deposit\t\t Check\t\t Balance\n"); fprintf(csis, "-----------\t\t -------\t\t -----\t\t -------\n"); } void initialBalance(double amount, double *balance, double *service, double *openBalance) { *openBalance = amount; *balance = *openBalance; *service = 3.00; printf("Initial Balance\t \t \t \t \t\t\t\t%8.2lf\n", *openBalance); fprintf(csis, "Initial Balance\t \t \t \t \t\t\t\t%8.2lf\n", *openBalance); } void deposit(double amount, double *balance, double *service, int *numDeposit, double *amtDeposit) { *balance += amount; *amtDeposit += amount; *numDeposit += 1; *service += 0.03; printf("Deposit\t\t\t %8.2lf\t \t\t\t\t%8.2lf\n", *amtDeposit, *balance); fprintf(csis, "Deposit\t\t\t %8.2lf\t \t\t\t\t%8.2lf\n", *amtDeposit, *balance); } void check(double amount, double *balance, double *service, int *numCheck, double *amtCheck) { *balance -= amount; *amtCheck += amount; *numCheck += 1; if (*balance < 0.00) { *service += 5.00; } { *service += 0.06; } printf("Check\t\t\t\t\t\t %9.2lf\t\t%8.2lf\n", *amtCheck, *balance); fprintf(csis, "Check\t\t\t\t\t\t %9.2lf\t\t%8.2lf\n", *amtCheck, *balance); } void outputSummary(int numDeposit, double amtDeposit, int numCheck, double amtCheck, double openBalance, double service, double closeBalance) { printf("\nTotal number deposits: %d\n", numDeposit); printf("Total amount deposits: %.2lf\n\n", amtDeposit); printf(" Total number checks: %d\n", numCheck); printf(" Total amount checks: %.2lf\n\n", amtCheck); printf(" Total service charge: %.2lf\n\n", service); printf(" Opening balance: %.2lf\n", openBalance); printf(" Closing balance: %.2lf\n", closeBalance); fprintf(csis, "\nTotal number deposits: %d\n", numDeposit); fprintf(csis, "Total amount deposits: %.2lf\n\n", amtDeposit); fprintf(csis, " Total number checks: %d\n", numCheck); fprintf(csis, " Total amount checks: %.2lf\nn", amtCheck); fprintf(csis, " Total service charge: %.2lf\n\n", service); fprintf(csis, " Opening balance: %.2lf\n", openBalance); fprintf(csis, " Closing balance: %.2lf\n", closeBalance); }
txtファイル
I 478.83
D 127.45
D 619.84
C 945.12
C 4.76
D 32.81
C 1.06
D 184.32
C 495.18
C 141.81
C 255.10
D 250.00
D 123.88
D 245.05
D 873.25
C 981.12
D 317.84
C 812.73
D 606.12
私のアウトプット
Transaction Deposit Check Balance
Initial Balance 478.83
Deposit 127.45 606.28
Deposit 747.29 1226.12
Check 945.12 281.00
Check 949.88 276.24
Deposit 780.10 309.05
Check 950.94 307.99
Deposit 964.42 492.31
Check 1446.12 -2.87
Check 1587.93 -144.68
Check 1843.03 -399.78
Deposit 1214.42 -149.78
Deposit 1338.30 -25.90
Deposit 1583.35 219.15
Deposit 2456.60 1092.40
Check 2824.15 111.28
Deposit 2774.44 429.12
Check 3636.88 -383.61
Deposit 3380.56 222.51
Total number deposits: 10
Total amount deposits: 3380.56
Total number checks: 8
Total amount checks: 3636.88
Total service charge: 23.78
Opening balance: 478.83 Closing balance: 198.73
答えアウトプット
Transaction Deposit Check Balance
Initial Balance 478.83
Deposit 127.45 606.28
Deposit 619.84 1226.12
Check 945.12 281.00
Check 4.76 276.24
Deposit 32.81 309.05
Check 1.06 307.99
Deposit 184.32 492.31
Check 495.18 -2.87
Check 141.81 -144.68
Check 255.10 -399.78
Deposit 250.00 -149.78
Deposit 123.88 -25.90
Deposit 245.05 219.15
Deposit 873.25 1092.40
Check 981.12 111.28
Deposit 317.84 429.12
Check 812.73 -383.61
Deposit 606.12 222.51
Total number deposits: 10
Total amount deposits: 3380.56
Total number checks: 8
Total amount checks: 3636.88
Total service charge: 23.78
Opening balance: 478.83 Closing balance: 198.73
回答1件
あなたの回答
tips
プレビュー