Adding the start of ex02

This commit is contained in:
Raphael 2024-10-25 12:56:51 +02:00
parent 8a95c7d3cb
commit 26f7e39833
6 changed files with 358 additions and 0 deletions

8
cpp00/ex02/.clangd Normal file
View file

@ -0,0 +1,8 @@
CompileFlags:
Compiler: clang
Add:
- "-xc++"
- "-I/usr/local/include"
- "-I/nix/store/pzmpyb9hxfv60vh0l67avr94h71nip1b-llvm-16.0.6-dev/include"
- "-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
- "-I/Users/raphael/Documents/piscine/cpp/cpp00/ex01/includes/"

View file

@ -0,0 +1,59 @@
[19920104_091532] index:0;amount:42;created
[19920104_091532] index:1;amount:54;created
[19920104_091532] index:2;amount:957;created
[19920104_091532] index:3;amount:432;created
[19920104_091532] index:4;amount:1234;created
[19920104_091532] index:5;amount:0;created
[19920104_091532] index:6;amount:754;created
[19920104_091532] index:7;amount:16576;created
[19920104_091532] accounts:8;total:20049;deposits:0;withdrawals:0
[19920104_091532] index:0;amount:42;deposits:0;withdrawals:0
[19920104_091532] index:1;amount:54;deposits:0;withdrawals:0
[19920104_091532] index:2;amount:957;deposits:0;withdrawals:0
[19920104_091532] index:3;amount:432;deposits:0;withdrawals:0
[19920104_091532] index:4;amount:1234;deposits:0;withdrawals:0
[19920104_091532] index:5;amount:0;deposits:0;withdrawals:0
[19920104_091532] index:6;amount:754;deposits:0;withdrawals:0
[19920104_091532] index:7;amount:16576;deposits:0;withdrawals:0
[19920104_091532] index:0;p_amount:42;deposit:5;amount:47;nb_deposits:1
[19920104_091532] index:1;p_amount:54;deposit:765;amount:819;nb_deposits:1
[19920104_091532] index:2;p_amount:957;deposit:564;amount:1521;nb_deposits:1
[19920104_091532] index:3;p_amount:432;deposit:2;amount:434;nb_deposits:1
[19920104_091532] index:4;p_amount:1234;deposit:87;amount:1321;nb_deposits:1
[19920104_091532] index:5;p_amount:0;deposit:23;amount:23;nb_deposits:1
[19920104_091532] index:6;p_amount:754;deposit:9;amount:763;nb_deposits:1
[19920104_091532] index:7;p_amount:16576;deposit:20;amount:16596;nb_deposits:1
[19920104_091532] accounts:8;total:21524;deposits:8;withdrawals:0
[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0
[19920104_091532] index:1;amount:819;deposits:1;withdrawals:0
[19920104_091532] index:2;amount:1521;deposits:1;withdrawals:0
[19920104_091532] index:3;amount:434;deposits:1;withdrawals:0
[19920104_091532] index:4;amount:1321;deposits:1;withdrawals:0
[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0
[19920104_091532] index:6;amount:763;deposits:1;withdrawals:0
[19920104_091532] index:7;amount:16596;deposits:1;withdrawals:0
[19920104_091532] index:0;p_amount:47;withdrawal:refused
[19920104_091532] index:1;p_amount:819;withdrawal:34;amount:785;nb_withdrawals:1
[19920104_091532] index:2;p_amount:1521;withdrawal:657;amount:864;nb_withdrawals:1
[19920104_091532] index:3;p_amount:434;withdrawal:4;amount:430;nb_withdrawals:1
[19920104_091532] index:4;p_amount:1321;withdrawal:76;amount:1245;nb_withdrawals:1
[19920104_091532] index:5;p_amount:23;withdrawal:refused
[19920104_091532] index:6;p_amount:763;withdrawal:657;amount:106;nb_withdrawals:1
[19920104_091532] index:7;p_amount:16596;withdrawal:7654;amount:8942;nb_withdrawals:1
[19920104_091532] accounts:8;total:12442;deposits:8;withdrawals:6
[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0
[19920104_091532] index:1;amount:785;deposits:1;withdrawals:1
[19920104_091532] index:2;amount:864;deposits:1;withdrawals:1
[19920104_091532] index:3;amount:430;deposits:1;withdrawals:1
[19920104_091532] index:4;amount:1245;deposits:1;withdrawals:1
[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0
[19920104_091532] index:6;amount:106;deposits:1;withdrawals:1
[19920104_091532] index:7;amount:8942;deposits:1;withdrawals:1
[19920104_091532] index:0;amount:47;closed
[19920104_091532] index:1;amount:785;closed
[19920104_091532] index:2;amount:864;closed
[19920104_091532] index:3;amount:430;closed
[19920104_091532] index:4;amount:1245;closed
[19920104_091532] index:5;amount:23;closed
[19920104_091532] index:6;amount:106;closed
[19920104_091532] index:7;amount:8942;closed

34
cpp00/ex02/Account.cpp Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Account.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 16:11:46 by rparodi #+# #+# */
/* Updated: 2024/10/23 16:52:20 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Account.hpp"
Account::Account()
{
_nbAccounts = 0;
_totalAmount = 0;
_totalNbDeposits = 0;
_totalNbWithdrawals = 0;
}
static int Account::getNbAccounts()
{
t.size();
}
static int Account::getTotalAmount();
static int Account::getNbDeposits();
static int Account::getNbWithdrawals();
static void Account::displayAccountsInfos();

69
cpp00/ex02/Account.hpp Normal file
View file

@ -0,0 +1,69 @@
// ************************************************************************** //
// //
// Account.hpp for GlobalBanksters United //
// Created on : Thu Nov 20 19:43:15 1989 //
// Last update : Wed Jan 04 14:54:06 1992 //
// Made by : Brad "Buddy" McLane <bm@gbu.com> //
// //
// ************************************************************************** //
#pragma once
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
// ************************************************************************** //
// Account Class //
// ************************************************************************** //
class Account {
public:
typedef Account t;
static int getNbAccounts( void );
static int getTotalAmount( void );
static int getNbDeposits( void );
static int getNbWithdrawals( void );
static void displayAccountsInfos( void );
Account( int initial_deposit );
~Account( void );
void makeDeposit( int deposit );
bool makeWithdrawal( int withdrawal );
int checkAmount( void ) const;
void displayStatus( void ) const;
private:
static int _nbAccounts;
static int _totalAmount;
static int _totalNbDeposits;
static int _totalNbWithdrawals;
static void _displayTimestamp( void );
int _accountIndex;
int _amount;
int _nbDeposits;
int _nbWithdrawals;
Account( void );
};
// ************************************************************************** //
// vim: set ts=4 sw=4 tw=80 noexpandtab: //
// -*- indent-tabs-mode:t; -*-
// -*- mode: c++-mode; -*-
// -*- fill-column: 75; comment-column: 75; -*-
// ************************************************************************** //
#endif /* __ACCOUNT_H__ */

116
cpp00/ex02/Makefile Normal file
View file

@ -0,0 +1,116 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/10/23 16:47:57 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = phonebook
# Commands
CXX = c++
RM = rm -rf
# Flags
# Mandatory flags for 42 cpp
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE)
CXXFLAGS += -D DEBUG=1
# Sources
SRC = ./tests.cpp \
./Account.cpp
# Objects
OBJDIRNAME = ./build
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.cpp
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@$(CXX) $(CXXFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
# Phony
.PHONY: all bonus clean fclean re
-include ${OBJ:.o=.d}

72
cpp00/ex02/tests.cpp Normal file
View file

@ -0,0 +1,72 @@
// ************************************************************************** //
// //
// tests.cpp for GlobalBanksters United //
// Created on : Thu Nov 20 23:45:02 1989 //
// Last update : Wed Jan 04 09:23:52 1992 //
// Made by : Brad "Buddy" McLane <bm@gbu.com> //
// //
// ************************************************************************** //
#include <vector>
#include <algorithm>
#include <functional>
#include "Account.hpp"
int main( void ) {
typedef std::vector<Account::t> accounts_t;
typedef std::vector<int> ints_t;
typedef std::pair<accounts_t::iterator, ints_t::iterator> acc_int_t;
int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 };
size_t const amounts_size( sizeof(amounts) / sizeof(int) );
accounts_t accounts( amounts, amounts + amounts_size );
accounts_t::iterator acc_begin = accounts.begin();
accounts_t::iterator acc_end = accounts.end();
int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 };
size_t const d_size( sizeof(d) / sizeof(int) );
ints_t deposits( d, d + d_size );
ints_t::iterator dep_begin = deposits.begin();
ints_t::iterator dep_end = deposits.end();
int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 };
size_t const w_size( sizeof(w) / sizeof(int) );
ints_t withdrawals( w, w + w_size );
ints_t::iterator wit_begin = withdrawals.begin();
ints_t::iterator wit_end = withdrawals.end();
Account::displayAccountsInfos();
std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
for ( acc_int_t it( acc_begin, dep_begin );
it.first != acc_end && it.second != dep_end;
++(it.first), ++(it.second) ) {
(*(it.first)).makeDeposit( *(it.second) );
}
Account::displayAccountsInfos();
std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
for ( acc_int_t it( acc_begin, wit_begin );
it.first != acc_end && it.second != wit_end;
++(it.first), ++(it.second) ) {
(*(it.first)).makeWithdrawal( *(it.second) );
}
Account::displayAccountsInfos();
std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
return 0;
}
// ************************************************************************** //
// vim: set ts=4 sw=4 tw=80 noexpandtab: //
// -*- indent-tabs-mode:t; -*-
// -*- mode: c++-mode; -*-
// -*- fill-column: 75; comment-column: 75; -*-
// ************************************************************************** //