具备迭代器功能的容器类sequence

来源:互联网 发布:招聘数据统计分析表 编辑:程序博客网 时间:2024/06/02 02:24

头文件sequence.h:

#ifndef MAIN_WTF_SEQUENCE_H
#define MAIN_WTF_SEQUENCE_H


#include <cstdlib>


namespace main_wtf_2
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
sequence();
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
size_type size() const;
bool is_item() const;
value_type current() const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}


#endif


实现文件sequence.cpp:


#include "sequence.h"
#include<iostream>
using namespace std;


namespace main_wtf_2
{
sequence::sequence()
{
used = 0;
current_index = 0;
}


void sequence::start()
{
current_index = 0;
}


void sequence::advance()
{
// if(current_index <= used)
current_index++;

}


bool sequence::is_item() const
{
if(current_index >= used)
return false;
return true;
}


void sequence::insert(const value_type& entry)
{
if(used < sequence::CAPACITY)
{
int count = used-current_index;
int i=0;
for(;i<count;i++)
{
data[used-i] = data[used-i-1];
}
data[used-i] = entry;
used++;

}
else 
cout<<"No room for new entry"<<endl;
}


void sequence::attach(const value_type& entry)
{
if(used < sequence::CAPACITY)
{
int count = used-current_index-1;
int i=0;
for(;i<count;i++)
{
data[used-i] = data[used-i-1];
}
data[used-i] = entry;
used++;
current_index++;
}
else 
cout<<"No room for new entry"<<endl;
}


void sequence::remove_current()
{
int count = used-current_index-1;
for(int i=0;i<count;i++)
{
data[current_index+i] = data[current_index+i+1];


}
used--;
}


sequence::size_type sequence::size() const
{
return used;
}


sequence::value_type sequence::current() const
{
return data[current_index];
}
}


测试代码:

#include<cctype>
#include<iostream>
#include<cstdlib>
#include "sequence.h"
using namespace std;
using namespace main_wtf_2;


void print_menu();
char get_user_command();
void show_sequence(sequence display);
char get_user_command();
double get_number();


int main()
{
sequence test;
char choice;


cout<<"I have initialized an empty sequence of real numbers"<<endl;


do
{
print_menu();
choice = toupper(get_user_command());
switch(choice)
{
case '!': test.start();
break;
case '+': test.advance();
break;
case '?': if(test.is_item())
 cout<<"There is an item."<<endl;
 else
 cout<<"There is no current item."<<endl;
break;
case 'C': if(test.is_item())
 cout<<"Current item is"<<test.current()<<endl;
 else
 cout<<"There is no current item."<<endl;
break;
case 'P': show_sequence(test);
break;
case 'S': cout<<"Size is "<<test.size()<<'.'<<endl;
break;
case 'I': test.insert(get_number());
break;
case 'A': test.attach(get_number());
break;
case 'R': test.remove_current();
cout<<"The current item has been removed"<<endl;
break;
case 'Q':cout<<"Ridicule is the best test of truth."<<endl;
break;
default:cout<<choice<<"is invalid"<<endl;


}
}while((choice != 'Q'));


return EXIT_SUCCESS;



}


void print_menu()
{
cout<<endl;
cout<<"The following choices are available"<<endl;
cout<<"!  Activate the start() function"<<endl;
cout<<"+  Activate the advance() function"<<endl;
cout<<"?  print the Result from the is_item() function"<<endl;
cout<<"C  print the Result from the current() function"<<endl;
cout<<"P  Print a copy of the entir sequence"<<endl;
cout<<"S  Print the result from the size() function"<<endl;
cout<<"I  Insert a new number with the insert() function"<<endl;
cout<<"A  Attach a new number with attach function"<<endl;
cout<<"R  Activate the remove_current() function"<<endl;
cout<<"Q  Quit this test program"<<endl;
}


char get_user_command()
{
char command;


cout<<"Enter choice: ";
cin>>command;


return command;
}


void show_sequence(sequence display)
{
for(display.start();display.is_item();display.advance())
cout<<display.current()<<endl;


}


double get_number()
{
double result;


cout<<"Please enter a real number for the sequence: ";
cin>>result;
cout<<result<<" has been read. "<<endl;
return result;
}

0 0