| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:一变量在循环中发上变化
只看楼主 加入收藏
鹿言弓
Rank: 1
等 级:新手上路
帖 子:26
专家分:1
注 册:2012-11-14
收藏
得分:0 
程序代码:
#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");//  
           } 
2012-12-07 23:23
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:0 
回复 9楼 鹿言弓
可是第一次就返回了,那做循环不就没有意义了?
而且第二次的值变了,说明是第二次调用函数时才变的,那问题就不在这里了啊?
哎说不清了...楼主你把源代码发过来吧...

I have not failed completely
2012-12-08 17:26
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:0 
程序代码:
 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;            
                                          }

这次应该对了啊?

I have not failed completely
2012-12-08 17:28
鹿言弓
Rank: 1
等 级:新手上路
帖 子:26
专家分:1
注 册:2012-11-14
收藏
得分:0 
回复 13楼 神龙赖了
没懂你的意思?你想表达什么意思呀?
2012-12-08 23:14
鹿言弓
Rank: 1
等 级:新手上路
帖 子:26
专家分:1
注 册:2012-11-14
收藏
得分:0 
回复 13楼 神龙赖了
我简化了一下,还是那点出错。您帮我看看。
#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 19:57
鹿言弓
Rank: 1
等 级:新手上路
帖 子:26
专家分:1
注 册:2012-11-14
收藏
得分:0 
回复 13楼 神龙赖了
我发现错啦,是我傻啦。。。
2012-12-11 18:20
快速回复:一变量在循环中发上变化
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019889 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved