#include <stdio.h>
#include <stdlib.h>
typedef struct imformation{
char name[5];
int money;
int rate;
int fame;
int reputation;
}im;
typedef struct{
im r[10];
int length;
}sqlist;
int compare(sqlist &L,im n)
{
int low,high,mid,i,j;
low=1;
high=L.length;
if(L.length==0)
{
L.r[1]=n;
L.length++;
}
else
{
while(low<high)
{
mid=(low+high)/2;
if(L.r[mid].money==n.money){
for(i=1;L.r[mid+i].money==n.money;)
++i;
if(mid+i-1==L.length){
L.r[mid+i]=n;
if(L.length<10)
L.length++;
return 0;}
else {
for(j=L.length;j>=mid+i;j--){
printf("%d_11\n",L.length);//检查
L.r[j+1]=L.r[j];}
printf("%d_12\n",L.length); //当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
L.r[mid+i]=n;
if(L.length<10)
L.length++;
return 0;
}
}
else if(L.r[mid].money<n.money)
low=mid+1;
else
high=mid-1;
}
if(L.r[low].money>n.money){
for(j=L.length;j>=low;j--){
printf("%d_21\n",L.length);//检查
L.r[j+1]=L.r[j];
printf("%d_22\n",L.length);// 同样,当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
}
L.r[low]=n;
if(L.length<10)
L.length++;
return 0;
}
else if(L.r[low].money<=n.money){
for(j=L.length;j>=low+1;j--){
printf("%d_31\n",L.length);//检查
L.r[j+1]=L.r[j];
printf("%d_32\n",L.length); }//同样,当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
L.r[low+1]=n;
if(L.length<10)
L.length++;
return 0;
}
}
}
int main()
{
sqlist L;
im m;
int i;
L.length=0;
while(1){
system("cls");
for(i=L.length;i>=1;--i)
printf("%d\n",L.r[i].money);
printf("请输入你的金额:\n");
scanf("%d",&m.money);
compare(L,m);
system("pause");
}
return 0;
}
程序就是这样。