思维导图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/3134bc4272304034ae4326611434fb6b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAV3Vwa2U=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
代码示例:
#include <iostream>
using namespace std;
template<class T>
void outputArray(const T *array, int count) {
for (int i=0;i < count;i++)
cout << array[i] << " ";
cout<<endl;
}
int main(){
const int A_COUNT = 8,B_COUNT = 8,C_COUNT = 20;
int a[A_COUNT] = {1,2,3,4,5,6,7,8};
double b[B_COUNT] = {1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};
char c[C_COUNT] = " Welcome! ";
cout << " a array contains: " << endl;
outputArray(a,A_COUNT);
cout << " b array contains: " << endl;
outputArray(b,B_COUNT);
cout << " c array contains: " << endl;
outputArray(c,C_COUNT);
return 0;
}
#include <iostream>
#include<cstdlib>
using namespace std;
struct Student {
int id;
float gpa;
};
template <class T>
class Store {
private:
T item;
bool haveValue;
public:
Store();
T &getElem();
void putElem(const T &x);
};
template <class T>
Store<T>::Store(): haveValue(false) { }
template <class T>
T &Store<T>::getElem() {
if (!haveValue) {
cout << "No item present!" << endl;
exit(1);
}
return item;
}
template <class T>
void Store<T>::putElem(const T &x) {
haveValue = true;
item = x;
}
int main() {
Store<int> s1, s2;
s1.putElem(3);
s2.putElem(-7);
cout << s1.getElem() << " " << s2.getElem() << endl;
Student g = { 1000, 23 };
Store<Student> s3;
s3.putElem(g);
cout << "The student id is " << s3.getElem().id << endl;
system("pause");
return 0;
}
#include <cassert>
#include <iostream>
using namespace std;
template <class T>
class Array {
private:
T* list;
int size;
public:
Array(int sz = 50);
Array(const Array<T> &a);
~Array();
Array<T> & operator = (const Array<T> &rhs);
T & operator [] (int i);
const T & operator [] (int i) const;
operator T * ();
operator const T * () const;
int getSize() const;
void resize(int sz);
};
template <class T>
Array<T>::Array(int sz) {
assert(sz >= 0);
size = sz;
list = new T [size];
}
template <class T>
Array<T>::~Array() {
delete [] list;
}
template <class T>
Array<T>::Array(const Array<T> &a) {
size = a.size;
list = new T[size];
for (int i = 0; i < size; i++)
list[i] = a.list[i];
}
template <class T>
Array<T> &Array<T>::operator = (const Array<T>& rhs) {
if (&rhs != this) {
if (size != rhs.size) {
delete [] list;
size = rhs.size;
list = new T[size];
}
for (int i = 0; i < size; i++)
list[i] = rhs.list[i];
}
return *this;
}
template <class T>
T &Array<T>::operator[] (int n) {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
const T &Array<T>::operator[] (int n) const {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
Array<T>::operator T * () {
return list;
}
template <class T>
Array<T>::operator const T * () const {
return list;
}
template <class T>
int Array<T>::getSize() const {
return size;
}
template <class T>
void Array<T>::resize(int sz) {
assert(sz >= 0);
if (sz == size)
return;
T* newList = new T [sz];
int n = (sz < size) ? sz : size;
for (int i = 0; i < n; i++)
newList[i] = list[i];
delete[] list;
list = newList;
size = sz;
}
void read(int *p, int n) {
for (int i = 0; i < n; i++)
cin >> p[i];
}
int main() {
Array<int> a(15);
read(a, 5);
for (int i = 0; i<10;i++){
cout << "read[" << i <<"] = "<< read[i] <<" "<<endl;
}
system("pause");
return 0;
}
#include <iostream>
#include <iomanip>
#include "Arrary.h"
using namespace std;
int main() {
Array<int> a(10);
int count = 0;
int n;
cout << "Enter a value >= 2 as upper limit for prime numbers: ";
cin >> n;
for (int i = 2; i <= n; i++) {
bool isPrime = true;
for (int j = 0; j < count; j++)
if (i % a[j] == 0) {
isPrime = false;
break;
}
if (isPrime) {
if (count == a.getSize())
a.resize(count * 2);
a[count++] = i;
}
}
for (int i = 0; i < count; i++)
cout << setw(8) << a[i];
cout << endl;
system("pause");
return 0;
}
#ifndef ARRAY_H
#define ARRAY_H
#include <cassert>
template <class T>
class Array {
private:
T* list;
int size;
public:
Array(int sz = 50);
Array(const Array<T> &a);
~Array();
Array<T> & operator = (const Array<T> &rhs);
T & operator [] (int i);
const T & operator [] (int i) const;
operator T * ();
operator const T * () const;
int getSize() const;
void resize(int sz);
};
template <class T>
Array<T>::Array(int sz) {
assert(sz >= 0);
size = sz;
list = new T [size];
}
template <class T>
Array<T>::~Array() {
delete [] list;
}
template <class T>
Array<T>::Array(const Array<T> &a) {
size = a.size;
list = new T[size];
for (int i = 0; i < size; i++)
list[i] = a.list[i];
}
template <class T>
Array<T> &Array<T>::operator = (const Array<T>& rhs) {
if (&rhs != this) {
if (size != rhs.size) {
delete [] list;
size = rhs.size;
list = new T[size];
}
for (int i = 0; i < size; i++)
list[i] = rhs.list[i];
}
return *this;
}
template <class T>
T &Array<T>::operator[] (int n) {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
const T &Array<T>::operator[] (int n) const {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
Array<T>::operator T * () {
return list;
}
template <class T>
Array<T>::operator const T * () const {
return list;
}
template <class T>
int Array<T>::getSize() const {
return size;
}
template <class T>
void Array<T>::resize(int sz) {
assert(sz >= 0);
if (sz == size)
return;
T* newList = new T [sz];
int n = (sz < size) ? sz : size;
for (int i = 0; i < n; i++)
newList[i] = list[i];
delete[] list;
list = newList;
size = sz;
}
#endif
#ifndef NODE_H
#define NODE_H
template <class T>
class Node {
private:
Node<T> *next;
public:
T data;
Node (const T &data, Node<T> *next = 0);
void insertAfter(Node<T> *p);
Node<T> *deleteAfter();
Node<T> *nextNode();
const Node<T> *nextNode() const;
};
template <class T>
Node<T>::Node(const T& data, Node<T> *next) : data(data), next(next) { }
template <class T>
Node<T> *Node<T>::nextNode() {
return next;
}
template <class T>
const Node<T> *Node<T>::nextNode() const {
return next;
}
template <class T>
void Node<T>::insertAfter(Node<T> *p) {
p->next = next;
next = p;
}
template <class T>
Node<T> *Node<T>::deleteAfter() {
Node<T> *tempPtr = next;
if (next == 0)
return 0;
next = tempPtr->next;
return tempPtr;
}
#endif
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
template <class T>
class LinkedList {
private:
Node<T> *front, *rear;
Node<T> *prevPtr, *currPtr;
int size;
int position;
Node<T> *newNode(const T &item,Node<T> *ptrNext=NULL);
void freeNode(Node<T> *p);
void copy(const LinkedList<T>& L);
public:
LinkedList();
LinkedList(const LinkedList<T> &L);
~LinkedList();
LinkedList<T> & operator = (const LinkedList<T> &L);
int getSize() const;
bool isEmpty() const;
void reset(int pos = 0);
void next();
bool endOfList() const;
int currentPosition() const;
void insertFront(const T &item);
void insertRear(const T &item);
void insertAt(const T &item);
void insertAfter(const T &item);
T deleteFront();
void deleteCurrent();
T& data();
const T& data() const;
void clear();
};
template <class T>
Node<T> *LinkedList<T>::newNode(const T& item, Node<T>* ptrNext)
{
Node<T> *p;
p = new Node<T>(item, ptrNext);
if (p == NULL)
{
cout << "Memory allocation failure!\n";
exit(1);
}
return p;
}
template <class T>
void LinkedList<T>::freeNode(Node<T> *p)
{
delete p;
}
template <class T>
void LinkedList<T>::copy(const LinkedList<T>& L)
{
Node<T> *p = L.front;
int pos;
while (p != NULL)
{
insertRear(p->data);
p = p->nextNode();
}
if (position == -1)
return;
prevPtr = NULL;
currPtr = front;
for (pos = 0; pos != position; pos++)
{
prevPtr = currPtr;
currPtr = currPtr->nextNode();
}
}
template <class T>
LinkedList<T>::LinkedList() : front(NULL), rear(NULL),
prevPtr(NULL), currPtr(NULL), size(0), position(-1)
{}
template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& L)
{
front = rear = NULL;
prevPtr = currPtr = NULL;
size = 0;
position = -1;
copy(L);
}
template <class T>
LinkedList<T>::~LinkedList()
{
clear();
}
template <class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& L)
{
if (this == &L)
return *this;
clear();
copy(L);
return *this;
}
template <class T>
int LinkedList<T>::getSize() const
{
return size;
}
template <class T>
bool LinkedList<T>::isEmpty() const
{
return size == 0;
}
template <class T>
void LinkedList<T>::reset(int pos)
{
int startPos;
if (front == NULL)
return;
if (pos < 0 || pos > size - 1)
{
std::cerr << "Reset: Invalid list position: " << pos << endl;
return;
}
if (pos == 0)
{
prevPtr = NULL;
currPtr = front;
position = 0;
}
else
{
currPtr = front->nextNode();
prevPtr = front;
startPos = 1;
for (position = startPos; position != pos; position++)
{
prevPtr = currPtr;
currPtr = currPtr->nextNode();
}
}
}
template <class T>
void LinkedList<T>::next()
{
if (currPtr != NULL)
{
prevPtr = currPtr;
currPtr = currPtr->nextNode();
position++;
}
}
template <class T>
bool LinkedList<T>::endOfList() const
{
return currPtr == NULL;
}
template <class T>
int LinkedList<T>::currentPosition() const
{
return position;
}
template <class T>
void LinkedList<T>::insertFront(const T& item)
{
if (front != NULL)
reset();
insertAt(item);
}
template <class T>
void LinkedList<T>::insertRear(const T& item)
{
Node<T> *nNode;
prevPtr = rear;
nNode = newNode(item);
if (rear == NULL)
front = rear = nNode;
else
{
rear->insertAfter(nNode);
rear = nNode;
}
currPtr = rear;
position = size;
size++;
}
template <class T>
void LinkedList<T>::insertAt(const T& item)
{
Node<T> *nNode;
if (prevPtr == NULL)
{
nNode = newNode(item, front);
front = nNode;
}
else
{
nNode = newNode(item);
prevPtr->insertAfter(nNode);
}
if (prevPtr == rear)
{
rear = nNode;
position = size;
}
currPtr = nNode;
size++;
}
template <class T>
void LinkedList<T>::insertAfter(const T& item)
{
Node<T> *p;
p = newNode(item);
if (front == NULL)
{
front = currPtr = rear = p;
position = 0;
}
else
{
if (currPtr == NULL)
currPtr = prevPtr;
currPtr->insertAfter(p);
if (currPtr == rear)
{
rear = p;
position = size;
}
else
position++;
prevPtr = currPtr;
currPtr = p;
}
size++;
}
template <class T>
T LinkedList<T>::deleteFront()
{
T item;
reset();
if (front == NULL)
{
cerr << "Invalid deletion!" << endl;
exit(1);
}
item = currPtr->data;
deleteCurrent();
return item;
}
template <class T>
void LinkedList<T>::deleteCurrent()
{
Node<T> *p;
if (currPtr == NULL)
{
cerr << "Invalid deletion!" << endl;
exit(1);
}
if (prevPtr == NULL)
{
p = front;
front = front->nextNode();
}
else
p = prevPtr->deleteAfter();
if (p == rear)
{
rear = prevPtr;
position--;
}
currPtr = p->nextNode();
freeNode(p);
size--;
}
template <class T>
T& LinkedList<T>::data()
{
if (size == 0 || currPtr == NULL)
{
cerr << "Data: invalid reference!" << endl;
exit(1);
}
return currPtr->data;
}
template <class T>
void LinkedList<T>::clear()
{
Node<T> *currPosition, *nextPosition;
currPosition = front;
while (currPosition != NULL)
{
nextPosition = currPosition->nextNode();
freeNode(currPosition);
currPosition = nextPosition;
}
front = rear = NULL;
prevPtr = currPtr = NULL;
size = 0;
position = -1;
}
#endif
#include <iostream>
#include "LinkedList.h"
#include"Node.h"
using namespace std;
int main() {
LinkedList<int> list;
for (int i = 0; i < 10; i++) {
int item;
cin >> item;
list.insertFront(item);
}
cout << "List: ";
list.reset();
while (!list.endOfList()) {
cout << list.data() << " ";
list.next();
}
cout << endl;
int key;
cout << "Please enter some integer needed to be deleted: ";
cin >> key;
list.reset();
while (!list.endOfList()) {
if (list.data() == key)
list.deleteCurrent();
list.next();
}
cout << "List: ";
list.reset();
while (!list.endOfList()) {
cout << list.data() << " ";
list.next();
}
cout << endl;
system("pause");
return 0;
}
#include"Calculator.h"
#include"Stack.h"
#include<iostream>
#include<sstream>
#include<cmath>
using namespace std;
inline double stringToDouble(const string &str){
istringstream stream(str);
double result;
stream >> result;
return result;
}
void Calculator::enter(double num){
s.push(num);
}
bool Calculator::getTwoOperands(double &opnd1, double &opnd2){
if (s.isEmpty()){
cerr << " Missing operand ! " << endl;
return false;
}
opnd1 = s.pop();
if (s.isEmpty()){
cerr << " Missing operand ! " << endl;
return false;
}
opnd2 = s.pop();
return true;
}
void Calculator::compute(char op) {
double operand1, operand2;
bool result = getTwoOperands(operand1,operand2);
if (result){
switch (op)
{
case '+': s.push(operand1 + operand2);break;
case '-': s.push(operand1 + operand2);break;
case '*': s.push(operand1 + operand2);break;
case '/': if (operand1==0){
cerr << " Divided by 0 !!! " << endl;
s.clear();
}else
s.push(operand2 / operand1);
break;
case '^': s.push(pow(operand2,operand1)); break;
default: cerr << "Unrecognized operator!" << endl;
break;
}
cout << "= " << s.peek() << " ";
}else
s.clear();
}
void Calculator::run() {
string str;
while (cin >> str, str != "q") {
switch(str[0]) {
case 'c': s.clear(); break;
case '-':
if (str.size() > 1)
enter(stringToDouble(str));
else
compute(str[0]);
break;
case '+':
case '*':
case '/':
case '^':
compute(str[0]); break;
default:
enter(stringToDouble(str)); break;
}
}
}
void Calculator::clear() {
s.clear();
}
int main(){
Calculator c;
c.run();
system("pause");
return 0;
}
#ifndef STACK_H
#define STACK_H
#include <cassert>
template <class T, int SIZE = 50>
class Stack {
private:
T list[SIZE];
int top;
public:
Stack();
void push(const T &item);
T pop();
void clear();
const T &peek() const;
bool isEmpty() const;
bool isFull() const;
};
template <class T, int SIZE>
Stack<T, SIZE>::Stack() : top(-1) { }
template <class T, int SIZE>
void Stack<T, SIZE>::push(const T &item) {
assert(!isFull());
list[++top] = item;
}
template <class T, int SIZE>
T Stack<T, SIZE>::pop() {
assert(!isEmpty());
return list[top--];
}
template <class T, int SIZE>
const T &Stack<T, SIZE>::peek() const {
assert(!isEmpty());
return list[top];
}
template <class T, int SIZE>
bool Stack<T, SIZE>::isEmpty() const {
return top == -1;
}
template <class T, int SIZE>
bool Stack<T, SIZE>::isFull() const {
return top == SIZE - 1;
}
template <class T, int SIZE>
void Stack<T, SIZE>::clear() {
top = -1;
}
#endif
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include"Stack.h"
class Calculator{
private:
Stack<double> s;
void enter(double num);
bool getTwoOperands(double &opnd1, double &opnd2);
void compute(char op);
public:
void run();
void clear();
};
#endif
#ifndef QUEUE_H
#define QUEUE_H
#include <cassert>
template <class T, int SIZE = 50>
class Queue {
private:
int front, rear, count;
T list[SIZE];
public:
Queue();
void insert(const T &item);
T remove();
void clear();
const T &getFront() const;
int getLength() const;
bool isEmpty() const;
bool isFull() const;
};
template <class T, int SIZE>
Queue<T, SIZE>::Queue() : front(0), rear(0), count(0) { }
template <class T, int SIZE>
void Queue<T, SIZE>::insert (const T& item) {
assert(count != SIZE);
count++;
list[rear] = item;
rear = (rear + 1) % SIZE;
}
template <class T, int SIZE>
T Queue<T, SIZE>::remove() {
assert(count != 0);
int temp = front;
count--;
front = (front + 1) % SIZE;
return list[temp];
}
template <class T, int SIZE>
const T &Queue<T, SIZE>::getFront() const {
return list[front];
}
template <class T, int SIZE>
int Queue<T, SIZE>::getLength() const {
return count;
}
template <class T, int SIZE>
bool Queue<T, SIZE>::isEmpty() const {
return count == 0;
}
template <class T, int SIZE>
bool Queue<T, SIZE>::isFull() const {
return count == SIZE;
}
template <class T, int SIZE>
void Queue<T, SIZE>::clear() {
count = 0;
front = 0;
rear = 0;
}
#endif
#ifndef HEADER_H
#define HEADER9_H
template <class T>
void insertionSort(T a[], int n) {
int i, j;
T temp;
for (int i = 1; i < n; i++) {
int j = i;
T temp = a[i];
while (j > 0 && temp < a[j - 1]) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
#endif
#ifndef HEADER_H
#define HEADER_H
template <class T>
void mySwap(T &x, T &y) {
T temp = x;
x = y;
y = temp;
}
template <class T>
void selectionSort(T a[], int n) {
for (int i = 0; i < n - 1; i++) {
int leastIndex = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[leastIndex])
leastIndex = j;
mySwap(a[i], a[leastIndex ]);
}
}
#endif
#ifndef HEADER_H
#define HEADER_H
template <class T>
void mySwap(T &x, T &y) {
T temp = x;
x = y;
y = temp;
}
template <class T>
void bubbleSort(T a[], int n) {
int i = n - 1;
while (i > 0) {
int lastExchangeIndex = 0;
for (int j = 0; j < i; j++)
if (a[j + 1] < a[j]) {
mySwap(a[j], a[j + 1]);
lastExchangeIndex = j;
}
i = lastExchangeIndex;
}
}
#endif
#ifndef HEADER_H
#define HEADER_H
template <class T>
int seqSearch(const T list[], int n, const T &key) {
for(int i = 0; i < n; i++)
if (list[i] == key)
return i;
return -1;
}
#endif
#ifndef HEADER_H
#define HEADER_H
template <class T>
int binSearch(const T list[], int n, const T &key) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key == list[mid])
return mid;
else if (key < list[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
#endif
#include "account.h"
#include "Array.h"
#include <iostream>
using namespace std;
int main() {
Date date(2008, 11, 1);
Array<Account *> accounts(0);
cout << "(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
char cmd;
do {
date.show();
cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";
char type;
int index, day;
double amount, credit, rate, fee;
string id, desc;
Account* account;
cin >> cmd;
switch (cmd) {
case 'a':
cin >> type >> id;
if (type == 's') {
cin >> rate;
account = new SavingsAccount(date, id, rate);
} else {
cin >> credit >> rate >> fee;
account = new CreditAccount(date, id, credit, rate, fee);
}
accounts.resize(accounts.getSize() + 1);
accounts[accounts.getSize() - 1] = account;
break;
case 'd':
cin >> index >> amount;
getline(cin, desc);
accounts[index]->deposit(date, amount, desc);
break;
case 'w':
cin >> index >> amount;
getline(cin, desc);
accounts[index]->withdraw(date, amount, desc);
break;
case 's':
for (int i = 0; i < accounts.getSize(); i++) {
cout << "[" << i << "] ";
accounts[i]->show();
cout << endl;
}
break;
case 'c':
cin >> day;
if (day < date.getDay())
cout << "You cannot specify a previous day";
else if (day > date.getMaxDay())
cout << "Invalid day";
else
date = Date(date.getYear(), date.getMonth(), day);
break;
case 'n':
if (date.getMonth() == 12)
date = Date(date.getYear() + 1, 1, 1);
else
date = Date(date.getYear(), date.getMonth() + 1, 1);
for (int i = 0; i < accounts.getSize(); i++)
accounts[i]->settle(date);
break;
}
} while (cmd != 'e');
for (int i = 0; i < accounts.getSize(); i++)
delete accounts[i];
system("pause");
return 0;
}
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include "accumulator.h"
#include <string>
class Account {
private:
std::string id;
double balance;
static double total;
protected:
Account(const Date &date, const std::string &id);
void record(const Date &date, double amount, const std::string &desc);
void error(const std::string &msg) const;
public:
const std::string &getId() const { return id; }
double getBalance() const { return balance; }
static double getTotal() { return total; }
virtual void deposit(const Date &date, double amount, const std::string &desc) = 0;
virtual void withdraw(const Date &date, double amount, const std::string &desc) = 0;
virtual void settle(const Date &date) = 0;
virtual void show() const;
};
class SavingsAccount : public Account {
private:
Accumulator acc;
double rate;
public:
SavingsAccount(const Date &date, const std::string &id, double rate);
double getRate() const { return rate; }
virtual void deposit(const Date &date, double amount, const std::string &desc);
virtual void withdraw(const Date &date, double amount, const std::string &desc);
virtual void settle(const Date &date);
};
class CreditAccount : public Account {
private:
Accumulator acc;
double credit;
double rate;
double fee;
double getDebt() const {
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public:
CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
double getCredit() const { return credit; }
double getRate() const { return rate; }
double getFee() const { return fee; }
double getAvailableCredit() const {
if (getBalance() < 0)
return credit + getBalance();
else
return credit;
}
virtual void deposit(const Date &date, double amount, const std::string &desc);
virtual void withdraw(const Date &date, double amount, const std::string &desc);
virtual void settle(const Date &date);
virtual void show() const;
};
#endif
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;
double Account::total = 0;
Account::Account(const Date &date, const string &id)
: id(id), balance(0) {
date.show();
cout << "\t#" << id << " created" << endl;
}
void Account::record(const Date &date, double amount, const string &desc) {
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
total += amount;
date.show();
cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}
void Account::show() const {
cout << id << "\tBalance: " << balance;
}
void Account::error(const string &msg) const {
cout << "Error(#" << id << "): " << msg << endl;
}
SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
: Account(date, id), rate(rate), acc(date, 0) { }
void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
if (amount > getBalance()) {
error("not enough money");
} else {
record(date, -amount, desc);
acc.change(date, getBalance());
}
}
void SavingsAccount::settle(const Date &date) {
if (date.getMonth() == 1) {
double interest = acc.getSum(date) * rate
/ (date - Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
}
CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee)
: Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { }
void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
record(date, amount, desc);
acc.change(date, getDebt());
}
void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
if (amount - getBalance() > credit) {
error("not enough credit");
} else {
record(date, -amount, desc);
acc.change(date, getDebt());
}
}
void CreditAccount::settle(const Date &date) {
double interest = acc.getSum(date) * rate;
if (interest != 0)
record(date, interest, "interest");
if (date.getMonth() == 1)
record(date, -fee, "annual fee");
acc.reset(date, getDebt());
}
void CreditAccount::show() const {
Account::show();
cout << "\tAvailable credit:" << getAvailableCredit();
}
#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.h"
class Accumulator {
private:
Date lastDate;
double value;
double sum;
public:
Accumulator(const Date &date, double value)
: lastDate(date), value(value), sum(0) { }
double getSum(const Date &date) const {
return sum + value * (date - lastDate);
}
void change(const Date &date, double value) {
sum = getSum(date);
lastDate = date;
this->value = value;
}
void reset(const Date &date, double value) {
lastDate = date;
this->value = value;
sum = 0;
}
};
#endif
#ifndef ARRAY_H
#define ARRAY_H
#include <cassert>
template <class T>
class Array {
private:
T* list;
int size;
public:
Array(int sz = 50);
Array(const Array<T> &a);
~Array();
Array<T> & operator = (const Array<T> &rhs);
T & operator [] (int i);
const T & operator [] (int i) const;
operator T * ();
operator const T * () const;
int getSize() const;
void resize(int sz);
};
template <class T>
Array<T>::Array(int sz) {
assert(sz >= 0);
size = sz;
list = new T [size];
}
template <class T>
Array<T>::~Array() {
delete [] list;
}
template <class T>
Array<T>::Array(const Array<T> &a) {
size = a.size;
list = new T[size];
for (int i = 0; i < size; i++)
list[i] = a.list[i];
}
template <class T>
Array<T> &Array<T>::operator = (const Array<T>& rhs) {
if (&rhs != this) {
if (size != rhs.size) {
delete [] list;
size = rhs.size;
list = new T[size];
}
for (int i = 0; i < size; i++)
list[i] = rhs.list[i];
}
return *this;
}
template <class T>
T &Array<T>::operator[] (int n) {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
const T &Array<T>::operator[] (int n) const {
assert(n >= 0 && n < size);
return list[n];
}
template <class T>
Array<T>::operator T * () {
return list;
}
template <class T>
Array<T>::operator const T * () const {
return list;
}
template <class T>
int Array<T>::getSize() const {
return size;
}
template <class T>
void Array<T>::resize(int sz) {
assert(sz >= 0);
if (sz == size)
return;
T* newList = new T [sz];
int n = (sz < size) ? sz : size;
for (int i = 0; i < n; i++)
newList[i] = list[i];
delete[] list;
list = newList;
size = sz;
}
#endif
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace {
const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
if (day <= 0 || day > getMaxDay()) {
cout << "Invalid date: ";
show();
cout << endl;
exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}
void Date::show() const {
cout << getYear() << "-" << getMonth() << "-" << getDay();
}
#ifndef __DATE_H__
#define __DATE_H__
class Date {
private:
int year;
int month;
int day;
int totalDays;
public:
Date(int year, int month, int day);
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
int getMaxDay() const;
bool isLeapYear() const {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
void show() const;
int operator - (const Date& date) const {
return totalDays - date.totalDays;
}
};
#endif