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

(还是上次的有关问题)这段代码编译通过,在输出上存在有关问题吗

2012-03-01 
求助:(还是上次的问题)这段代码编译通过,在输出上存在问题吗?#includestdio.h#includestdlib.htypedef

求助:(还是上次的问题)这段代码编译通过,在输出上存在问题吗?
#include   <stdio.h>
#include   <stdlib.h>

typedef   struct{
                float   *elem;
                int   length;
                int   listsize;
                }sqlist;

void   initlist(sqlist   *la){
          la-> elem   =   (float   *)malloc(100   *   sizeof(float));
          if(!la-> elem)
          exit(-1);
          la-> length   =   0;
          la-> listsize   =   100;

}

float   fuzhi(sqlist   *la){
          float   x;
          int   i,n;
          printf( "Please   input   how   much   emplete:   ");
          scanf( "%d ",   &n);
          printf( "please   input   emplete: ");
          for(i=0;   i <n;   i++){
                            scanf( "%f ",   &x);
                            la-> elem[i]   =   x;
          }
          la-> length   =   n;
          return   1;
}

float   aa(sqlist   *la,   sqlist   *lb,   sqlist   *lc){
          float   *pa,   *pb,   *pc,   *pa_last,   *pb_last;
          pa   =   la-> elem;
          pb   =   lb-> elem;
          lc-> listsize   =   lc-> length   =   la-> length   +   lb-> length;
          pc   =   lc-> elem   =   (float   *)malloc(lc-> listsize   *   sizeof(float));
          if(!lc-> elem)
          exit(-2);
          pa_last   =   la-> elem   +   la-> length   -   1;
          pb_last   =   lb-> elem   +   lb-> length   -   1;
          while(pa   <=   pa_last   &&   pb   <=   pb_last)
          {
                  if(*pa   <=   *pb)
                  *pc++   =   *pa++;
                  else
                  *pc++   =   *pb++;
          }
          while(pc   <=   pa_last)
          *pc++   =   *pa++;
          while(pc   <=   pb_last)


          *pc++   =   *pa++;
}

float   shuchu(sqlist   *lc){
          int   i;
          for(i   =   0;   i   <   lc-> length;   i++)
          printf( "%f\n ",   lc-> elem[i]);
}

main(){
              sqlist   a,   b,   c;
              sqlist   *la   =   &a,   *lb   =   &b,   *lc   =   &c;
              initlist(la);
              initlist(lb);
              fuzhi(la);
              fuzhi(lb);
              aa(la,   lb,   lc);
              shuchu(lc);
}


例如如此执行:
Please   input   how   much   emplete:   3
please   input   emplete:1   6   8
Please   input   how   much   emplete:   4
please   input   emplete:1   2   5   9

结果:
1.000000
1.000000
2.000000
5.000000
6.000000
8.000000
0.000000


而正确结果应该是:
1.000000
1.000000
2.000000
5.000000
6.000000
8.000000
9.000000


请问如何修改?

[解决办法]
#include <stdio.h>
#include <stdlib.h>

typedef struct{
float *elem;
int length;
int listsize;
}sqlist;

void initlist(sqlist *la){
la-> elem = (float *)malloc(100 * sizeof(float));
if(!la-> elem)
exit(-1);
la-> length = 0;
la-> listsize = 100;

}

int fuzhi(sqlist *la){
float x;
int i,n;
printf( "Please input how much emplete: ");
scanf( "%d ", &n);
printf( "please input emplete: ");
for(i=0; i <n; i++){
scanf( "%f ", &x);
la-> elem[i] = x;
}
la-> length = n;
/*return 1; */
return la-> length;
}

void aa(sqlist *la, sqlist *lb, sqlist *lc){
float *pa, *pb, *pc, *pa_last, *pb_last;
pa = la-> elem;
pb = lb-> elem;
lc-> listsize = lc-> length = la-> length + lb-> length;
pc = lc-> elem = (float *)malloc(lc-> listsize * sizeof(float));
if(!lc-> elem)
exit(-2);
pa_last = la-> elem + la-> length - 1;
pb_last = lb-> elem + lb-> length - 1;
while(pa <= pa_last && pb <= pb_last)
{
if(*pa <= *pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
/*while(pc <= pa_last) */
while(pa <= pa_last)
*pc++ = *pa++;
/*while(pc <= pb_last) */
while(pb <= pb_last)
*pc++ = *pb++;
}

void shuchu(sqlist *lc){
int i;
for(i = 0; i < lc-> length; i++)
printf( "%f\n ", lc-> elem[i]);
}

main(){
sqlist a, b, c;
sqlist *la = &a, *lb = &b, *lc = &c;
initlist(la);
initlist(lb);
fuzhi(la);
fuzhi(lb);
aa(la, lb, lc);
shuchu(lc);

return 0;
}
自己对照看一下。。
------解决方案--------------------


while(pa <= pa_last && pb <= pb_last)
{
if(*pa <= *pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while(pc <= pa_last)----pa <=pa_last
*pc++ = *pa++;
while(pc <= pb_last)________pb <= pb_last
*pc++ = *pa++;
}
————————————————————
然后就ok啦

热点排行
Bad Request.