#include <memory.h>
#include <stdio.h>
有哪位大侠可以给我看看 我这程序哪里出问题了啊? 按主叫号号码排好以后怎么按时间排的时候就出错啊??
谢谢各位啊 感激!~!~~!
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
int time_heapsort(char *buf1, char *buf2)
{
struct tm tm1,tm2; //*定义一个结构*//
char sec[3] = "\0";
char min[3] = "\0";
char hour[3] = "\0";
char mday[3] = "\0";
char mon[3] = "\0";
char year[5] = "\0";
time_t result1,result2;
memcpy(year, buf1 + 52, 4);
tm1.tm_year = atoi(year) - 1900;
memcpy(year, buf2 + 52, 4);
tm2.tm_year = atoi(year) - 1900;
memcpy(mon, buf1 + 57, 2);
tm1.tm_mon = atoi(mon);
memcpy(mon, buf2 + 57, 2);
tm2.tm_mon = atoi(mon);
memcpy(mday, buf1 + 60, 2);
tm1.tm_mday = atoi(mday);
memcpy(mday, buf2 + 60, 2);
tm2.tm_mday = atoi(mday);
memcpy(hour, buf1 + 34, 2);
tm1.tm_hour = atoi(hour);
memcpy(hour, buf2 + 34, 2);
tm2.tm_hour = atoi(hour);
memcpy(min, buf1 + 37, 2);
tm1.tm_min = atoi(min);
memcpy(min, buf2 + 37, 2);
tm2.tm_min = atoi(min);
memcpy(sec, buf1 + 40, 2);
tm1.tm_sec = atoi(sec);
memcpy(sec, buf2 + 40, 2);
tm2.tm_sec = atoi(sec);
result1 = mktime(&tm1);
result2 = mktime(&tm2);
if(result1 > result2)
{
return 0;
}
else
{
return 1;
}
}
void createHeap(char *heap,int root,int index)//*index是节点的总数 heap是一个头指针 root是根节点*//
{
int j; //*定义j是root的子节点*//
char temp[82]; //*定义一个temp数组*//
char temp1[8] = "\0";
char temp2[8] = "\0";
char temp0[8] = "\0"; //*数组里主叫7位的暂存变量*//
int finish; //*判断累堆是否建立成功*//
j = 2 * root;
finish = 0; //*finish初始化为0*//
memcpy(temp, heap + root * 82, 82); //*根节点的82个字节存入temp*//
memcpy(temp0, temp + 3, 7); //*temp里的主叫7位存入temp0*//
while(j <= index && finish == 0)
{
memcpy(temp1, heap + 3 + j * 82, 7); //*j里的主叫7位存入temp1*//
memcpy(temp2, heap + 3 + (j + 1) * 82, 7); //*j下一位里的主叫7位存入temp2*//
if(j < index) //*temp1和temp2比较,如果相等 就比较时间*//
if(atoi(temp1) < atoi(temp2) || ((atoi(temp1) == atoi(temp2)) &&
time_heapsort(heap + j * 82,heap + (j + 1) * 82)))
{
j++;
}
memcpy(temp1, heap + 3 + j * 82, 7);
if(atoi(temp0) > atoi(temp1) || ((atoi(temp0) == atoi(temp1)) &&
time_heapsort(heap + root * 82, heap + j * 82)))
finish = 1; //*累堆完成*//
else
{
memcpy(heap + (j / 2) * 82, heap + j * 82, 82);
j = 2*j; //*把j的值存入他的根节点*//
}
}
memcpy(heap + (j / 2) * 82, temp, 82); //*把根节点的值存入temp*//
}
void HeapSort(char *heap,int index)
{
int i;
int temp[82];
for(i = (index / 2);i >= 0; i--) //*将2叉树转成heap*//
createHeap(heap, i, index);
for(i = index - 1;i >= 0; i--) //*进行累堆排序*//
{
memcpy(temp, heap + (i + 1) * 82, 82); //*heap的root值和最后一个值交换*//
memcpy(heap + (i + 1) * 82, heap, 82);
memcpy(heap, temp, 82);
createHeap(heap, 0, i); //*对其余的数组重建累堆*//
}
}
void main()
{
FILE *fp, *fq; //*打开第一个文件fp,fq是排完后的文件*//
char *ch;
struct _stat buf;
int num; //*话单总的字节数转换成整型*//
int M; //*话单数目*//
if( (fp = fopen("618.txt","rb")) == NULL) //*判断文件打开成功*//
{
printf("Sorry!");
return;
}
if(_stat("618.txt",&buf) != 0) //*计算话单的总字节数*//
printf("Not find!");
num = buf.st_size; //*总字节数转换成整型*//
M = num / 82; //*话单数目*//
ch = (char*)malloc(num); //*分配空间*//
fread(ch, 82, M, fp); //*将fp中M个82字节的话单读入ch中*//
HeapSort(ch, M - 1);
fq = fopen("666.txt","wb");
fwrite(ch, 82, M, fq); //*将ch中M个82字节的话单写入fq中*//
fclose(fp);
free(ch);
fclose(fq);
}
01 2722841 01 0991114 16:06:37 16:06:40 1996/09/03 000003 0002 0003
01 2721923 01 07338218731 13:20:11 13:21:51 1996/09/04 000100 0004 0003
01 2721901 01 09987282473 13:26:30 13:26:41 1996/09/04 000012 0002 0003
01 2721901 01 09987282473 13:27:10 13:27:20 1996/09/04 000011 0002 0003
01 2721923 01 03574924345 13:23:42 13:30:38 1996/09/04 000417 0014 0003
01 2721901 01 09987281473 13:28:11 13:31:12 1996/09/04 000180 0008 0003
01 2721901 01 0995524502 13:32:42 13:33:26 1996/09/04 000044 0002 0003
01 2721900 01 09982828809 13:43:15 13:43:22 1996/09/04 000008 0002 0003
01 2721901 01 09987281473 14:01:38 14:03:20 1996/09/04 000101 0004 0003
01 2721926 01 09987282755 14:01:21 14:05:08 1996/09/04 000227 0008 0003
01 2721915 01 09912818247 14:00:09 14:12:02 1996/09/04 000713 0024 0003
01 2721901 01 09987281473 14:12:33 14:14:36 1996/09/04 000122 0006 0003
01 2721912 01 09913835181 17:19:16 17:19:51 1996/09/04 000034 0002 0003
01 2721912 01 07555590787 17:21:09 17:24:16 1996/09/04 000188 0008 0003
01 2721912 01 09912842354 17:26:13 17:26:40 1996/09/04 000028 0002 0003
01 2721903 01 09982822738 19:01:46 19:02:12 1996/09/04 000026 0002 0003
01 2721663 01 111 17:42:11 17:42:26 1996/09/05 000015 0002 0002
01 2721665 01 111 18:15:02 18:15:12 1996/09/05 000010 0002 0002
01 2721665 01 111 18:15:23 18:15:33 1996/09/05 000010 0002 0002
01 2721665 01 111 18:15:52 18:16:04 1996/09/05 000011 0002 0002
01 2721663 01 0992223628 18:24:41 18:25:14 1996/09/05 000034 0002 0003
01 2721665 01 09913827631 18:31:51 18:33:12 1996/09/05 000081 0004 0003
01 2721665 01 0297264675 19:07:01 19:10:30 1996/09/05 000208 0008 0003
01 2721702 01 111 19:46:26 19:46:38 1996/09/05 000011 0002 0002
01 2721669 01 01065957683 20:10:44 20:11:11 1996/09/05 000027 0002 0003
01 2721669 01 01063853796 20:12:02 20:12:21 1996/09/05 000020 0002 0003
01 2721669 01 03353017332 20:13:50 20:17:26 1996/09/05 000217 0008 0003
01 2721671 01 09912832342 20:16:40 20:18:09 1996/09/05 000088 0004 0003
01 2721669 01 03353101770 20:18:02 20:18:48 1996/09/05 000047 0002 0003
01 2721671 01 01069211811 20:26:10 20:43:33 1996/09/05 001044 0036 0003
01 2721760 01 09318422433 20:53:15 20:56:44 1996/09/05 000208 0008 0003
01 2721671 01 09914894347 21:04:34 21:05:27 1996/09/05 000053 0002 0003
01 2721671 01 05353221929 21:06:55 21:07:59 1996/09/05 000064 0004 0003
01 2721669 01 03787222138 21:03:45 21:09:38 1996/09/05 000354 0012 0003
01 2721671 01 03722921107 21:10:47 21:18:33 1996/09/05 000467 0016 0003
01 2721663 01 0992221862 21:17:54 21:19:27 1996/09/05 000093 0004 0003
01 2721671 01 05353221929 21:19:20 21:23:51 1996/09/05 000270 0010 0003
01 2721663 01 0992228377 21:20:07 21:25:01 1996/09/05 000294 0010 0003
01 2721671 01 09912865628 21:39:14 21:39:30 1996/09/05 000017 0002 0003
01 2721671 01 09962151010 21:44:22 21:45:35 1996/09/05 000073 0004 0003
01 2721671 01 09962151010 22:51:37 22:52:58 1996/09/05 000081 0002 0003
01 2721671 01 09962151010 22:53:11 22:53:27 1996/09/05 000016 0001 0003
01 2723633 01 111 17:30:16 17:30:27 1996/09/05 000011 0002 0002
01 2723633 01 09913820126 17:31:01 17:31:21 1996/09/05 000020 0002 0003
01 2723633 01 5296293 17:36:12 17:36:42 1996/09/05 000030 0002 0002
01 2723633 01 111 17:38:48 17:38:58 1996/09/05 000010 0002 0002
01 2723633 01 5296293 17:39:39 17:40:08 1996/09/05 000029 0002 0002
01 2723633 01 5296293 17:42:18 17:43:48 1996/09/05 000090 0004 0002
01 2723621 01 111 18:20:21 18:20:35 1996/09/05 000014 0002 0002
01 2723613 01 111 18:46:56 18:47:07 1996/09/05 000011 0002 0002
01 2723613 01 0940255453 19:05:05 19:07:35 1996/09/05 000150 0006 0003
01 2723611 01 111 19:24:33 19:25:22 1996/09/05 000048 0002 0002
01 2723610 01 111 19:37:48 19:38:00 1996/09/05 000012 0002 0002
01 2723610 01 111 19:38:09 19:38:26 1996/09/05 000017 0002 0002
01 2723610 01 09913835181 20:07:03 20:08:56 1996/09/05 000113 0004 0003
01 2723612 01 111 20:28:57 20:29:09 1996/09/05 000012 0002 0002
01 2723612 01 09988512393 20:30:01 20:33:07 1996/09/05 000186 0008 0003
01 2723610 01 09982831093 20:28:25 20:33:56 1996/09/05 000331 0012 0003
01 2723601 01 111 20:49:13 20:49:22 1996/09/05 000009 0002 0002
01 2723601 01 111 20:54:46 20:54:57 1996/09/05 000011 0002 0002
01 2723601 01 111 20:55:09 20:55:22 1996/09/05 000013 0002 0002
01 2723612 01 09982829980 20:42:24 20:58:08 1996/09/05 000944 0032 0003
01 2723612 01 09958231177 20:58:56 20:59:24 1996/09/05 000028 0002 0003