데이터 구조 - 순서 표 에서 기본 작업 의 실현

17096 단어 데이터 구조
/*
           
2018.04.10
*/
#include
#include
#include
using namespace std;

//        
#define OK 1
#define ERROR 0
#define OVERFLOW -2
//Status         ,           
typedef int Status;

#define MAXSIZE 10000
typedef struct{
    char no[20];    //  IBSN
    char name[50];  //    
    double price;   //    
}Book;

typedef struct{
    Book *elem;     //        
    int length;     //           
}SqList;            //             SqList

//-----------------------------------------------------------------------------------------
//       
//    :
//1.                    , elem          ;
//2.      0

Status InitList(SqList &L){
    //         L
    L.elem=new Book[MAXSIZE];   //           MAXSIZE     
    if(!L.elem)                 //         
        exit(OVERFLOW);
    L.length=0;                 //     0
    return OK;
}

//-----------------------------------------------------------------------------------------
//  (        i,        i     )
//    :
//1.  i    (1<=i<=L.L.length);
//2. i  ,   i   L.elem[i-1]     e,  e   i        ;
Status GetElem(SqList L,int i,Book &e){
    if(i<1||i>L.length)
        return ERROR;
    e=L.elem[i-1];
    return OK;
}

//-----------------------------------------------------------------------------------------
//  (        e,        1  e     ;                 ;   ,   0)
//    :
//1.       ,   e   ,    e     L.elem[i],     ,        i+1;
//2.             ,     ,  0;

Status LocateElem1(SqList L,Book e){
    for(int i=0;iif(strcmp(L.elem[i].name,e.name)==0)
        //if(L.elem[i].name==e.name)    //      
            return i+1;
    return ERROR;
}

Status LocateElem2(SqList L,Book e){
    for(int i=0;iif(strcmp(L.elem[i].no,e.no)==0)
        //if(L.elem[i].no==e.no)    //    IBSN  
            return i+1;
    return ERROR;
}

//-----------------------------------------------------------------------------------------
//  (    i             ,    n         n+1    )
//    :
//1.      i    (i      1<=i<=n+1),       ERROR;
//2.              ,     ERROR;
//3.  n   i                ,   i   (i=n+1     );
//4.        e   i   ;
//5.   1;

Status ListInsert(SqList &L, int i, Book e){
    if((i<1)||(i>L.length+1))   //  i    
        return ERROR;
    if(L.length==MAXSIZE)       //          
        return ERROR;   
    for(int j=L.length-1;j>=i-1;--j)    //            
        L.elem[j+1]=L.elem[j];
    L.elem[i-1]=e;  //    e   i   
    ++L.length; //   1
    return OK;
}

//-----------------------------------------------------------------------------------------
//  (    i     ,       n  n-1)
//    :
//1.      i    ,       ERROR;
//2.  i+1   n             ( i=n     );
//3.   1;

Status ListDelect(SqList &L,int i){
    if(i<1||i>=L.length)
        return ERROR;
    for(int j=i;j<=L.length;++j)
        L.elem[j-1]=L.elem[j];
    --L.length;
    return OK;
}

//-----------------------------------------------------------------------------------------
//     
Status DeleteList(SqList &L){
    delete L.elem;
    return OK;
}

int main(){
    int option;
    SqList L;
    Book BookStore[3];
    strcpy(BookStore[0].name,"Happy");
    strcpy(BookStore[0].no,"BKFD532146");
    BookStore[0].price=12.445;
    strcpy(BookStore[1].name,"My Name");
    strcpy(BookStore[1].no,"JKNDK48792");
    BookStore[1].price=73.993;
    strcpy(BookStore[2].name,"I SEE");
    strcpy(BookStore[2].no,"KJFD456348");
    BookStore[2].price=58.039;
    while(1){
        system("pause");
        system("CLS");
        cout<"      "<cout<<"---------------------------------"<cout<<"1.      "<cout<<"2.    "<cout<<"3.  IBSN  "<cout<<"4.      "<cout<<"5.    "<cout<<"6.    "<cout<<"7.    "<cout<<"8.     "<cout<<"9.  "<cout<<"---------------------------------"<cout<<"Please input option:";
        cin>>option;
        Book e;
        int tmp;
        switch(option){
        case 1:
            if(OK==InitList(L))
                cout<<"1.Success!"<else
                cout<<"1.ERROR!"<break;
        case 2:
            for(int i=0;i<3;++i){
                if(ListInsert(L,i+1,BookStore[i])!=OK){
                    cout<<"2.ERROR!"<break;
                }
            }
            cout<<"2.Success!"<break;
        case 3:
            cout<<"Please input Book IBNS:";
            getchar();
            gets(e.no);
            if(LocateElem2(L,e)==ERROR){
                cout<<"3.No Found!"<break;  
            }
            else
                cout<" Stay in "<break;
        case 4:
            cout<<"Please input Book Name:";
            getchar();
            gets(e.name);
            if(LocateElem1(L,e)==ERROR){
                cout<<"4.No Found!"<break;  
            }
            else
                cout<" Stay in "<break;
        case 5:
            cout<<"Please input number:";
            cin>>tmp;
            if(GetElem(L,tmp,e)==OK){
                cout<<"\tIBNS\t\t  \t\t  "<cout<<"-----------------------------------------------"<cout<<"\t"<"\t"<"\t\t"<else
                cout<<"5.ERROR!"<break;
        case 6:
            if(GetElem(L,1,e)==OK){
                cout<<"\tIBNS\t\t  \t\t  "<cout<<"-----------------------------------------------"<for(int i=1;icout<<"\t"<"\t"<"\t\t"<1,e);
                }
            }
            else
                cout<<"6.ERROR!"<break;
        case 7:
            cout<<"Please input number:";
            cin>>tmp;
            if(ListDelect(L,tmp)==OK)
                cout<<"7.Success!"<else
                cout<<"7.ERROR!"<break;
        case 8:
            if(OK==DeleteList(L))
                cout<<"8.Success!"<else
                cout<<"8.ERROR!"<break;
        case 9:
            cout<<"See you!"<exit(0);
            break;
        default:cout<<"ERROR Input!"<break;
        }
    }
    return 0;
}

좋은 웹페이지 즐겨찾기