More makefile stuff, improve cent overflow logic
This commit is contained in:
parent
0755838f32
commit
ca03d93f04
6 changed files with 228 additions and 74 deletions
43
.clang-format
Normal file
43
.clang-format
Normal 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
43
.class-clang-format
Normal 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
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# Packed files
|
||||||
|
./*.tar.gz
|
||||||
|
# Built object files
|
||||||
|
./**/*.o
|
14
Makefile
14
Makefile
|
@ -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++
|
||||||
|
|
|
@ -4,6 +4,18 @@
|
||||||
#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;
|
||||||
|
@ -23,6 +35,8 @@ void start_atm() {
|
||||||
deposit_money(&dollars, ¢s);
|
deposit_money(&dollars, ¢s);
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
|
withdraw_money(&dollars, ¢s);
|
||||||
|
break;
|
||||||
case 'Q':
|
case 'Q':
|
||||||
continuing = false;
|
continuing = false;
|
||||||
std::cout << "Have a nice day!" << std::endl;
|
std::cout << "Have a nice day!" << std::endl;
|
||||||
|
@ -66,31 +80,73 @@ void print_balance(int* pDollars, int* pCents) {
|
||||||
|
|
||||||
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, ¢sToDeposit);
|
||||||
*pCents += newCents;
|
|
||||||
|
|
||||||
// Condense cent amounts > 100 into dollars
|
*pDollars += dollarsToDeposit;
|
||||||
*pDollars += *pCents / 100;
|
*pCents += centsToDeposit;
|
||||||
*pCents = *pCents % 100;
|
|
||||||
|
// Normalize the true account values after addition
|
||||||
|
normalize_dollars(pDollars, pCents);
|
||||||
|
|
||||||
// Display the amount deposited nicely
|
// Display the amount deposited nicely
|
||||||
std::cout << "You have successfully deposited a total of $"
|
std::cout << "You have successfully deposited a total of $"
|
||||||
<< (newDollars + (newCents / 100)) << "." << std::setfill('0')
|
<< dollarsToDeposit << "." << std::setfill('0') << std::setw(2)
|
||||||
<< std::setw(2) << newCents % 100 << "." << std::endl
|
<< 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, ¢sToWithdraw);
|
||||||
|
|
||||||
|
// 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;
|
<< std::endl;
|
||||||
}
|
}
|
Loading…
Reference in a new issue