69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
/* order item struct */
struct orderitem
{
char col[10];
char rest[40];
struct orderitem *next;
};
/* order item index struct */
struct orderindex
{
char col[10];
struct orderitem *item;
struct orderindex *next;
};
struct orderindex *_pidx;
struct orderindex *_pidxlast;
int setitem(struct orderitem *pitem, char *pstr);
void additem(char *pstr);
void addindex(struct orderindex *pindex);
void addtail(struct orderitem *psearch, struct orderitem *pitem);
void savefile(FILE *fp);
void release(void);
int main(int argc, char *argv[])
{
FILE *in;
FILE *out;
char line[80];
/* check for param */
if(argc != 3)
{
printf("Usage: order.exe <source file> <dest file>\n");
exit(EXIT_SUCCESS);
}
_pidx = NULL;
_pidxlast = NULL;
/* open source */
in = fopen(argv[1], "r");
if(!in)
{
printf("Can't open source file %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
/* open dest */
out = fopen(argv[2], "w");
if(!out)
{
printf("Can't open dest file %s.\n", argv[2]);
exit(EXIT_FAILURE);
}
/* process */
printf("Processing...\n");
while(1)
{
fgets(line, 80, in);
if(feof(in))
{
break;
}
line[strlen(line) - 1] = '\0';
additem(line);
}
/* save */
printf("Saving...\n");
savefile(out);
/* close */
fclose(in);
fclose(out);
printf("%s -> %s completed.\n", argv[1], argv[2]);
/* release memory */
release();
return 0;
}
/* set an item */
int setitem(struct orderitem *pitem, char *pstr)
{
int i;
int len;
/* check */
assert(pitem != NULL);
if(!pitem) return 0;
assert(pstr != NULL);
if(!pitem) return 0;
/* reset memory */
memset(pitem, 0, sizeof(struct orderitem));
/* split parts */
i = 0;
len = strlen(pstr);
while(i < len)
{
if(pstr[i] != ',')
{
pitem->col[i] = pstr[i];
i++;
}
else
{
strcpy(pitem->rest, pstr + i + 1);
break;
}
}
return 1;
}
/* add the item to the tail */
void addtail(struct orderitem *psearch, struct orderitem *pitem)
{
assert(psearch != NULL);
if(!psearch) return;
assert(pitem != NULL);
if(!pitem) return;
while(psearch)
{
if(psearch->next == NULL)
{
psearch->next = pitem;
break;
}
psearch = psearch->next;
}
}
/* add an index */
void addindex(struct orderindex *pindex)
{
if(!_pidxlast)
{
_pidx = pindex;
_pidxlast = pindex;
}
else
{
_pidxlast->next = pindex;
_pidxlast = pindex;
}
}
/* add an item to the list */
void additem(char *pstr)
{
struct orderitem *pitem;
struct orderindex *idx;
assert(pstr != NULL);
if(!pstr) return;
/* allocate memory for new item */
pitem = (struct orderitem*)malloc(sizeof(struct orderitem));
assert(pitem != NULL);
if(!pitem)
{
printf("Out of memory.\n");
exit(EXIT_FAILURE);
}
/* set the item data */
if(!setitem(pitem, pstr))
{
exit(EXIT_FAILURE);
}
/* looking for item col in index */
idx = _pidx;
while(1)
{
/* index not found, add one */
if(!idx)
{
idx = (struct orderindex*)malloc(sizeof(struct orderindex));
assert(idx != NULL);
if(!idx)
{
printf("Out of memory.\n");
exit(EXIT_FAILURE);
}
memset(idx, 0, sizeof(struct orderindex));
strcpy(idx->col, pitem->col);
idx->item = pitem;
addindex(idx);
break;
}
/* found index, add item */
if(strcmp(idx->col, pitem->col) == 0)
{
addtail(idx->item, pitem);
break;
}
/* continue searching */
idx = idx->next;
}
}
/* save result to file */
void savefile(FILE *fp)
{
struct orderindex *idx;
struct orderitem *pitem;
idx = _pidx;
while(idx)
{
fprintf(fp, "%s\n", idx->col);
pitem = idx->item;
while(pitem)
{
fprintf(fp, "%s\n", pitem->rest);
pitem = pitem->next;
}
idx = idx->next;
}
}
/* release memory */
void release(void)
{
struct orderindex *idx;
struct orderitem *pitem;
void *p;
idx = _pidx;
while(idx)
{
pitem = idx->item;
while(pitem)
{
p = pitem;
pitem = pitem->next;
free(p);
}
p = idx;
idx = idx->next;
free(p);
}
}