我在毕业设计项目中作为案例研究采用了一系列记录。在我编写的代码中,我创建了一个结构数组,并在数组中输入了结构的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()
时它挂起并且没有给出任何输出,但为什么?