2012-07-13 74 views
-1

我在畢業設計項目中將一組記錄作爲案例研究。在我編寫的代碼中,我創建了一個結構數組,並輸入int的數組 中的結構的id,以使排序更容易,但是當它運行時它會卡住。通過編號排序結構數組

#include<iostream.h> 
struct book{ 
    char Bname[8], Bsubject[8]; 
    int copyies, Bid, Bfound; 
} a[100]; 

void Ider(book a[],int r[],int index){ 
    for(int i=0;i<index;i++) 
     r[i]=a[i].Bid; 
} 

void swap(int &a,int &b){ 
    int c=a; 
    a=b; 
    b=c; 
} 

void Bublesort(int a[],int n){ 
    for(int j=0;j<n;j++) 
    for(int i=1;i<n-1;i++) 
     if(a[i]>a[i+1]) 
      swap(a[i+1],a[i]);     
} 

int bsearch(int b[],int key,int first,int last){ 
    int midel=(first+last)/2; 
    for(int i=0;i<last;i++){ 
     if(b[midel]==key) 
      return midel; 
     else 
      if(b[midel]>key) 
       return bsearch(b,key,first,midel-1); 
      else 
       if(b[midel]<key) 
        return bsearch(b,key,midel+1,last); 
    } 
} 

int main(){ 

    int r,i, index=0,m[100]; 
    char ch; 

    do{ 
     cout<<"Enter your"<<index+1<<" Book id"<<" "; 
     cout<<endl; 
     cin>>a[index].Bid; 
     cout<<"Enter your"<<index+1<<" Book name"<<" "; 
     cout<<endl; 

     cin>>a[index].Bname; 

     cout<<"Enter your"<<index+1<<" Book subject"<<" "; 
     cout<<endl; 

     cin>>a[index].Bsubject; 
     index++; 
     a[index].Bfound++; 
     a[index].copyies++; 
     if(index==99) 
      break; 

    quite : cout<<"Do you want to continue"; 
     cin>>ch; 

    } while(ch=='y'); 

    Ider(a,m,index); 
    Bublesort(m,index); 

    char p; 

    do{ 
     cout<<"Do you want to search?"<<endl; 
     cin>>p; 
     cout<<"enter your id"; 
     cin>>i; 
     r=bsearch(m,i,0,index); 
     cout<<a[r].Bfound<<"   "; 

    } while(ch=='y'); 

    system("pause"); 
    return 0; 
} 

當它到達bublesort()它掛起並沒有給出任何輸出,但爲什麼?

+0

我沒有看到任何'oop'這裏,只是一個結構和功能。這應該是面向對象嗎? – crashmstr 2012-07-13 17:19:25

+0

thanx在這裏尋求幫助 – 2012-07-13 17:22:21

+0

@AbdallaAdam:不是。這段代碼會在C語言中進行編譯(除了包含 [順帶一提,這個頭文件已被棄用,它應該是]),這不是OO語言......暗示代碼中沒有面向對象。無論你想要還是需要它是一個完全不同的問題,但不應該將該問題標記爲OOP – 2012-07-13 17:24:28

回答

0

其中還有一些問題:一次一次地將數組遍歷數組,直到它完全排序。你的只能在陣列上一次。

C example

Bubble sort (Wikipedia)

+0

謝謝我知道錯誤的位置 – 2012-07-13 18:38:12