をこれがすべてで処理いずれかがあったと仮定して、最終的にアカウントの詳細を印刷します。先に述べたように、これは、変更されたアカウント番号のために移行を行わずにファイルの最後に到達するために必要です。
(あなたはそれが宿題ではありません保証しなければ)私は私の実際のコードを投稿しませんが、次の転写物は、それらの変更は、あなたの現在の問題を修正するために十分でなければならないことを示しています。
$ cat clients.dat
1 Pax 4.2
2 Roger 0
$ cat transactions.dat
1 1/2/3 112.3
1 2/3/4 -22.1
2 3/4/5 7
$ ./qq
Account Name Balance
|----------------------------------|
1 Pax 94.40
2 Roger 7.00
価値があることについては、私は、トランスアーションファイルを主要な要素として扱うことに全く同意していません。あなたの仕事は(トランザクションファイルで指定されているように)必要な変更を加えてアカウントファイルをコピーすることなので、アカウントファイルをプライマリユニットとして処理したいと思っています。
それは私にはそんなにクリーンなようです。
そして、完全性のために、私が思いついたコードはここにあります。あなたのマークを減らす可能性のあるものがたくさんあるので、これを譲渡として伝えることはお勧めしません。実際の世界では問題はほとんどありませんが、教育者はボンネットに特定の蜂を持っています。 enum
の代わりに#define
を使用するなど、複数の戻り点があります。この出力は
1 1/2/3 112.3
1 2/3/4 -22.1
4 3/4/5 7
:
0 Mary 99.9
1 Pax 4.2
2 Bob 7
4 Roger 0
5 Nobody 9
とのtransactions.dat
:
+--------+------------+--------+--------+--------+
| Acct | Name | Date | Amount | Balnce |
+--------+------------+--------+--------+--------+
| 0 | Mary | | | 99.90 | <--
| | | | | 99.90 | -->
+--------+------------+--------+--------+--------+
| 1 | Pax | | | 4.20 | <--
| | | 1/2/3 | 112.30 | 116.50 |
| | | 2/3/4 | -22.10 | 94.40 |
| | | | | 94.40 | -->
+--------+------------+--------+--------+--------+
| 2 | Bob | | | 7.00 | <--
| | | | | 7.00 | -->
+--------+------------+--------+--------+--------+
| 4 | Roger | | | 0.00 | <--
| | | 3/4/5 | 7.00 | 7.00 |
| | | | | 7.00 | -->
+--------+------------+--------+--------+--------+
| 5 | Nobody | | | 9.00 | <--
| | | | | 9.00 | -->
+--------+------------+--------+--------+--------+
とclientupdate.dat
を作成するclients.dat
考える
#include <stdio.h>
#include <string.h>
#define HDR_LINE 0
#define HDR_FULL 1
static void outHeader (int withHeading) {
printf ("+-%-6s-+-%-10s-+-%-6s-+-%-6s-+-%-6s-+\n",
"------", "----------", "------", "------", "------");
if (withHeading)
printf ("| %-6s | %-10s | %-6s | %-6s | %-6s |\n",
" Acct ", " Name ", " Date ", "Amount", "Balnce");
}
static void closeFiles (FILE *pfCli, FILE *pfTxn, FILE *pfNew) {
if (pfCli) fclose (pfCli);
if (pfTxn) fclose (pfTxn);
if (pfNew) fclose (pfNew);
}
static int openFiles (FILE **pfCli, FILE **pfTxn, FILE **pfNew) {
*pfCli = *pfTxn = *pfNew = NULL;
if ((*pfCli = fopen ("clients.dat", "r")) == NULL) {
printf ("File clients.dat could not be opened\n");
closeFiles (*pfCli, *pfTxn, *pfNew);
return 0;
}
if ((*pfTxn = fopen ("transactions.dat", "r")) == NULL) {
printf ("File transactions.dat could not be opened\n");
closeFiles (*pfCli, *pfTxn, *pfNew);
return 0;
}
if ((*pfNew = fopen ("clientupdate.dat", "w")) == NULL) {
printf ("File clientupdate.dat could not be opened\n");
closeFiles (*pfCli, *pfTxn, *pfNew);
return 0;
}
return 1;
}
static void outCli (int acct, char *nm, double bal, char *ind) {
if (acct == -1)
printf ("| %6s | %-10s | %6s | %6s | %6.2lf | %s\n",
"", nm, "", "", bal, ind);
else
printf ("| %6d | %-10s | %6s | %6s | %6.2lf | %s\n",
acct, nm, "", "", bal, ind);
}
static void outTxn (char *date, double amt, double bal) {
printf ("| %6s | %10s | %-6s | %6.2lf | %6.2lf |\n",
"", "", date, amt, bal);
}
static void getTxn (FILE *fh, int *acct, char *date, double *amt) {
if (feof (fh))
*acct = -1;
else
fscanf (fh, "%d%s%lf\n", acct, date, amt);
}
static void getCli (FILE *fh, int *last, int *acct, char *nm, double *bal) {
if (*last != -1)
outCli (-1, "", *bal, "-->");
*last = *acct;
fscanf (fh, "%d%s%lf\n", acct, nm, bal);
}
int main (void) {
int cli_acct, txn_acct, last_acct = -1;
char txn_date[10], cli_nm[10];
double cli_bal, txn_amt;
FILE *fCli, *fTxn, *fNew;
// Open all files, output header and load up first transaction.
if (!openFiles (&fCli, &fTxn, &fNew))
return 1;
outHeader (HDR_FULL);
getTxn (fTxn, &txn_acct, txn_date, &txn_amt);
// Process every account.
while (!feof (fCli)) {
// Bring in an account and print starting balance.
getCli (fCli, &last_acct, &cli_acct, cli_nm, &cli_bal);
outHeader (HDR_LINE);
outCli (cli_acct, cli_nm, cli_bal, "<--");
// While account is not yet up to txn, output and read new.
while (cli_acct < txn_acct) {
fprintf (fNew, "%d %s %.2lf\n", cli_acct, cli_nm, cli_bal);
outCli (-1, "", cli_bal, "-->");
outHeader (HDR_LINE);
fscanf (fCli, "%d%s%lf\n", &cli_acct, &cli_nm, &cli_bal);
outCli (cli_acct, cli_nm, cli_bal, "<--");
}
// If they don't match, you have a orphan transaction.
if ((cli_acct != txn_acct) && (txn_acct != -1)) {
printf ("Orphan transaction for account # %d\n", txn_acct);
closeFiles (fCli, fTxn, fNew);
return 1;
}
// While account and transaction match, process transaction lines.
while (cli_acct == txn_acct) {
cli_bal = cli_bal + txn_amt;
outTxn (txn_date, txn_amt, cli_bal);
if (feof (fTxn))
txn_acct = -1;
else
fscanf (fTxn, "%d%s%lf\n", &txn_acct, &txn_date, &txn_amt);
}
fprintf (fNew, "%d %s %.2lf\n", cli_acct, cli_nm, cli_bal);
}
// Output final lines if needed.
if (last_acct != -1)
outCli (-1, "", cli_bal, "-->");
outHeader (HDR_LINE);
// Close all files and exit successfully.
closeFiles (fCli, fTxn, fNew);
return 0;
}
of:
0 Mary 99.90
1 Pax 94.40
2 Bob 7.00
4 Roger 7.00
5 Nobody 9.00
あなたの宿題として渡す以外の目的で、このコードを自由に使用することができます。
私は、画面への印刷を除き、あなたはある程度ニックに何を意味するか理解は、私の主な目標が何であるかを私自身の利益のために、新しいクライアントが重複なしでファイルを作成することで、その私に長い時間がかかっそれはファイルにバランスとfprintfを更新する段階に到達するために、私はその情報をすべて格納してマージするのに十分な能力を持っているのか分かりません。 – James