More makefile stuff, improve cent overflow logic

This commit is contained in:
Tyler Beckman 2024-09-21 16:40:38 -06:00
parent 0755838f32
commit ca03d93f04
Signed by: Ty
GPG key ID: 2813440C772555A4
6 changed files with 228 additions and 74 deletions

43
.clang-format Normal file
View file

@ -0,0 +1,43 @@
# Tyler's personal code-style formatting file, make sure to use the class version before turning in
IndentWidth: 4
UseTab: Always
TabWidth: 4
InsertBraces: false
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# System headers from C (hardcoded, it isn't really possible to automatically detect them with regex)
- Regex: '<c(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)>'
Priority: 3
# System headers without extension.
- Regex: '<([A-Za-z0-9\Q/-_\E])+>'
Priority: 2
# Local headers with extension.
- Regex: '"([A-Za-z0-9\Q/-_\E])+\.h(pp)?"'
Priority: 1
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentCaseLabels: true
IntegerLiteralSeparator:
Binary: 0
Decimal: 3
Hex: -1
DerivePointerAlignment: false
PointerAlignment: Right
QualifierAlignment: Left

43
.class-clang-format Normal file
View file

@ -0,0 +1,43 @@
# A clang-format config to follow CSCI200's style guide, use before turning in
IndentWidth: 2
UseTab: Never
TabWidth: 2
InsertBraces: true
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# System headers from C
- Regex: '<(cassert|ccomplex|cctype|cerrno|cfenv|cfloat|cinttypes|ciso646|climits|clocale|cmath|csetjmp|csignal|cstdalign|cstdarg|cstdatomic|cstdbool|cstddef|cstdint|cstdio|cstdlib|cstdnoreturn|cstring|ctgmath|cthreads|ctime|cuchar|cwchar|cwctype)>'
Priority: 3
# System headers without extension.
- Regex: '<([A-Za-z0-9\Q/-_\E])+>'
Priority: 2
# Local headers with extension.
- Regex: '"([A-Za-z0-9\Q/-_\E])+\.h(pp)?"'
Priority: 1
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentCaseLabels: true
IntegerLiteralSeparator:
Binary: -1
Decimal: -1
Hex: -1
DerivePointerAlignment: false
PointerAlignment: Left
QualifierAlignment: Left

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
# Packed files
./*.tar.gz
# Built object files
./**/*.o

View file

@ -1,13 +1,13 @@
TARGET = A2 TARGET = A2
SRC_FILES = main.cpp atmFunctions.cpp SRC_FILES = main.cpp atmFunctions.cpp
# Tyler's custom makefile extensions # Tyler's custom makefile extensions for CSCI200 (anyone can use these if they want)
.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" .DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all"
.PHONY: pack clean-run c run .PHONY: pack clean-run c run fmt
## Adds only the necessary files for build into a .tar.gz file, named appropriately ## Adds only the necessary files for build into a .tar.gz file, named appropriately
ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp)
pack: pack: fmtc
tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null) tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null)
## Runs the pack target and then attempts to build & run the program to make sure it functions correctly ## Runs the pack target and then attempts to build & run the program to make sure it functions correctly
@ -26,6 +26,14 @@ c: clean
run: all run: all
./$(TARGET) ./$(TARGET)
## Formats all cpp, h, and hpp files with clang-format, using my personal clang-format config
fmt:
find . -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format --style=file:.clang-format -i
## Formats all cpp, h, and hpp files with clang-format, using the class clang-format config
fmtc:
find . -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format --style=file:.class-clang-format -i
# NO EDITS NEEDED BELOW THIS LINE # NO EDITS NEEDED BELOW THIS LINE
CXX = g++ CXX = g++

View file

@ -4,93 +4,149 @@
#include <iostream> #include <iostream>
#include <limits> #include <limits>
/**
* @brief Normalizes dollar & cent amounts by converting any 100 cent chunks to
* dollars
*
* @param pDollars A pointer to the amount of dollars
* @param pCents A pointer to the amount of cents
*/
void normalize_dollars(int* pDollars, int* pCents) {
*pDollars += *pCents / 100;
*pCents = *pCents % 100;
}
void start_atm() { void start_atm() {
int dollars = 0; int dollars = 0;
int cents = 0; int cents = 0;
std::cout << "You have entered the ATM menu" << std::endl; std::cout << "You have entered the ATM menu" << std::endl;
bool continuing = true; bool continuing = true;
char choice; char choice;
do { do {
print_menu(); print_menu();
choice = get_user_selection(); choice = get_user_selection();
switch (choice) { switch (choice) {
case '1': case '1':
print_balance(&dollars, &cents); print_balance(&dollars, &cents);
break; break;
case '2': case '2':
deposit_money(&dollars, &cents); deposit_money(&dollars, &cents);
break; break;
case '3': case '3':
case 'Q': withdraw_money(&dollars, &cents);
continuing = false; break;
std::cout << "Have a nice day!" << std::endl; case 'Q':
break; continuing = false;
} std::cout << "Have a nice day!" << std::endl;
} while (continuing); break;
}
} while (continuing);
} }
void print_menu() { void print_menu() {
std::cout << "Please make a selection:" << std::endl std::cout << "Please make a selection:" << std::endl
<< "(1) Print Current Balance" << std::endl << "(1) Print Current Balance" << std::endl
<< "(2) Deposit" << std::endl << "(2) Deposit" << std::endl
<< "(3) Withdraw" << std::endl << "(3) Withdraw" << std::endl
<< "(Q) Quit" << std::endl; << "(Q) Quit" << std::endl;
} }
char get_user_selection() { char get_user_selection() {
char input = 'Q'; char input = 'Q';
while (true) { while (true) {
std::cout << "Choice: "; std::cout << "Choice: ";
std::cin >> input; std::cin >> input;
std::cin.clear(); std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail() || if (std::cin.fail() ||
!(input == '1' || input == '2' || input == '3' || input == 'Q')) { !(input == '1' || input == '2' || input == '3' || input == 'Q')) {
std::cout << "Invalid choice, please try again" << std::endl; std::cout << "Invalid choice, please try again" << std::endl;
} else { } else {
std::cout << std::endl; std::cout << std::endl;
return input; return input;
} }
}; };
} }
void print_balance(int* pDollars, int* pCents) { void print_balance(int* pDollars, int* pCents) {
std::cout << "Your account currently contains a total of $" << *pDollars std::cout << "Your account currently contains a total of $" << *pDollars
<< "." << std::setfill('0') << std::setw(2) << *pCents << "." << "." << std::setfill('0') << std::setw(2) << *pCents << "."
<< std::endl << std::endl
<< std::endl; << std::endl;
} }
void deposit_money(int* pDollars, int* pCents) { void deposit_money(int* pDollars, int* pCents) {
// Prompt the user for the amount to deposit // Prompt the user for the amount to deposit
int newDollars = 0, newCents = 0; int dollarsToDeposit = 0, centsToDeposit = 0;
std::cout << "How many dollars would you like to deposit? "; std::cout << "How many dollars would you like to deposit? ";
std::cin >> newDollars; std::cin >> dollarsToDeposit;
std::cout << "How many cents would you like to deposit? "; std::cout << "How many cents would you like to deposit? ";
std::cin >> newCents; std::cin >> centsToDeposit;
// Ensure valid positive integers were entered // Ensure valid positive integers were entered
if (newDollars < 0 || newCents < 0) { if (dollarsToDeposit < 0 || centsToDeposit < 0) {
std::cout << "Deposits must be positive amounts, please try again with " std::cout << "Deposits must be positive amounts, please try again with "
"proper values" "proper values"
<< std::endl << std::endl
<< std::endl; << std::endl;
} return;
}
// Add dollar and cent values to variables passed-by-pointer // Normalize the user-inputted values for later display
*pDollars += newDollars; normalize_dollars(&dollarsToDeposit, &centsToDeposit);
*pCents += newCents;
// Condense cent amounts > 100 into dollars *pDollars += dollarsToDeposit;
*pDollars += *pCents / 100; *pCents += centsToDeposit;
*pCents = *pCents % 100;
// Display the amount deposited nicely // Normalize the true account values after addition
std::cout << "You have successfully deposited a total of $" normalize_dollars(pDollars, pCents);
<< (newDollars + (newCents / 100)) << "." << std::setfill('0')
<< std::setw(2) << newCents % 100 << "." << std::endl // Display the amount deposited nicely
<< std::endl; std::cout << "You have successfully deposited a total of $"
<< dollarsToDeposit << "." << std::setfill('0') << std::setw(2)
<< centsToDeposit << "." << std::endl
<< std::endl;
}
void withdraw_money(int* pDollars, int* pCents) {
// Prompt the user for the amount to withdraw
int dollarsToWithdraw = 0, centsToWithdraw = 0;
std::cout << "How many dollars would you like to withdraw? ";
std::cin >> dollarsToWithdraw;
std::cout << "How many cents would you like to withdraw? ";
std::cin >> centsToWithdraw;
if (centsToWithdraw < 0 || dollarsToWithdraw < 0) {
std::cout << "Withdrawals must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
return;
}
normalize_dollars(&dollarsToWithdraw, &centsToWithdraw);
// Convert dollar amounts to cents for easier subtraction
int totalCentWithdrawal = dollarsToWithdraw + (dollarsToWithdraw * 100);
*pCents += *pDollars * 100;
*pDollars = 0;
if (totalCentWithdrawal > *pCents) {
std::cout << "You do not have enough money for this withdrawal" << std::endl
<< std::endl;
return;
}
*pCents -= totalCentWithdrawal;
normalize_dollars(pDollars, pCents);
std::cout << "You have successfully withdrawn a total of $"
<< dollarsToWithdraw << "." << std::setfill('0') << std::setw(2)
<< centsToWithdraw << "." << std::endl
<< std::endl;
} }

View file

@ -1,6 +1,6 @@
#include "atmFunctions.h" #include "atmFunctions.h"
int main() { int main() {
start_atm(); start_atm();
return 0; return 0;
} }