home *** CD-ROM | disk | FTP | other *** search
- /*BUDGET1.C - calculates your bank account balances.
- There are two types of accounts:
- Checking - $.20 per check if
- balance < 500
- Savings - $5.00 per withdrawal
- (first is free)
- */
- #include <stdio.h>
-
- /*the maximum number of accounts you can have*/
- #define MAXACCOUNTS 10
-
- /*Checking - this describes checking accounts*/
- struct Checking
- {
- unsigned accountNumber;
- float balance;
- } chkAcnts[MAXACCOUNTS];
-
- /*Savings - you can probably figure this one out*/
- struct Savings
- {
- unsigned accountNumber;
- float balance;
- int noWithdrawals;
- } svgAcnts[MAXACCOUNTS];
-
- /*prototype declarations, just to keep us honest*/
- void initChecking(struct Checking *pCO);
- void processChecking(struct Checking*pCO);
-
- void initSavings (struct Savings *pSO);
- void processSavings (struct Savings*pSO);
- int main();
-
- /*main - accumulate the initial input and output totals*/
- int main()
- {
- char accountType; /*S or C*/
- unsigned keepLooping; /*0 -> get out of loop*/
- float chkTotal; /*total in checking accounts*/
- float svgTotal; /*total in savings accounts*/
- float total; /*total of all accounts*/
- int i;
- int noChkAccounts; /*no. of checking accounts*/
- int noSvgAccounts; /*no. of savings accounts*/
-
- /*loop until someone enters X or x*/
- noChkAccounts = 0;
- noSvgAccounts = 0;
- keepLooping = 1;
- while (keepLooping) /*Note 1*/
- {
- printf("Enter S for Savings,"
- " C for Checking,"
- " X for exit\n");
- scanf("\n%c", &accountType);
-
- switch (accountType)
- {
- case 'c':
- case 'C': /*Note 2*/
- if (noChkAccounts < MAXACCOUNTS)
- {
- initChecking(&chkAcnts[noChkAccounts]);
- processChecking(&chkAcnts[noChkAccounts]);
- noChkAccounts++;
- }
- else
- {
- printf("No more room for checking accounts\n");
- }
- break;
-
- case 's':
- case 'S':
- if (noSvgAccounts < MAXACCOUNTS)
- {
- initSavings(&svgAcnts[noSvgAccounts]);
- processSavings(&svgAcnts[noSvgAccounts]);
- noSvgAccounts++;
- }
- else
- {
- printf("No more room for savings accounts\n");
- }
- break;
-
- case 'x':
- case 'X':
- keepLooping = 0;
- break;
-
- default:
- printf("I didn't get that.\n");
- }
- }
-
- /*now present totals*/
- chkTotal = 0.0F; /*Note 3*/
- printf("Checking accounts:\n");
- for (i = 0; i < noChkAccounts; i++)
- {
- printf("Account %6d = %8.2f\n",
- chkAcnts[i].accountNumber,
- chkAcnts[i].balance);
- chkTotal += chkAcnts[i].balance;
- }
- svgTotal = 0.0F;
- printf("Savings accounts:\n");
- for (i = 0; i < noSvgAccounts; i++)
- {
- printf("Account %6d = %8.2f (no. withdrawals = %d)\n",
- svgAcnts[i].accountNumber,
- svgAcnts[i].balance,
- svgAcnts[i].noWithdrawals);
- svgTotal += svgAcnts[i].balance;
- }
- total = chkTotal + svgTotal;
- printf("Total for checking account = %8.2f\n", chkTotal);
- printf("Total for savings account = %8.2f\n", svgTotal);
- printf("Total worth = %8.2f\n", total);
- return 0;
- }
-
- /*initChecking - initialize a checking account*/
- void initChecking(struct Checking *pChecking)
- {
- printf("Enter account number:");
- scanf("%d", &pChecking->accountNumber);
- pChecking->balance = 0.0F;
- }
-
- /*processChecking - input the data for a checking account*/
- void processChecking(struct Checking *pChecking)
- {
- float transaction;
-
- printf("Enter positive number for deposit,\n"
- "negative for check, 0 to terminate");
- do
- {
- printf(":");
- scanf("%f", &transaction);
-
- /*is this a deposit?*/
- if (transaction > 0.0F)
- {
- pChecking->balance += transaction;
- }
-
- /*how about a withdrawal?*/
- if (transaction < 0.0F)
- {
- transaction = -transaction;
- if (pChecking->balance < transaction)
- {
- printf("Insufficient funds: "
- "balance %f, check %f\n",
- pChecking->balance, transaction);
- }
- else
- {
- pChecking->balance -= transaction;
-
- /*if balance falls too low, charge service fee*/
- if (pChecking->balance < 500.00F)
- {
- pChecking->balance -= 0.20F;
- }
- }
- }
- } while (transaction != 0.0F);
- }
-
- /*initSavings - initialize a savings account*/
- void initSavings (struct Savings *pSavings)
- {
- printf("Enter account number:");
- scanf("%d", &pSavings->accountNumber);
- pSavings->balance = 0.0F;
- pSavings->noWithdrawals = 0;
- }
-
- /*processSavings - input the data for a savings
- account*/
- void processSavings(struct Savings *pSavings)
- {
- float transaction;
-
- printf("Enter positive number for deposit,\n"
- "negative for withdrawal, 0 to terminate");
- do
- {
- printf(":");
- scanf("%f", &transaction);
-
- /*is this a deposit?*/
- if (transaction > 0.0F)
- {
- pSavings->balance += transaction;
- }
-
- /*how about a withdrawal?*/
- if (transaction < 0.0F)
- {
- transaction = -transaction;
- if (pSavings->balance < transaction)
- {
- printf("Insufficient funds: "
- "balance %f, check %f\n",
- pSavings->balance, -transaction);
- }
- else
- {
- if (++pSavings->noWithdrawals > 1)
- {
- pSavings->balance -= 5.00F;
- }
- pSavings->balance -= transaction;
- }
- }
- } while (transaction != 0.0F);
- }
-