在另一个函数中使用指针 - 银行程序

时间:2016-03-28 19:38:00

标签: c function pointers bank

我有一个关于我正在编写的代码的快速问题。参考void RunBankMenu(int *choice)void TransactionDecision(...),如何使用从RunBankMenu(Choice)获取的值为TransactionDecision设置if / else或switch语句?例如,如果用户选择1,则TransactionDecision函数将使用我设置开关的一批代码。我如何将指针传递给另一个函数,以便我可以读取值?

谢谢!

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define MAXCREDIT -4500 

void RunBankMenu(int *choice);
void Greeting();
void AccountBalance(double account, char letter);
void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr);
void DepositMoney(double *accountPtr);
void WithdrawMoney(double *accountPtr, char letter);

int main()
{
    double checkings = 430.00;
    double savings = 812.00;
    double credit = -2254.00;

    int NumberChoice = 0;
    Greeting();
    AccountBalance(checkings, savings, credit);
    RunBankMenu(&NumberChoice);

    printf("%d", NumberChoice);
}


void Greeting()
{
    printf("Welcome to the Bank of COP 2220\n\nIt is a pleasure to manage"
              " your checking, savings, and credit accounts\n");
}

void AccountBalance(double account, char letter)
{
    double checkings = 430.00;
    double savings = 812.00;
    double credit = -2254.00;

    printf("-- You currently have $%.2f in your checking account\n",checkings);
    printf("-- You currently have $%.2f in your savings account\n",savings);
    printf("-- You currently have $%.2f credit balance\n", credit);
}



void RunBankMenu(int *choice)
{
    do{
        printf("-----------------------------\n");
        printf("(1) to DEPOSIT to CHECKING\n");

        printf("(2) to WITHDRAW from CHECKING\n");

        printf("(3) to DEPOSIT to SAVINGS\n");

        printf("(4) to WITHDRAW from SAVINGS\n");

        printf("(5) to DEPOSIT to CREDIT\n");

        printf("(6) to TAKE an ADVANCE from CREDIT\n");

        printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n");

        printf("(8) for all ACCOUNT BALANCES\n");

        printf("\n(9) QUIT\n\n");

        printf("Select an option: ");
        scanf("%d", &*choice);
    } while (*choice <= 8);


}

void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr)
{
    int num1;
}

void DepositMoney(double *accountPtr)
{

}

void WithdrawMoney(double *accountPtr, char letter)
{

}

1 个答案:

答案 0 :(得分:0)

也许这段代码会让你入门......

#include <stdio.h>

typedef struct accounts_S
   {
   double checkings;
   double savings;
   double credit;
   } accounts_T; 

void DepositMoney(double *accountPtr)
   {

   }

void WithdrawMoney(double *accountPtr)
   {

   }

void TransferBetweenAccounts(accounts_T *accounts)
   {

   }

void AccountBalance(
      accounts_T *I__accounts
      )
   {
   printf("-- You currently have $%.2f in your checking account\n", I__accounts->checkings);
   printf("-- You currently have $%.2f in your savings account\n", I__accounts->savings);
   printf("-- You currently have $%.2f credit balance\n", I__accounts->credit);

   return;
   }

void TransactionDecision(int NumberChoice, accounts_T *accounts)
   {
   switch(NumberChoice)
      {
      case 1:
         DepositMoney(&accounts->checkings);
         break;

      case 2:
         WithdrawMoney(&accounts->checkings);
         break;

      case 3:
         DepositMoney(&accounts->savings);
         break;

      case 4:
         WithdrawMoney(&accounts->savings);
         break;

      case 5:
         DepositMoney(&accounts->credit);
         break;

      case 6:
         WithdrawMoney(&accounts->credit);
         break;

      case 7:
         TransferBetweenAccounts(accounts);
         break;

      case 8:
         AccountBalance(accounts);
         break;
      }

   return;
   }

void Greeting()
   {
   printf("Welcome to the Bank of COP 2220\n"
          "\n"
          "It is a pleasure to manage your checking, savings, and credit accounts\n"
          );

   return;
   }

void RunBankMenu(
      int *choice
      )
   {
   printf("-----------------------------\n");
   printf("(1) to DEPOSIT to CHECKING\n");
   printf("(2) to WITHDRAW from CHECKING\n");
   printf("(3) to DEPOSIT to SAVINGS\n");
   printf("(4) to WITHDRAW from SAVINGS\n");
   printf("(5) to DEPOSIT to CREDIT\n");
   printf("(6) to TAKE an ADVANCE from CREDIT\n");
   printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n");
   printf("(8) for all ACCOUNT BALANCES\n");
   printf("\n(9) QUIT\n\n");

   do {
      printf("Select an option: ");
      scanf("%d", choice);
      } while((0 == *choice) && (*choice > 9));

   return;
   }

int main()
   {
   int        rCode           = 0;
   int        NumberChoice;
   accounts_T accounts        =
      {
      .checkings = 430.00,
      .savings = 812.00,
      .credit = -2254.00
      };

   Greeting();
   AccountBalance(&accounts);

   do {
      RunBankMenu(&NumberChoice);
      TransactionDecision(NumberChoice, &accounts);
      } while(9 != NumberChoice);

   return(rCode);
   }
相关问题