У меня есть задание, в котором я должен запрограммировать простой банкомат, который принимает сумму в 10 долларов и обменивает их на 100, 50, 20 и 10 долларов.Наименьшее количество счетов должно быть возвращено.
Задание сосредоточено на указателях, и я начинаю сомневаться в том, что то, что я делаю, является хорошим стилем программирования, потому что я считаю, что я беру указатель на указатель (например, &*balance
)
Это хороший способ сделать это или я должен переписать его?
Это мой мой main
:
int main(void) {
int amount = 0; // Amount of 10 dollar bills
int balance = 0; // Total balance (in dollars)
int hundred = 0, fifty = 0, twenty = 0, ten = 0; // Amount of bills calculated
// Set number of 10 dollar bills
printf("Enter amount of 10 dollar bills: ");
scanf("%d", &amount);
// Set balance to user's 10 dollar bills
valueOfBills(&balance, &amount, BILL_VALUE_TEN);
// Calculate amount of bills for the different types
amountOfBills(&balance, &hundred, &fifty, &twenty, &ten);
}
В моем main
Я звоню amountOfBills()
, что является процедурой, которая подсчитывает, сколько из каждого счета вернуть:
void amountOfBills(int *balance, int *hundred, int *fifty, int *twenty, int *ten) {
/* To end up with the least amount of bills, we will start with bills with the largest value */
// Calculate amount of 100 dollar bills
amountOfBillsForBillValue(&*hundred, &*balance, BILL_VALUE_HUNDRED);
// Calculate amount of 50 dollar bills
amountOfBillsForBillValue(&*fifty, &*balance, BILL_VALUE_FIFTY);
// Calculate amount of 20 dollar bills
amountOfBillsForBillValue(&*twenty, &*balance, BILL_VALUE_TWENTY);
// Calculate amount of 10 dollar bills
amountOfBillsForBillValue(&*ten, &*balance, BILL_VALUE_TEN);
}
В amountOfBills()
Я звоню amountOfBillsForBillValue()
, который подсчитывает, сколько из конкретного счета (например, 100 долларовых купюр)) для возврата:
void amountOfBillsForBillValue(int *storeAmount, int *balance, int billValue) {
/* We don't want to do anything if the balance is 0
therefore we will just return to avoid any calculations */
if (balance == 0) return;
// Calculate amount
*storeAmount = *balance / billValue;
// Set balance
*balance %= billValue;
}
В amountOfBills()
Я использую &*balance
.Должен ли я сделать это по-другому или это нормально?