正文

面向对象c++数据结构==>>数组2005-09-06 22:15:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/4462.html

分享到:

#ifndef MGR
#define MGR
class arraymgr
{
    int total_elements,max_elements;
    int * thearray;
public:
    arraymgr(int);
    ~arraymgr();
    bool addelement(int);
    bool getelement(int,int &);
    bool deleteelement(int);
    bool findelement(int,int &);
    void showelements();
    int getsize();
};
#endif
#ifndef a
#define a
#include"mgr.h"
#include<iostream>
using namespace std;
arraymgr::arraymgr(int num)
{
    max_elements=num;
    total_elements=0;
    thearray=new int [max_elements];
}
arraymgr::~arraymgr()
{
    delete [] thearray;
}
bool arraymgr::addelement(int num2)
{
    if(total_elements>=max_elements-1)
       return false;
    else
        thearray[total_elements++]=num2;
    return true;
}
bool arraymgr::getelement(int position,int& element)
{
if(position>total_elements||position<0)
return false;
else
element=thearray[position];
return true;
}
bool arraymgr::deleteelement(int position)
{
    if(position>total_elements||position<0)
      return false;
    else
    {
        for(int i=position;i<=total_elements-1;i++)
            thearray[i]=thearray[i+1];
        total_elements--;
    }
  return true;
}
bool arraymgr::findelement(int position,int &element)
{
if(position>total_elements||position<0)
     return false;
else
     element=thearray[position];
return true;
}
void arraymgr::showelements()
{
    cout<<endl;
    for(int i=0;i<total_elements;i++)
      cout<<thearray[i]<<" ";
    cout<<endl;
}
int arraymgr::getsize()
{
    return total_elements;
}
#endif
#ifndef b
#define b
#include"mgr.h"
#include"a.h"
#include<iostream>
using namespace std;
class applause
{
    arraymgr* arraybase;
    int menu();
    void add();
    void remove();
    void retrieve();
    void find();
    void list();
    void size();
public:
    applause();
    void run();
};
#endif
#ifndef c
#define c
#include"b.h"
#include<iostream>
using namespace std;
#define max 10
applause::applause()
{
    arraybase=new arraymgr(max);
}
void applause::run()
{
    int choice=menu();
    while(choice!=7)
    {
        switch(choice)
        {
        case 1:
            add();
            break;
        case 2:
            remove();
            break;
        case 3:
            retrieve();
            break;
        case 4:
            find();
            break;
        case 5:
            list();
            break;
        case 6:
            size();
            break;
        }
        choice=menu();
    }
}
int applause::menu()
{
    int choice;
    cout<<"1.add a element."<<endl;
    cout<<"2.remove a element."<<endl;
    cout<<"3.retrive an item by its' position in the structure."<<endl;
    cout<<"4.find a element."<<endl;
    cout<<"5.show the elements of the list."<<endl;
    cout<<"6.show the size of the list."<<endl;
    cout<<"7.exit."<<endl;
    cin>>choice;
    return choice;
}
void applause::add()
{
    int value;
    cout<<endl<<"Enter a new element:";
    cin>>value;
    bool result=arraybase->addelement(value);
    if(result)
        cout<<endl<<"element added successfully."<<endl;
    else
        cout<<endl<<"failture to add a new element to the list."<<endl;
}
void applause::remove()
{
    int position;
    cout<<endl<<"please input the element deleted position: ";
    cin>>position;
    bool result=arraybase->deleteelement(position);
    if(result)
        cout<<endl<<"delete the element successfully."<<endl;
    else
        cout<<endl<<"failture to delete the element."<<endl;
}
void applause::retrieve()
{
    int position,value;
    cout<<endl<<"Enter the the element's postion:";
    cin>>position;
    bool result=arraybase->getelement(position,value);
    if(result)
        cout<<endl<<"the element is:"<<value<<endl;
    else
        cout<<endl<<"cannot get the element due to the position error."<<endl;
}
void applause::find()
{
    int value,pos;
    cout<<endl<<"please input the element:";
    cin>>value;
    bool result=arraybase->findelement(value,pos);
    if(result)
        cout<<endl<<"the elem"<<value<<"at "<<pos<<endl;
    else
        cout<<endl<<"cannot find the elem in the list."<<endl;
}
void applause::list()
{
    arraybase->showelements();
}
void applause::size()
{
    cout<<endl<<"there are "<<arraybase->getsize()
        <<" elements int the list."<<endl;
}
#endif
                     
#include"mgr.h"
#include"a.h"
#include"b.h"
#include"c.h"
#include<iostream>
using namespace std;
int main()
{
    applause* theapp=new applause();
    theapp->run();
    return 0;
}

阅读(3934) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册