1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
friend.h
#include<iostream>
#include<string>
#include "date.h"
#ifndef FRIEND_H
#define FRIEND_H
class Friend{
public:
Friend();
std::string get_name()const;
Date get_bday()const;
bool operator == (const Friend& other)const;
bool operator != (const Friend& other)const;
void input(std::istream& ins);
void output(std::ostream& outs)const;
private:
std::string name;
Date bday;
};
std::istream& operator >>(std::istream& ins,Friend& f);
std::ostream& operator <<(std::ostream& outs,const Friend& f);
#endif
friend.cc
#include "friend.h"
#include<sstream>
using namespace std;
Friend::Friend(){
name = "";
bday = Date();
}
std::string Friend::get_name()const{
return name;
}
Date Friend::get_bday()const{
return bday;
}
bool Friend::operator==(const Friend &f)const{
return name == f.get_name() && bday == f.get_bday();
}
bool Friend::operator !=(const Friend &f)const{
return !(*this == f);
}
void Friend::input(std::istream &ins){
ins.ignore();
getline(ins,name);
ins.ignore();
int d,m,y;
string dt;
getline(ins,dt);
stringstream ss;
for(int i = 0;i<dt.length();i++){
if(dt[i] == '/'){
ss<<" ";
}else{
ss<<dt[i];
}
}
ss>>d>>m>>y;
ss.clear();
bday = Date(d,m,y);
}
void Friend::output(std::ostream &ous)const{
outs<<name<<"\t"<<bday;
}
std::ostream& operator<<(std::ostream &ous,const Friend &f){
f.output(outs);
return outs;
}
std::istream& operator>>(std::istream &ins,Friend &f){
f.input(ins);
return ins;
}
|
FBFriends.cc
#include "fbfriends.h"
#include <iostream>
FBFriends::FBFriends(){
data = new Friend[5];
used = 0;
capacity = 5;
current_index = -1;
}
FBFriends::~FBFriends(){
delete[] data;
}
FBFriends::FBFriends(const FBFriends &other){
*this = other;
}
void FBFriends::operator=(const FBFriends &other){
if(this == &other){
return;
}
capacity = other.capacity;
data = new Friend[capacity];
used = other.used;
for(int i = 0;i<used;i++){
data[i] = other.data[i];
}
}
void FBFriends::start(){
current_index = 0;
}
void FBFriends::advance(){
if(current_index < used-1)
current_index++;
}
Friend FBFriends::current(){
return data[current_index];
}
bool FBFriends::is_item(){
return current_index > 0 && current_index < used;
}
void FBFriends::remove_current(){
if(is_item()){
for(int i = current_index;i<used-1;i++){
data[i] = data[i+1];
}
used--;
}
}
void FBFriends::insert(const Friend &f){
if(!is_item()){
return;
used++;
}
if(used >= capacity){
resize();
}
for(int i = used;i>current_index;i--){
data[i] = data[i-1];
}
data[current_index] = f;
}
void FBFriends::attach(const Friend &f){
if(used >= capacity){
resize();
}
data[used++] = f;
}
void FBFriends::show_all(std::ostream &outs)const{
for(int i = 0;i<used;i++){
outs<<data[i]<<' ';
}
}
Friend FBFriends::find_friend(const std::string &name)const{
for(int i = 0;i<used;i++){
if(data[i].get_name() == name){
return data[i];
}
}
}
bool FBFriends::is_friend(const Friend &f)const{
for(int i = 0;i<used;i++){
if(f == data[i]){
return true;
}
}
return false;
}
void FBFriends::load(std::istream &ins){
Friend temp;
while(!ins.eof()){
while(ins>>temp){
attach(temp);
}
}
}
void FBFriends::save(std::ostream &outs){
for(int i = 0;i<used;i++){
outs<<data[i]<<' ';
}
}
void FBFriends::bday_sort(){
Friend temp;
for(int i = 0;i<used;i++){
for(int j = i+1;j<used;j++){
if(data[i].get_bday() > data[j].get_bday()){
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
void FBFriends::resize(){
capacity = capacity + 5;
Friend *temp = new Friend[capacity];
for(int i = 0;i<used;i++){
temp[i] = data[i];
}
delete[] data;
data = temp;
}
------------------------------------------------------------------------
fbfriends.h
#include<iostream>
#include<string>
#include<fstream>
#include "friend.h"
#ifndef FBFRIENDS_H
#define FBFRIENDS_H
class FBFriends{
public:
FBFriends();
//The functions known as the Big 3
~FBFriends();
FBFriends(const FBFriends& other);
void operator = (const FBFriends& other);
// Functions for the internal iterator
void start();
void advance();
bool is_item();
Friend current();
void remove_current();
void insert(const Friend& f);
void attach(const Friend& f);
void show_all(std::ostream& outs)const;
void bday_sort();
Friend find_friend(const std::string& name)const;
bool is_friend(const Friend& f) const;
void load(std::istream& ins);
void save(std::ostream& outs);
private:
void resize(); // increases the capacity of the container by 5
Friend *data;
int used;
int capacity;
int current_index;
};
#endif
|