组合练习1
来源:互联网 发布:苹果c语言编程软件 编辑:程序博客网 时间:2024/06/08 04:52
Date类
#ifndef Date_hpp#define Date_hpp#include <stdio.h>class Date{public: Date(); Date(int day, int month, int year); void Print() const ;private: int _year; int _month; int _day;};#endif /* Date_hpp */
#include "Date.hpp"#include<iostream>using namespace std;Date::Date(){}Date::Date( int day, int month, int year ){ _year = year; _month = month; _day = day;}void Date::Print() const { cout<<"出生日期:"<<_year<<"-"<<_month<<"-"<<_day<<endl;}
Person类
#ifndef Person_hpp#define Person_hpp#include <stdio.h>#include"Date.hpp"class Person{public: Person(void); Person(char * their_name, char * email, int day, int month, int year); char * GetName() const ; char * GetEmailAddress() const ; Date GetBirthDate() const ; void Print() const ; ~Person();private: char* name; char* email_address; Date date;};#endif /* Person_hpp */
#include "Person.hpp"#include<string.h>#include<iostream>using namespace std;Person::Person(void){}Person::Person(char * their_name, char * email, int day, int month, int year):date(day,month,year){ name = new char [strlen(their_name)+1]; strcpy(name,their_name); email_address = new char[strlen(email)+1]; strcpy(email_address,email);}char * Person::GetName() const{ return name;}char * Person::GetEmailAddress() const{ return email_address;}Date Person::GetBirthDate() const{ return date;}void Person::Print() const{ cout<<"姓名:"<<name<<endl; cout<<"邮箱地址:"<<email_address<<endl; date.Print() ;}Person::~Person(){ if(name != NULL) { delete name; name = NULL; } if( email_address != NULL) { delete email_address; email_address = NULL; } cout<<"Person析构函数!"<<endl;}
PersonSet类
#ifndef PersonSet_hpp#define PersonSet_hpp#include"Date.hpp"#include"Person.hpp"#include <stdio.h>class PersonSet{public: PersonSet (int initial_size = 4); ~ PersonSet (void); void Add(Person* element); Person & NextElement(); Person & RemoveElement(); Person & RemoveElement( int index ); int Size() const; void Print() const;private: Person ** _elements; int _capacity; //volume of the set int _size; //number of elements in the set int _index; };#endif /* PersonSet_hpp */
#include "PersonSet.hpp"#include<iostream>using namespace std;PersonSet::PersonSet (int initial_size){ _elements = new Person*[initial_size]; _capacity = initial_size; _size = 0; _index = 0;}PersonSet::~ PersonSet (void){ if(_elements != NULL) { delete []_elements; } cout<<"PersonSet析构函数!"<<endl;}void PersonSet:: Add(Person* element){ if(_size == _capacity) { Person* *temp = _elements; _elements = new Person*[_capacity * 2]; for(int i = 0; i < _size ;i++) { _elements[i] = temp[i]; } _capacity *= 2; delete []temp; } _elements[_size] = element; _size++;}Person& PersonSet::NextElement() //调用时先判断_index的值是否大于_size;{ if(_index >= _size) { _index = 0; } return *_elements[_index++];}// 从set中移除最后一个成员,如果Set空的数据超过一半,释放一些内存Person& PersonSet::RemoveElement(){ _size--; Person *p = _elements[_size]; if(_size < _capacity / 2) { Person* *temp = _elements; _elements = new Person*[_capacity / 2]; for(int i = 0 ;i < _size; i++) { _elements[i] = temp[i]; } _capacity /= 2; delete []temp; } return *p;}//从Set中的index索引处移除成员。如果Set空的数据超过一半, 释放一些内存Person& PersonSet:: RemoveElement( int index ){ Person *p = _elements[_index]; for(int i = index; i < _size; i++) { _elements[i] = _elements[i + 1]; } _size--; if(_size < _capacity / 2) { Person* *temp = _elements; _elements = new Person*[_capacity / 2]; for(int i = 0 ;i < _size; i++) { _elements[i] = temp[i]; } _capacity /= 2; delete []temp; } return *p;}int PersonSet::Size() const //answer the number of elements in the set.{ return _size;}void PersonSet::Print() const //print the elements of the set{ for(int i = 0; i < _size; i++) { _elements[i]->Print(); }}
main.cpp
#include"Date.hpp"#include"Person.hpp"#include"PersonSet.hpp"#include <iostream>using namespace std;int main(int argc, const char * argv[]) {// Person p1("Lou", "lou@chat.ca", 20, 6, 1960);// Person p2("Frank", "f123@chat.ca", 20, 3, 1967);// Person p3("Ann", "ann@chat.ca", 20, 8, 1960);// // p2.Print();// cout<<p1.GetName()<<endl;// cout<<p1.GetEmailAddress()<<endl;// p1.GetBirthDate().Print(); Person *p1 = new Person((char*)"Lou", (char*)"lou@chat.ca", 20, 6, 1960); Person *p2 = new Person((char*)"Frank", (char*)"f123@chat.ca", 20, 3, 1967); Person *p3 = new Person((char*)"Ann", (char*)"ann@chat.ca", 20, 8, 1960);// PersonSet per;// per.Add(p1);// per.Add(p2);// per.Add(p3);// per.Print();// // per.RemoveElement();// per.Print();// per.RemoveElement(1);// per.Print();// int size = per.Size();// for(int i = 0; i < size; i++)// {// per.NextElement().Print();// } PersonSet boys, girls; boys.Add( p1); if (p1 != &boys.RemoveElement() ) { cout << "ERROR: the objects are different \n"; } else { cout << "Good, the objects are the same \n"; } boys.Add( p1); boys.Add( p2); girls.Add( p3); boys.Add((new Person((char*)"John",(char*) "f123@chat.ca", 20, 3, 1967))); girls.Add((new Person((char*)"Sue",(char*) "f123@chat.ca", 20, 3, 1967))); boys.Add((new Person((char*)"Frank",(char*)"frank@chat.ca", 25, 4, 1958))); girls.Add((new Person((char*)"Mary",(char*) "mary@chat.ca", 25, 4, 1955))); boys.Add((new Person((char*)"John", (char*)"johnchat.ca", 12, 12, 1970))); boys.Add((new Person((char*)"John",(char*) "f123@chat.ca", 20, 3, 1967))); girls.Add((new Person((char*)"Sue",(char*) "f123@chat.ca", 20, 3, 1967))); boys.Add((new Person((char*)"Frank",(char*)"frank@chat.ca", 25, 4, 1958))); girls.Add((new Person((char*)"Mary",(char*) "mary@chat.ca", 25, 4, 1955))); boys.Add((new Person((char*)"John", (char*)"johnchat.ca", 12, 12, 1970))); int numberOfBoys = boys.Size(); cout << "number of boys = " << numberOfBoys << endl; for(int i = 0; i<7; i++) { Person & boy = boys.RemoveElement(); // boy.Print(); delete &boy; } cout << "number of boys = " << boys.Size() << endl; boys.Print();// Person & boy = boys.RemoveElement();// boy.Print();// delete &boy;// for(int i = 0; i<numberOfBoys; i++)//delete all the boys from the heap// {// Person & boy = boys.NextElement();// boy.Print();// delete &boy;// } cout << "number of girls = " << girls.Size() << "\n"; int numberOfGirls = girls.Size(); for(int i = 0; i<numberOfBoys; i++) { boys.NextElement().Print(); } for(int i = 0; i<numberOfGirls; i++)//delete all the girls from the heap { Person & her = girls.RemoveElement(); delete &her; } return 0;}
0 0
- 组合练习1
- 数据库组合查询练习1
- [练习] 求组合数
- 【基础练习】组合数
- 组合练习之投票
- 组合数 C语言练习
- 练习,求组合公式值
- Clojure练习-5.组合数据类型
- thinking in java test chapter7练习(1)~(10)组合与继承
- 算法练习:排列组合之组合和
- 概率统计、组合数学等专项练习
- 动态规划练习一 10:数字组合
- 动态规划练习10:数字组合
- Python 练习100例 Day1--排序组合
- 对象的组合 课堂练习4
- 对象的组合 课堂练习5
- spring+springmvc+hibernate+jsp框架组合练习
- VC++练习之 组合框和列表框同步
- 原型链
- ActiveServices
- upload临时文件生存周期
- 检查读写权限
- NDK与Cygwin配置
- 组合练习1
- javascript ES 6 class 详解
- IO多路复用之poll总结
- 设计模式C++实现(8)——代理模式
- YUV学习之一
- 通过单例模式去加载可配置的常量
- C++程序设计语言练习11.4 算术运算符重载的类成员函数写法
- 正向代理、反向代理、透明代理
- android;layout_weight