通信録(チェーンテーブル+ファイル実装)+反省

12224 ワード

最近、他の人の通信録(チェーンテーブル+ファイル実装)を変更するのを手伝って、多分自分が学んだのが透き通っていないので、かつて犯した過ちはやはり犯し続けています...記録して、自分を警戒します.
#include 
#include 
#include 
#include 
#define LEN sizeof(person)
struct  stu     /*    */
{
	char name[20];            /*  */
	char email[20];           /*    */
	char tel[20];             /*  */
	char category[10];        /*  */
	struct stu *next;      /*    */
};
typedef struct stu person;
person *first = NULL;      /*      */
int num = 0;

void Homepage();    /*    */
void add();      /*      */
void show();     /*      */
void del();      /*      */
void search();   /*      */
void modify();   /*      */
void quit();     /*      */
void elect();    /*      */
void read();     /*      */
void save();

int main()
{
	while (1)
	{
		Homepage();
		elect();         /*                                       */
	}
	return 0;
}

void Homepage()
{
	system("cls");     /*  ,                 */
	printf("                                                           
"); printf("
**** ****
"); printf("************************************************************
"); printf("* ----- (1) *
"); printf("* ----- (2) *
"); printf("* ----- (3) *
"); printf("* ----- (4) *
"); printf("* ----- (5) *
"); printf("* ----- (6) *
"); printf("* ----- (7) *
"); printf("* ----- (0) *
"); printf("************************************************************
"); printf(""); /* */ printf("

:"); } void elect() { int i; scanf("%d", &i); while (i<0 || i>7) /* */ { printf(" , :"); scanf("%d", &i); } switch (i) /* switch */ { case 1:add();break; case 2:show();break; case 3:del();break; case 4:search();break; case 5:modify();break; case 6:read();break; case 7:save();break; case 0:quit();break; } printf(" "); getchar(); /* exit elect */ getchar(); } void add() { int i; person *p1, *p2; /* p1 ,p2 */ printf("
*******************************
"); printf("* *
"); printf("*******************************
"); p2 = first; /* p2 */ if (p2 != NULL&&p2->next != NULL) { p2 = p2->next; /* 1 , , p2 */ } do { printf(" 1 "); scanf("%d", &i); if (i) /* */ { if (num == 15) { printf(" !"); /* , , num 15 , */ } else { printf("
( ; ; ( , , ); )
"); p1 = (person *)malloc(LEN); /*p1 */ scanf("%s%s%s%s", p1->name, p1->tel, p1->category, p1->email); /* */ p1->next = NULL; /*p1 */ if (first == NULL) { first = p1; } /* */ else { p2->next = p1; } /* */ p2 = p1; /* p2 p1 , */ num++; /* */ printf("
!
"); } } } while (i); /* 0 */ } void show() { person *p1; printf("
*************************************
"); printf("* *
"); printf("*************************************
"); p1 = first; /* first , p1 , */ while (p1 != NULL) { printf("%s\t%s\t%s\t%s

", p1->name, p1->tel, p1->category, p1->email); p1 = p1->next; /* */ } } void del() { int i, d; person *p1; person *p2; /* , , */ printf("
*************************************
"); printf("* *
"); printf("*************************************
"); do { p1 = first; printf("%s\t%s\t%s\t%s

", " ", " ", " ", " "); for (i = 0;iname, p1->tel, p1->category, p1->email); p1 = p1->next; } /* , */ printf("
:"); scanf("%d", &d); if (d) { if (d>0 && d <= num) { if (d == 1) { p2 = first; first = first->next; /* , , , */ free(p2); /* */ } else { d--; d--; /* d-- while p1 */ p1 = first; while (d--) { p1 = p1->next; /* p1 */ } p2 = p1->next; /*p2 */ p1->next = p2->next; /* p1 , p1next , */ free(p2); /* */ } num--; printf("
!

"); } else { printf("
, :
"); } } } while (d); /* */ } void search() { char sh[10]; int count = 0; /* count */ person *p1; /* p1 */ printf("
*************************************
"); printf("* *
"); printf("*************************************
"); do { printf("A. B. C.

"); printf(" , Q :"); scanf("%s", sh); p1 = first; /* p1 , p1 */ count = 0; if (sh[0] == 'A' || sh[0] == 'B' || sh[0] == 'C') /* , ABC */ { if (sh[0] == 'A') { while (p1 != NULL) { if (!strcmp(p1->category, " ")) /*strcmp , 0, !0 , if */ { if (!count) /* */ { printf(" :

"); printf("%s\t%s\t%s\t%s

", " ", " ", " ", " "); } printf("%s\t%s\t%s\t%s
", p1->name, p1->tel, p1->category, p1->email); /* */ count++; /* */ } p1 = p1->next; /* , */ } } else if (sh[0] == 'B') /* A */ { while (p1 != NULL) { if (!strcmp(p1->category, " ")) { if (!count) { printf(" :

"); printf("%s\t%s\t%s\t%s

", " ", " ", " ", " "); } printf("%s\t%s\t%s\t%s
", p1->name, p1->tel, p1->category, p1->email); count++; } p1 = p1->next; } } else if (sh[0] == 'C') /* A */ { while (p1 != NULL) { if (!strcmp(p1->category, " ")) { if (!count) { printf(" :

"); printf("%s\t%s\t%s\t%s

", " ", " ", " ", " "); } printf("%s\t%s\t%s\t%s
", p1->name, p1->tel, p1->category, p1->email); count++; } p1 = p1->next; } } if (!count) /* count 0 if */ { printf(" !
"); } else { printf("
%d ", count); } /* */ } else { printf(" !
"); } } while (sh[0] != 'Q'); /* Q */ } void modify() { int i, mdf; person *p1; printf("
*************************************
"); printf("* *
"); printf("*************************************
"); do { p1 = first; printf("%s\t%s\t%s\t%s

", " ", " ", " ", " "); for (i = 0;iname, p1->tel, p1->category, p1->email); p1 = p1->next; } /* , */ printf("
1-%d ; 0 :", num); scanf("%d", &mdf); if (mdf) { if (mdf>0 && mdf <= num) { printf("

( “ ( / / ) ” )

"); printf("%d. ", mdf); p1 = first; mdf--; while (mdf--) { p1 = p1->next; } /* -- p1 */ scanf("%s%s%s%s", p1->name, p1->tel, p1->category, p1->email); printf("


"); /* */ } else { printf("
, :
"); } } } while (mdf); /* */ } #if 0 void save() { person *p1; FILE *fp = fopen("addresslist.dat", "w"); /* , */ p1 = first; while (p1 != NULL) { fprintf(fp, "%s\t%s\t%s\t%s
", p1->name, p1->tel, p1->category, p1->email); p1 = p1->next; /* , , , printf f*/ } fclose(fp); /* */ printf("
!

"); } #endif #if 0 void save() { int i; person *p1; p1 = first; FILE *fp = fopen("addresslist.txt", "w"); /* , */ for (i = 0;inext; } fclose(fp); } #endif #if 0 void read() { int i; FILE *fp; person *p1, *p2; p1 = (person *)malloc(LEN); p2 = first; fp = fopen("addresslist.txt", "a+"); for (i = 0;i < num;i++) { fread(p1, LEN, 1, fp); p2 = p1; if (p2->next == NULL)break; p1 = (person *)malloc(LEN); p2->next = p1; } p2->next = NULL; fclose(fp); printf("
!"); } #endif #if 0 void read() { int i; FILE *fp; person *p1, *p2; p1 = (person *)malloc(LEN); p2 = first; fp = fopen("addresslist.txt", "rt"); for (i = 0;i < num;i++) { p1 = (person *)malloc(LEN); fread(p1, LEN, 1, fp); if (p2 == NULL)break; while (p2->next != NULL) { p2 = p2->next; } p2->next = p1; p1->next = NULL; } fclose(fp); printf("
!"); } #endif void save() { FILE *fp; person *p = first; int i; if ((fp = fopen("addresslist.txt", "wt")) == NULL) { printf("cannot open file
"); return; } while (p != NULL) { fprintf(fp, "%s %s %s %s
", p->name, p->email, p->tel, p->category); p = p->next; } fclose(fp); } void read() { FILE *fp; person *p = first; person * newnode; int i = 1; if ((fp = fopen("addresslist.txt", "rt")) == NULL) { printf("cannot open file
"); return; } while (i > 0) { newnode = (person *)malloc(LEN); i = fscanf(fp, "%s %s %s %s
", newnode->name, newnode->email, newnode->tel, newnode->category); if (i == EOF) { free(newnode); newnode = NULL; break; } printf("%s %s %s %s
", newnode->name, newnode->email, newnode->tel, newnode->category); if (p == NULL) { p = newnode; first = newnode; p ->next = NULL; } else { newnode->next = p->next; p->next = newnode; p = p->next; } } fclose(fp); return; } void quit() { exit(0); /* , , */ }

反省:
1.やはり正しいエラーデバッグに慣れていない、printf("%d"、——LINE——);
2.グローバルチェーンヘッダ変数、関数では、一時的なポインタが彼を指すことを定義する必要がありますが、関数で一時的なポインタのポインタが変更されないことを確認する必要があります.そうすれば、関数の操作が無効になり、NULL(このプログラム)になります.
...