[42Seoul] CPP Module 00 - ex02
42812 ワード
Account.hpp
およびmain
を提供します.test.cpp
を参照して19920104_091532.log
を記述する.Account.hpp
// ************************************************************************** //
// //
// 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 <[email protected]> //
// //
// ************************************************************************** //
#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__ */
test.cpp
// ************************************************************************** //
// //
// 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 <[email protected]> //
// //
// ************************************************************************** //
#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; -*-
// ************************************************************************** //
Account.cpp
void Account::_displayTimestamp(void)
{
}
void Account::displayAccountsInfos()
{
}
Account::Account(int initial_deposit)
{
}
void Account::displayStatus( void ) const
{
}
void Account::makeDeposit( int deposit )
{
}
bool Account::makeWithdrawal( int withdrawal )
{
}
Account::~Account()
{
}
hppで宣言された関数はtestされます.cppがどのように出力されるかを決定するために、関数宣言のみが追加され、cmdログが出力されます.次に、低関数の順序に一致するログファイルを比較して作成します.
void Account::_displayTimestamp(void)
{
time_t t_stamp;
char buff[16];
time(&t_stamp);
strftime(buff, sizeof(buff), "%Y%m%d_%H%M%S", localtime(&t_stamp));
std::cout << "[" << buff << "]";
}
ログファイルの時間通りに出力すべきではありませんか?と思っていたのですが、プログラム実行時間のみを出力してctimeの関数time()を利用することにしました.time t構造体(処理時間を格納する情報)では、time()を用いて秒単位で時間を指定し、strftime()を用いて所望の出力フォーマットで表すことができる.この関数でtime tを指定したフォーマットに変換する機能です.(リファレンス)localtime()は、現在の時間に秒単位で出力される.Account::Account(int initial_deposit)
{
_accountIndex = Account::_nbAccounts;
_amount = initial_deposit;
_nbDeposits = 0;
_nbWithdrawals = 0;
_displayTimestamp();
std::cout << " index:" << _accountIndex << ";";
std::cout << "amount:" << _amount << ";";
std::cout << "created" << std::endl;
Account::_nbAccounts++;
Account::_totalAmount += _amount;
}
ロゴの第1部を担当します.これは、情報が生成されたことを示す関数です.void Account::displayAccountsInfos()
{
_displayTimestamp();
std::cout << " accounts:" << Account::_nbAccounts << ";";
std::cout << "total:" << Account::_totalAmount << ";";
std::cout << "deposits:" << Account::_totalNbDeposits << ";";
std::cout << "withdrawals:" << Account::_totalNbWithdrawals << std::endl;
}
Account.cpp
があり、中間統計の感覚で出力される内容です.うん.吸気剤で近づけるように言われたようですが、忘れてしまいました..^^!void Account::makeDeposit( int deposit )
{
int p_amount;
p_amount = _amount;
_amount += deposit;
_nbDeposits++;
_displayTimestamp();
std::cout << " index:" << _accountIndex << ";";
std::cout << "p_amount:" << p_amount << ";";
std::cout << "deposits:" << deposit << ";";
std::cout << "amount:" << _amount << ";";
std::cout << "nb_deposits:" << _nbDeposits << std::endl;
Account::_totalNbDeposits++;
Account::_totalAmount += deposit;
}
[20220103_011945] accounts:8;total:20049;deposits:0;withdrawals:0
の価格上昇に伴い、deposit
の価格が変動した.amount
と理解した後、関数を作成するための新しいパラメータ인덱스; 금액; 예치금; 금액 + 예치금; 예치금 횟수;
が発表された.p_amount
で使用するために、displayAccountsInfos
パラメータ値が与えられる.bool Account::makeWithdrawal( int withdrawal )
{
int p_amount;
p_amount = _amount;
_amount = _amount - withdrawal;
_displayTimestamp();
std::cout << " index:" << _accountIndex << ";";
std::cout << "p_amount:" << p_amount << ";";
if (_amount >= 0)
{
_nbWithdrawals++;
std::cout << "withdrawal:" << withdrawal << ";";
std::cout << "amount:" << _amount << ";";
std::cout << "nb_withdrawals:" << _nbWithdrawals << std::endl;
_totalNbWithdrawals++;
Account::_totalAmount -= withdrawal;
return (true);
}
else
{
std::cout << "withdrawal:refused" << ";" << std::endl;
_amount = p_amount;
return (false);
}
}
total
関数が인덱스; 원금; 회수액; 원금 - 회수액; 회수 횟수;
boolen
が원금 - 회수액
より大きいと宣言した場合、小さな出力文も異なるため、条件文は各戻り値を0
とtrue
に分けます.Account::~Account()
{
_displayTimestamp();
std::cout << " index:" << _accountIndex << ";";
std::cout << "amount:" << _amount << ";";
std::cout << "closed" << ";" << std::endl;
}
これはすべてのプログラムを終了する関数です.残りの金額をプリントアウトすればいいのですが、私の考えでプリントアウトしました.例で受け取ったlogと出力値が異なるので、少しクラッシュしました.スレイクといくつかのブログを見て、原因を知りました.簡単に言えば,システム仕様が異なるためmacではベクトル内の消失順序はバンプを実現した標準ライブラリに依存する.評価を忘れずに言ってください.
Reference
この問題について([42Seoul] CPP Module 00 - ex02), 我々は、より多くの情報をここで見つけました https://velog.io/@soooh_42/42Seoul-CPP-Module-00-ex02テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol