資料結構?

在資料結構的學習中,我們將要掌握以下的知識:掌握類的定義,根據具體需求設計類,會根據類建立各種物件,掌握物件的各種成員的使用方法。

下面是實現三元組實驗的程式碼,大家可以參考一下

方法/步驟

#include

#include

typedef int ElemType;

typedef ElemType *Triplet;

typedef int Status;

#define OK 1

#define ERROR 0

#define OVERFLOW -2

Status InitTriplet (Triplet &t,ElemType v1,ElemType v2,ElemType v3) {

t=(ElemType *)malloc(3*sizeof(ElemType));

if(!t) return OVERFLOW;

t[0]=v1;t[1]=v2;t[2]=v3;

return OK;

}

Status DestroyTriplet (Triplet &t) {

free(t); t=NULL;

return OK;

}

Status get(Triplet t,int i,ElemType &e)

{if (i<1 i>3) return ERROR;

e=t[i-1];

return OK; }

Status put(Triplet &t,int i,ElemType e)

{if (i<1 i>3) return ERROR;

t[i-1]=e;

return OK; }

Status IsAscend(Triplet t)

{return (t[0]

Status Isdescending(Triplet t){

return(t[0]>=t[1])&&(t[1]<=t[2]);}

Status IsDescending(Triplet t){

return(t[0]<=t[1])&&(t[1]>=t[2]);}

Status Max(Triplet t,ElemType &e)

{e=(t[0]>=t[1])?((t[0]>=t[2])?t[0]:t[2]):((t[1]>=t[2])?t[1]:t[2]);

return OK; }

Status Min(Triplet t,ElemType &e)

{e=(t[0]<=t[1])?((t[0]<=t[2])?t[0]:t[2]):((t[1]<=t[2])?t[1]:t[2]);

return OK;}

void main()

{ Triplet T;

ElemType e,v1,v2,v3;

int i;

char select;

cout<<"輸入三個數,建立一個三元組"<

cin>>v1>>v2>>v3;

if (InitTriplet(T,v1,v2,v3)==OVERFLOW)

cout<<"分配失敗,退出程式!"<

else

do{cout<<"請輸入1.查詢 2.遞增 3.最大值 4.置換 5.遞減 6.最小值"<

cin>>select;

switch (select)

{case '1': cout<<"請輸入i"< >i;if (get(T,i,e)==ERROR) cout<<"cuowu"<

else cout<<"第i個元素的值為:"<

case '2': if (IsAscend(T)==1) cout<<"三元組遞增"<

else cout<<"三元組非遞增\n"; break;

case '3': Max(T,e); cout<<"最大值是:"<

case '4': cout<<"請輸入i,e"< >i>>e;if (put(T,i,e)==ERROR) cout<<"i值不合法\n";

else cout<<"置換第i個素後的3個元素分別為:"<

case '5': if (IsDescending(T)==1) cout<<"三元組遞減"<

else cout<<"三元組非遞減\n"; break;

case '6': Min(T,e); cout<<"最小值是:"<

}while(select!='0');

}

最後除錯一下,出現下面的圖片,就是除錯成功了。

資料結構 經典案例 實現三元組實驗

物件, 經典, 案例, 資料結構, 三元組,
相關問題答案