GDGFmax
´«ËµÖеÄË®ÊÖ
 
UID 9509
¾«»ª
1
»ý·Ö 55
Ìû×Ó 45
ÔĶÁȨÏÞ 1
×¢²á 2001-8-31
״̬ ÀëÏß
|
QUOTE: ÔÌûÓÉ jiaoyj ÓÚ 2006-7-6 15:13 ·¢±í
²»Äѵá£Ö»ÊÇÒ»¸ögraphicµÄÎÊÌâ Çë²»ÒªÎóµ¼±ðÈË,graphicÊÇͼÏñ´¦Àí,ºÍÂ¥Ö÷µÄÐèÇó´óÏྶͥ.
ÒÔÏÂÊÇÎÒдµÄÒ»¸öÁ´±í´¦Àí³ÌÐò,Ò²Êǿκó×÷Òµ,ÓÃTurboC»òVC++±àÒë,ÓÐÎÊÌâ¿ÉÒÔ¼ÓQQ:430005½»Á÷
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* For system("CLS") and itoa */
#define VISUAL_C
/*#define TURBO_C*/
#ifdef VISUAL_C
#include <malloc.h>
#define DISPLAY "1. ÏÔʾȫ²¿×éÔ±ÐÅÏ¢.\n"
#define INSERT "2. ²åÈëÒ»¸ö×éÔ±.\n"
#define DELETE "3. ɾ³ýÒ»¸ö×éÔ±.\n"
#define UPDATE "4. ¸üÐÂ×éÔ±ÐÅÏ¢.\n"
#define INVERT "5. ·´×ª×éÔ±Á´±í.\n"
#define SEARCH "6. ËÑË÷·ûºÏÖ¸¶¨Ìõ¼þµÄ×éÔ±.\n"
#define RANGE "7. °´Ö¸¶¨Ìõ¼þÅÅÐòÈ«²¿×éÔ±.\n"
#define CLRSCR "8. ÇåÆÁ.\n"
#define EXIT "0. Í˳ö.\n"
#else
#include <alloc.h>
#define DISPLAY "1. Display all member information.\n"
#define INSERT "2. Insert a new member.\n"
#define DELETE "3. Delete a member.\n"
#define UPDATE "4. Update member information.\n"
#define INVERT "5. Invert member linklist.\n"
#define SEARCH "6. Search member accord with condition.\n"
#define RANGE "7. Range member accord with condition.\n"
#define CLRSCR "8. Clear screen.\n"
#define EXIT "0. Exit.\n"
#endif
#define DATAFILENAME "data.txt"
#define LINELENGTH 30
typedef struct node
{
void *info;
struct node *link;
}NODE;
typedef struct member
{
int num; /* ѧºÅ number */
char name[12]; /* ÐÕÃû name */
char sex[2]; /* ÐÔ±ð sex */
int age; /* ÄêÁä age */
int score; /* ³É¼¨ score */
}MEMBER; /* ¶¨Òå³ÉÔ±ÊôÐÔ define member attribute*/
print_llist(NODE *h) /* Êä³öÁ´±í ouput linklist */
{
NODE *p;
MEMBER *mb;
#ifdef VISUAL_C
printf(" ѧºÅ ÐÕÃû ÐÔ±ð ÄêÁä ³É¼¨\n");
#else
printf(" num name sex age score\n");
#endif
p = h->link;
while (p!=NULL)
{
mb = (MEMBER*)p->info;
printf( "%5d %8s %4s %5d %6d\n",
mb->num, mb->name, mb->sex, mb->age, mb->score);
p = p->link;
}
printf("\n");
}
/*
int length(NODE *h)
{
int i = 0;
NODE *p;
p = h;
while (p->link != NULL)
{
p = p->link;
i++;
}
return i;
}
*/
insert_llist(NODE *h, int i)
{
NODE *p,*q;
MEMBER *mb;
p = h;
for ( ; i > 0; i--)
p = p->link;
q = (NODE*)malloc(sizeof(NODE));
mb = (MEMBER*)malloc(sizeof(MEMBER));
printf("ÇëÊäÈëÐÂ×éÔ±µÄѧºÅ:"); scanf("%d", &mb->num);
printf("ÇëÊäÈëÐÂ×éÔ±µÄÐÕÃû:"); scanf("%s", mb->name);
printf("ÇëÊäÈëÐÂ×éÔ±µÄÐÔ±ð:"); scanf("%s", mb->sex);
printf("ÇëÊäÈëÐÂ×éÔ±µÄÄêÁä:"); scanf("%d", &mb->age);
printf("ÇëÊäÈëÐÂ×éÔ±µÄ³É¼¨:"); scanf("%d", &mb->score);
q->info = (void*)mb;
q->link = p->link;
p->link = q;
}
NODE *del_llist(NODE *h, int i)
{
NODE *p,*q;
MEMBER *mb;
p = h;
for ( ; i > 1; i--)
p = p->link;
q = p->link;
p->link = q->link;
mb = (MEMBER*)q->info;
free(mb);
free(q);
return h;
}
update_llist(NODE *h, int i)
{
NODE *p;
MEMBER *mb;
p = h;
for ( ; i > 0; i--)
p = p->link;
mb = (MEMBER*)p->info;
printf( "%5d %8s %4s %5d %6d\n",
mb->num, mb->name, mb->sex, mb->age, mb->score);
printf("ÇëÊäÈë¸Ã×éÔ±µÄѧºÅ:"); scanf("%d", &mb->num);
printf("ÇëÊäÈë¸Ã×éÔ±µÄÐÕÃû:"); scanf("%s", mb->name);
printf("ÇëÊäÈë¸Ã×éÔ±µÄÐÔ±ð:"); scanf("%s", mb->sex);
printf("ÇëÊäÈë¸Ã×éÔ±µÄÄêÁä:"); scanf("%d", &mb->age);
printf("ÇëÊäÈë¸Ã×éÔ±µÄ³É¼¨:"); scanf("%d", &mb->score);
p->info = (void*)mb;
}
NODE *invert(NODE *h) /* ·´×ªÁ´±í invert linklist */
{
NODE *p,*ht;
ht = h->link; /* ÓÃÖ¸ÕëÖ¸ÏòÍ·½áµãºóµÄµÚÒ»¸ö½áµã£¬ÒÔ·À¶ÏÁ´ */
h->link = NULL; /* ÖÃÁ´±íΪ¿Õ±í */
while (ht != NULL)
{
p = ht; /* ½«ÔÁ´±íÖеĽáµãÒÀ´Î²åÈëµ½ÐÂÁ´ */
ht = ht->link;
p->link = h->link;
h->link = p;
}
return h;
}
search_llist(NODE *h, char *condition)
{
NODE *p;
MEMBER *mb;
char str[30];
p = h->link;
while (p != NULL)
{
mb = (MEMBER*)p->info;
if (0 == strcmp(condition, itoa(mb->num, str, 10)))
printf( "%5d %8s %4s %5d %6d\n",
mb->num, mb->name, mb->sex, mb->age, mb->score);
p = p->link;
}
}
/* Ëã·¨: ½»»»ÅÅÐò·¨ arithmetic: change */
/* ÃèÊö: ÿ´ÎÓõ±Ç°µÄ½áµãÒ»Ò»µÄͬÆäºóµÄ½áµã±È½Ï²¢½»»» */
/* ÌØµã: ×îÇåÎú¼òµ¥ */
NODE *range_llist(NODE *h, int i)
{
NODE *p,*q;
void *v;
MEMBER *mb1,*mb2;
char s1[30];
char s2[30];
for (p = h->link; p->link; p = p->link )
{
for (q = p->link; q; q = q->link)
{
mb1 = (MEMBER*)p->info;
mb2 = (MEMBER*)q->info;
switch(i)
{
case 1:{strcpy(s1, itoa(mb1->num, s1, 10));
strcpy(s2, itoa(mb2->num, s2, 10));break;}
case 2:{strcpy(s1, mb1->name);
strcpy(s2, mb2->name);break;}
case 3:{strcpy(s1, mb1->sex);
strcpy(s2, mb2->sex);break;}
case 4:{strcpy(s1, itoa(mb1->age, s1, 10));
strcpy(s2, itoa(mb2->age, s2, 10));break;}
case 5:{strcpy(s1, itoa(mb1->score, s1, 10));
strcpy(s2, itoa(mb2->score, s2, 10));break;}
}
if (strcmp(s1, s2) > 0)
{
v = q->info;
q->info = p->info;
p->info = v;
}
} /* End of for */
} /* End of for */
return h;
}
NODE *ReadData(FILE *fp, int *len)
{
NODE *h,*p;
MEMBER *mb;
char line[LINELENGTH];
h = (NODE *)malloc(sizeof(NODE)); /* Éú³ÉÍ·½áµã create head node */
if (NULL == h) return NULL;
h->info = (void*)len;
h->link = NULL;
/* ½¨Á¢Á´±í create linklist */
while (fgets( line, LINELENGTH, fp) != NULL)
{
mb = (MEMBER*)malloc(sizeof(MEMBER));
sscanf( line, "%d|%[^'|']|%[^'|']|%d|%d|",
&mb->num, mb->name, mb->sex, &mb->age, &mb->score );
p = (NODE*)malloc(sizeof(NODE));
p->info = (void*)mb;
p->link = h->link;
h->link = p;
(*(int*)h->info)++;
}
return invert(h);
}
int WriteData(NODE* h, FILE *fp)
{
NODE *p;
MEMBER *mb;
char line[LINELENGTH];
p = h->link;
while (p)
{
mb = (MEMBER*)p->info;
sprintf( line, "%d|%s|%s|%d|%d|\r\n",
mb->num, mb->name, mb->sex, mb->age, mb->score);
fputs(line, fp);
p = p->link;
}
return 1;
}
void main()
{
NODE *head;
int count = 0; /* ½áµã¸öÊý(±í³¤) node number(list length) */
FILE *fp = 0; /* Îļþ¾ä±ú file handle */
int sel,pos;
char condition[30];
if ((fp = fopen(DATAFILENAME, "r+")) == NULL)
{
printf("cannot open this file!\n");
return;
}
head = ReadData(fp, &count);
fclose(fp);
if (NULL == head) return;
while (1) /* ÉèÖò˵¥ Setting menu */
{
printf(DISPLAY);
printf(INSERT);
printf(DELETE);
printf(UPDATE);
printf(INVERT);
printf(SEARCH);
printf(RANGE);
printf(CLRSCR);
printf(EXIT);
printf("please input a number:");
scanf("%d", &sel);
switch(sel)
{
case 1:
{ /* Print */
print_llist(head);
break;
}
case 2:
{ /* Insert */
printf("Please input position:");
scanf("%d",&pos); /* head:0 first node:1 ... */
if (pos < 0 || pos > count) break;
insert_llist(head, pos);
count++;
print_llist(head);
break;
}
case 3:
{ /* Delete */
printf("Please input position:");
scanf("%d",&pos); /* first node:1 ... */
if (pos < 1 || pos > count) break;
del_llist(head, pos);
count--;
print_llist(head);
break;
}
case 4:
{ /* Update */
printf("Please input position:");
scanf("%d",&pos); /* first node:1 ... */
if (pos < 1 || pos > count) break;
update_llist(head, pos);
print_llist(head);
break;
}
case 5:
{ /* Invert */
head = invert(head);
print_llist(head);
break;
}
case 6:
{ /* Search */
printf("Please input the member number:");
scanf("%s", &condition);
search_llist(head, condition);
break;
}
case 7:
{ /* Range */
#ifdef VISUAL_C
printf("(1:ѧºÅ 2.ÐÕÃû 3.ÐÔ±ð 4.ÄêÁä 5.³É¼¨) Please input a choose:");
#else
printf("(1:Number 2.Name 3.Sex 4.Age 5.Score) Please input a choose:");
#endif
scanf("%d",&pos);
if (pos < 1 || pos > 5) break;
head = range_llist(head, pos);
print_llist(head);
break;
}
case 8:
{
#ifdef VISUAL_C
system("CLS");
#else
clrscr();
#endif
break;
}
case 0: break;
default: break;
} /* End of switch */
if (0 == sel)
break;
printf("Press ENTER to continue..");
setbuf(stdin, NULL);
getchar();
} /* End of while */
/* Save to file */
fp = fopen(DATAFILENAME, "w");
WriteData(head, fp);
fclose(fp);
free(head);
} ½¨Ò»Îı¾Îĵµ,½¨Á¢Ò»Ð©Êý¾ÝÈç:
1001|°¢Ã¨|Å®|18|566|
1002|°¢¹·|ÄÐ|20|525|
1003|°¢Öí|ÄÐ|21|586|
1004|°¢Êó|Å®|21|569|
1005|´óÀÏ»¢|ÄÐ|19|516|
¾Í¿ÉÒÔ²Ù×÷ÁË
|  Ëã·¨µÄÁ¦Á¿ |
|