变量在循环中发生改变

我就一小菜 2012-12-08 04:15:37
#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;
}



程序就是这样。
...全文
264 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-12-10
  • 打赏
  • 举报
回复
To set a breakpoint when a variable changes value From the Edit menu, click Breakpoints. Click the Data tab of the Breakpoints dialog box. In the Expression text box, type the name of the variable. Click OK to set the breakpoint.
子善旬 2012-12-09
  • 打赏
  • 举报
回复
L.r[j+1]=L.r[j]; 即L.r[10]=L.r[9]; 即是L.length=L.r[j];
子善旬 2012-12-09
  • 打赏
  • 举报
回复
这个问题实在是。。。。 for(j=L.length;j>=low;j--){ //如果L.length==9 printf("%d_21\n",L.length);//检查 L.r[j+1]=L.r[j]; //则L.r[j+1]就是L.r[10],而你定义的L只从L.r[0]到L.r[9],所以L.r[10]就相当于L.length的内存空间 printf("%d_22\n",L.length);// 同样,当length=9时,此处会发生改变,为-2;其他时候正常,比如length为1,2,3等 }
我就一小菜 2012-12-09
  • 打赏
  • 举报
回复
引用 7 楼 enlinux 的回复:
其它地方是不是一样的数组下标问题呢?
不是啦,我专门这样从结构体数组[1]开始的,好进写,偷了下懒。我现在把简化版的发给你下,是一个数组,帮我看看。 #include <stdio.h> #include <stdlib.h> typedef struct{ int r[10]; int length; }sqlist; int compare(sqlist &L,int money) { int low,high,mid,i,j; low=1; high=L.length; if(L.length==0) { L.r[1]=money; L.length++; } else { while(low<=high) { mid=(low+high)/2; if(money<L.r[mid]) high=mid-1; else low=mid+1; } for(j=L.length;j>=high+1;--j) L.r[j+1]=L.r[j]; L.r[high+1]=money; if(L.length<10) L.length++; } return 1; } int main() { sqlist L; int i,money; L.length=0; while(1){ system("cls"); for(i=L.length;i>=1;--i) printf("%d\n",L.r[i]); printf("请输入你的金额:\n"); scanf("%d",&money); compare(L,money); system("pause"); } return 0; }
子善旬 2012-12-09
  • 打赏
  • 举报
回复
其它地方是不是一样的数组下标问题呢?
子善旬 2012-12-09
  • 打赏
  • 举报
回复
其它地方没看 感觉例如下面代码是不是错了? if(L.length==0) { L.r[1]=n; // 不应该是 L.r[0]=n; 么???? L.length++; }
我就一小菜 2012-12-08
  • 打赏
  • 举报
回复
引用 4 楼 enlinux 的回复:
lz,感觉代码太长,没看,太累 不过问题的原因是: 在length=0~8时,处理覆盖了length=9时的空间,或者在length==0的时候,就扩大了一点范围,累加到9的时候,就完全覆盖了,具体是哪个语句,请lz自己找了。。。。
没太懂起呀,覆盖了那片,不对呀,我一个个输入看了的呀。
子善旬 2012-12-08
  • 打赏
  • 举报
回复
lz,感觉代码太长,没看,太累 不过问题的原因是: 在length=0~8时,处理覆盖了length=9时的空间,或者在length==0的时候,就扩大了一点范围,累加到9的时候,就完全覆盖了,具体是哪个语句,请lz自己找了。。。。
我就一小菜 2012-12-08
  • 打赏
  • 举报
回复
引用 1 楼 AnYidan 的回复:
代码长了???其实就是个排序,用到二分查找,然后再插入。可我在我表明那点,就出现了问题。
我就一小菜 2012-12-08
  • 打赏
  • 举报
回复
代码长了???其实就是个排序,用到二分查找,然后再插入。可我在我表明那点,就出现了问题。
AnYidan 2012-12-08
  • 打赏
  • 举报
回复

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧