程序代码:
#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 1;}
else {
for(j=L.length;j>=mid+i;--j)
L.r[j+1]=L.r[j];
printf("%d_1\n",L.length); //当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
L.r[mid+i]=n;
if(L.length<10)
L.length++;
return 1;
}
}
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_2\n",L.length);//
L.r[j+1]=L.r[j];
}//同样,当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
L.r[low]=n;
if(L.length<10)
L.length++;
return 1;
}
else if(L.r[low].money<=n.money){
for(j=L.length;j>=low+1;--j){
printf("%d_3\n",L.length);//同样,当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等
L.r[j+1]=L.r[j];
}
L.r[low+1]=n;
if(L.length<10)
L.length++;
return 1;
}
}
}
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);
for(i=L.length;i>=1;--i) //
printf("%d\n",L.r[i].money); //
system("pause");//
}