首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

变量在循环中发作改变

2012-12-29 
变量在循环中发生改变#include stdio.h#include stdlib.htypedef struct imformation{char name[5]in

变量在循环中发生改变

#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; 
        }



程序就是这样。
[解决办法]
变量在循环中发作改变
[解决办法]
lz,感觉代码太长,没看,太累

不过问题的原因是:
在length=0~8时,处理覆盖了length=9时的空间,或者在length==0的时候,就扩大了一点范围,累加到9的时候,就完全覆盖了,具体是哪个语句,请lz自己找了。。。。

[解决办法]
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. 

热点排行