Chinaunix首页 | 论坛 | 博客
  • 博客访问: 769113
  • 博文数量: 199
  • 博客积分: 3584
  • 博客等级: 中校
  • 技术积分: 2193
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 21:18
文章分类

全部博文(199)

文章存档

2018年(6)

2013年(14)

2012年(30)

2011年(28)

2010年(24)

2009年(86)

2008年(11)

分类: C/C++

2011-10-19 15:36:06

  1. 原创代码,转载需注明。
  2. /*
  3.  *CopyRight@sh365
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. typedef struct _node{
  9.     char name[32];
  10.     char phone[13];
  11.     char qq[12];
  12.     char addres[128];
  13.     struct _node *pNext;
  14. }NODE;


  15. char *myfgets(char *s, int n, FILE *stream)
  16. {
  17.     int i = 0;

  18.     fgets(s, n, stream);
  19.     i = (int)strlen(s);
  20.     i = (i<=n-1) ? i : n-1;
  21.     s[i-1] = ((s[i-1] == '\n')||(s[i-1] == '\r')) ? '\0' : s[i-1];

  22.     return s;
  23. }

  24. int create_address_book(NODE **header)
  25. {
  26.     static int create_flag = 0;

  27.     if (!create_flag) {
  28.         (*header) = (NODE*)malloc(sizeof(NODE));
  29.         if (NULL == *header) {
  30.             printf("malloc error,create_address_book failed.\n");
  31.             return -1;
  32.         }
  33.         memset(*header, 0, sizeof(NODE));
  34.         create_flag = 1;
  35.     }

  36.     return 0;
  37. }

  38. int add_address_item(NODE *header)
  39. {
  40.     NODE *p = (NODE*)malloc(sizeof(NODE));
  41.     char dumy[8];

  42.     if (NULL == p) {
  43.         printf("malloc error,add_address_item failed.\n");
  44.         return -1;
  45.     }
  46.     memset(p, 0, sizeof(NODE));
  47.     fgets(dumy, 7, stdin);
  48.     printf("Input Name: ");
  49.     myfgets(p->name, 31, stdin);

  50.     printf("Input Phone: ");
  51.     myfgets(p->phone, 12, stdin);

  52.     printf("Input QQ: ");
  53.     myfgets(p->qq, 11, stdin);

  54.     printf("Input Addr: ");
  55.     myfgets(p->addres, 127, stdin);


  56.     while (NULL != header->pNext) {
  57.         header = header->pNext;
  58.     }
  59.     header->pNext = p;
  60.     p->pNext = NULL;

  61.     return 0;
  62. }

  63. int query_address_item(NODE *header)
  64. {
  65.     char name[32] = {'\0'};
  66.     int find_flag = -1;
  67.     myfgets(name,31,stdin);
  68.     memset(name, '\0', sizeof(name));
  69.     printf("Input Name for search: ");
  70.     myfgets(name,31,stdin);

  71.     find_flag = -1;
  72.     header = header->pNext;
  73.     while (NULL != header) {
  74.         if (NULL != strstr(header->name, name)) {
  75.             printf(" Name: %s\n", header->name);
  76.             printf("Phone: %s\n", header->phone);
  77.             printf("   QQ: %s\n", header->qq);
  78.             printf(" Addr: %s\n\n", header->addres);
  79.             find_flag = 0;
  80.         }
  81.         header = header->pNext;
  82.     }

  83.     return find_flag;
  84. }

  85. int delete_address_item(NODE *header)
  86. {
  87.     char name[32] = {'\0'};
  88.     int find_flag = -1;
  89.     NODE *p = NULL;

  90.     memset(name, '\0', sizeof(name));
  91.     printf("Input Name for delete: ");
  92.     myfgets(name,31,stdin);

  93.     find_flag = -1;
  94.     while (NULL != header->pNext) {
  95.         if (!strcmp(header->pNext->name, name)) {
  96.             printf("will delete this item:\n");
  97.             printf(" Name: %s\n", header->name);
  98.             printf("Phone: %s\n", header->phone);
  99.             printf("   QQ: %s\n", header->qq);
  100.             printf(" Addr: %s\n\n", header->addres);

  101.             p = header->pNext;
  102.             header->pNext = p->pNext;
  103.             free(p);
  104.             find_flag = 0;
  105.         }
  106.         header = header->pNext;
  107.     }

  108.     return find_flag;

  109. }

  110. int output_address_book(NODE *header)
  111. {
  112.     header = header->pNext;
  113.     while (NULL != header) {
  114.         printf(" Name: %s\n", header->name);
  115.         printf("Phone: %s\n", header->phone);
  116.         printf("   QQ: %s\n", header->qq);
  117.         printf(" Addr: %s\n\n", header->addres);

  118.         header = header->pNext;
  119.     }

  120.     return 0;
  121. }

  122. int save_address_book(NODE *header)
  123. {
  124.     FILE *fp = NULL;
  125.     fp = fopen("my_address_book.txt", "wt");
  126.     if (NULL == fp) {
  127.         printf("file open failed, can not save\n");
  128.         return -1;
  129.     }

  130.     header = header->pNext;
  131.     while (NULL != header) {
  132.         fprintf(fp, " Name: %s\n", header->name);
  133.         fprintf(fp, "Phone: %s\n", header->phone);
  134.         fprintf(fp, " QQ: %s\n", header->qq);
  135.         fprintf(fp, " Addr: %s\n\n", header->addres);

  136.         header = header->pNext;
  137.     }
  138.     fclose(fp);

  139.     return 0;
  140. }

  141. int delete_address_book(NODE *header)
  142. {
  143.     NODE *p = NULL;

  144.     while (NULL != header->pNext) {
  145.         p = header->pNext;
  146.         header->pNext = p->pNext;
  147.         free(p);
  148.     }
  149.     free(header);

  150.     return 0;
  151. }


  152. void usage()
  153. {
  154.     printf("\t|--------------------------------------------------------------|\n");
  155.     printf("\t| X Welcom to Students AddressBook X |\n");
  156.     printf("\t|--------------------------------------------------------------|\n");
  157.     printf("\t|****************X 1-Create AddressBook X**************** |\n");
  158.     printf("\t|****************X 2-Add AddressBook X**************** |\n");
  159.     printf("\t|****************X 3-Query AddressBook X**************** |\n");
  160.     printf("\t|****************X 4-Delete AddressBook X**************** |\n");
  161.     printf("\t|****************X 5-Output AddressBook X**************** |\n");
  162.     printf("\t|****************X 6-Save AddressBook X**************** |\n");
  163.     printf("\t|****************X 0-Exit AddressBook X**************** |\n");
  164.     printf("\t|--------------------------------------------------------------|\n");
  165.     printf("Input a command(0--6): ");
  166. }

  1. /*
  2.  *CopyRight @ sh365
  3.  */
  1. int main(int argc, char *argv[])
  2. {
  3.     int comd = 0;
  4.     int quit = 0;
  5.     int ret = 0;
  6.     NODE *header = NULL;

  7.     ret = create_address_book(&header);
  8.     while (!quit) {
  9.         usage();
  10.         scanf("%d", &comd);

  11.         switch (comd) {
  12.             case 1:
  13.                 ret = create_address_book(&header);
  14.                 break;

  15.             case 2:
  16.                 ret = add_address_item(header);
  17.                 break;

  18.             case 3:
  19.                 ret = query_address_item(header);
  20.                 break;

  21.             case 4:
  22.                 ret = delete_address_item(header);
  23.                 break;

  24.             case 5:
  25.                 ret = output_address_book(header);
  26.                 break;

  27.             case 6:
  28.                 ret = save_address_book(header);
  29.                 break;

  30.             case 0:
  31.                 quit = 1;
  32.                 ret = delete_address_book(header);
  33.                 break;

  34.             default:
  35.                 break;
  36.         }
  37.     }
  38. }
阅读(750) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~