Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- struct HistoryEntry{
- int month;
- char *url;
- /*~HistoryEntry(){
- delete url;
- }*/
- HistoryEntry(int _month, const char *_url){
- month = _month;
- url = new char[strlen(_url)+1];
- strcpy(url, _url);
- }
- HistoryEntry(){
- }
- };
- class BrowserHistory{
- private:
- HistoryEntry *history;
- int size;
- int capacity;
- void del();
- public:
- BrowserHistory(int N){
- history = new HistoryEntry [N];
- size = 0;
- capacity = N;
- }
- /* ~BrowserHistory(){
- delete[] history;
- }*/
- void push_back(int _month, const char* _url){
- if(size<capacity){
- HistoryEntry he = HistoryEntry(_month, _url);
- history[size]=he;
- size++;
- }
- else cout<<"Limit is reached!\n";
- }
- void push_back(const HistoryEntry& h){
- if(size<capacity){
- history[size]=h;
- size++;
- }
- else cout<<"Limit is reached!\n";
- }
- void print(){
- for(int i=0; i<size; i++){
- cout<<history[i].month<<" "<<history[i].url<<endl;
- }
- }
- int per_month(int month)const{
- int result=0;
- for(int i=0; i<size; i++){
- if(history[i].month == month){
- result++;
- }
- }
- return result;
- }
- int most_entries()const{
- int max_num=0, max_month=0, cnt = 0;
- for(int i=1; i<=12; i++){
- for(int j = 0; j < size; j++){
- if(history[j].month == i){
- cnt++;
- }
- }
- if(cnt > max_num){
- max_num = cnt;
- cnt = 0;
- max_month = i;
- }
- }
- return max_month;
- }
- void delete_last(){
- if(size > 0){
- size--;
- }
- }
- BrowserHistory concat(const BrowserHistory& other)const{
- BrowserHistory result(capacity+other.capacity);
- for(int i=0; i<size; i++){
- result.push_back(history[i]);
- }
- for(int j=0; j<other.size; j++){
- result.push_back(other.history[j]);
- }
- return result;
- }
- };
- int main()
- {
- BrowserHistory b(5);
- b.push_back(3, 'c');
- b.push_back(HistoryEntry(3, 'v'));
- b.print();
- cout<<b.most_entries()<<endl;
- cout<<b.per_month(3)<<endl;
- b.push_back(HistoryEntry(7, 'f'));
- b.print();
- b.delete_last();
- BrowserHistory c=b.concat(b);
- cout<<"b+b: \n";
- c.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement