B
bbcat
To practice C language,I began to develop a simple project which
implements the management of students' information.I have worked it for
one week. I plan to finish it for two weeks.
Are there some experienced friends who would like to read my source
code and give some advice to me.tks
---------------------------------------------------------
main.c
---------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "logic.h"
const char *filename = "student.dat";
main(void)
{
int menu_id = 99;
int student_num = 0;
struct node *phead = NULL;
void load_menu();
void func_menu_new(struct node *phead);// new a student
void func_menu_search(struct node *phead);
void func_menu_edit(struct node *phead);
void func_menu_print(struct node *phead);
void func_menu_exit(struct node *phead);
load_menu();
phead = init();
if(read_datafile(phead,filename) == 1)
{
printf(" The file %s doesn't exist! \n",filename);
}
student_num = get_student_num(phead);
printf(" there are %d students' information.\n",student_num);
tag_start: scanf("%d",&menu_id);
switch (menu_id)
{
case 0:
func_menu_exit(phead);
break;
case 1:
func_menu_new(phead);
break;
case 2:
func_menu_search(phead);
break;
case 3:
func_menu_edit(phead);
break;
case 4:
func_menu_print(phead);
break;
default:
printf("Sorry,Only 0--4 can be chosen,please input again \n");
}
if(menu_id !=0)
goto tag_start;
}
void load_menu()
{
clrscr();
printf("\n");
printf("\n");
printf(" Welcome to Use Student System \n");
printf(" =====================menu=====================\n");
printf(" Input a student's informations..................1\n");
printf(" Search student's informations...................2\n");
printf(" Modify student's informations...................3\n");
printf(" Print student's informations....................4\n");
printf(" exit system.....................................0\n");
printf("\n");
}
void func_menu_new(struct node *phead)
{
struct student *pnewstudent = (struct student *)(sizeof(struct
student));
printf("please input a new student's information\n");
scanf("%d",&(pnewstudent->id));
scanf("%s",pnewstudent->name);
student_add(phead,pnewstudent);
printf("you have input a student's informations successfully!\n");
free(pnewstudent);
pnewstudent = NULL;
}
void func_menu_print(struct node *phead)
{
struct node *ptemp;
int student_num;
ptemp = phead;
student_num = get_student_num(phead);
printf(" All Student Information\n");
printf(" =================================\n");
printf(" there are %d students' information.\n",student_num);
printf("| Student ID ");
printf("|");
printf(" Student name |\n");
while(ptemp->next != NULL)
{
printf("| %10d ",ptemp->next->stu_info.id);
printf("| %14s |\n",ptemp->next->stu_info.name);
ptemp = ptemp->next;
}
ptemp = NULL;
}
void func_menu_exit(struct node *phead)
{
if(save_to_datafile(phead,filename) == 0)
{
printf(" All students' informations are saved!");
}
free_memo(phead);
}
void func_menu_search(struct node *phead)
{
int search_type;
int search_id;
char search_name[20];
struct node *psearch;
printf("Search by what?\n");
printf(" 1: student ID\n");
printf(" 2: student name\n");
scanf("%d",&search_type);
while((search_type != 1) && (search_type != 2))
{
printf("sorry, please choose 1 or 2\n");
scanf("%d",&search_type);
}
switch (search_type)
{
case 1:
{
printf("please input the student's ID\n");
scanf("%d",&search_id);
psearch = search_by_id(search_id,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",search_id);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
}
break;
case 2:
{
printf("please input the student's name\n");
scanf("%s",search_name);
psearch = search_by_name(search_name,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with name =
%d\n",search_name);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
}
break;
default:
printf("error occurs");
}
psearch = NULL;
}
void func_menu_edit(struct node *phead)
{
int stu_id,stu_id_new;
char stu_name_new[20];
struct node *psearch;
printf(" Please input the ID of the student you want to edit.\n");
scanf("%d",&stu_id);
psearch = search_by_id(stu_id,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",stu_id);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
printf(" Please input the student's new information!\n");
scanf("%d",&stu_id_new);
scanf("%s",stu_name_new);
psearch->stu_info.id = stu_id_new;
strcpy(psearch->stu_info.name,stu_name_new);
printf("You update a student's information successfully!\n");
psearch = NULL;
}
--------------------------------------------------------
logic.c
-------------------------------------------------------
#include "logic.h"
#include <stdio.h>
struct node * init()
{
struct node * ptemp;
ptemp = (struct node*)malloc(sizeof(struct node));
ptemp->next = NULL;
return ptemp;
}
void student_add(struct node *phead,struct student *pnewstudent)
{
struct node *pnew,*ptemp;
ptemp = phead;
pnew = (struct node*)malloc(sizeof(struct node));
pnew->stu_info.id = pnewstudent->id;
strcpy(pnew->stu_info.name,pnewstudent->name);
while(ptemp->next != NULL)
{
ptemp = ptemp->next;
}
ptemp->next = pnew;
pnew->next = NULL;
pnew = NULL;
ptemp = NULL;
}
int get_student_num(struct node *phead)
{
struct node *ptemp;
int student_num = 0;
ptemp = phead->next;
while(ptemp != NULL)
{
ptemp = ptemp->next;
student_num ++;
}
ptemp = NULL;
return student_num;
}
int save_to_datafile(struct node *phead,char *filename)
{
struct node *ptemp;
int student_num;
FILE *fp;
ptemp = phead;
student_num = get_student_num(phead);
fp=fopen(filename, "w");
fprintf(fp,"%d\n",student_num);
while(ptemp->next != NULL)
{
fprintf(fp, "%d", ptemp->next->stu_info.id);
fprintf(fp, "%s\n", ptemp->next->stu_info.name);
ptemp = ptemp->next;
}
ptemp = NULL;
fclose(fp);
return 0;
}
int read_datafile(struct node *phead,char *filename)
{
struct node *pnew,*ptemp;
FILE *fp;
int student_num;
ptemp = phead;
fp = fopen(filename, "r");
if(fp == NULL)
{
return 1;
}
fscanf(fp,"%d",&student_num);
while(student_num > 0)
{
pnew = (struct node*)malloc(sizeof(struct node));
fscanf(fp,"%d",&(pnew->stu_info.id));
fscanf(fp,"%s",pnew->stu_info.name);
ptemp->next = pnew;
ptemp = ptemp->next;
student_num --;
}
ptemp = NULL;
pnew = NULL;
fclose(fp);
return 0;
}
void free_memo(struct node *phead)
{
struct node *ptemp,*pnext;
ptemp = phead->next;
while(ptemp != NULL)
{
pnext = ptemp->next;
free(ptemp);
ptemp = pnext;
}
free(phead);
phead = NULL;
ptemp = NULL;
pnext = NULL;
}
struct node * search_by_id(int search_id,struct node *phead)
{
int id = search_id;
struct node *ptemp,*psearch;
psearch = NULL;
ptemp = phead->next;
while(ptemp != NULL)
{
if(ptemp->stu_info.id == id)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}
return psearch;
}
struct node * search_by_name(char search_name[20],struct node *phead)
{
struct node *ptemp,*psearch;
psearch = NULL;
ptemp = phead->next;
while(ptemp != NULL)
{
if(strcmp(ptemp->stu_info.name,search_name) == 0)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}
return psearch;
}
-----------------------------------------------------------
logic.h
-----------------------------------------------------
#ifndef LOGIC_H
#define LOGIC_H
#define LEN sizeof(struct student)
struct student
{
int id;
char name[20];
};
struct node
{
struct student stu_info;
struct node *next;
};
extern struct node * init();
extern int read_datafile(struct node *phead,char *filename);
extern int get_student_num(struct node *phead);
extern int save_to_datafile(struct node *phead,char *filename);
extern void student_add(struct node *phead,struct student
*pnewstudent);
extern void free_memo(struct node *phead);
extern struct node * search_by_id(int search_id,struct node *phead);
extern struct node * search_by_name(char search_name[20],struct node
*phead);
#endif
implements the management of students' information.I have worked it for
one week. I plan to finish it for two weeks.
Are there some experienced friends who would like to read my source
code and give some advice to me.tks
---------------------------------------------------------
main.c
---------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "logic.h"
const char *filename = "student.dat";
main(void)
{
int menu_id = 99;
int student_num = 0;
struct node *phead = NULL;
void load_menu();
void func_menu_new(struct node *phead);// new a student
void func_menu_search(struct node *phead);
void func_menu_edit(struct node *phead);
void func_menu_print(struct node *phead);
void func_menu_exit(struct node *phead);
load_menu();
phead = init();
if(read_datafile(phead,filename) == 1)
{
printf(" The file %s doesn't exist! \n",filename);
}
student_num = get_student_num(phead);
printf(" there are %d students' information.\n",student_num);
tag_start: scanf("%d",&menu_id);
switch (menu_id)
{
case 0:
func_menu_exit(phead);
break;
case 1:
func_menu_new(phead);
break;
case 2:
func_menu_search(phead);
break;
case 3:
func_menu_edit(phead);
break;
case 4:
func_menu_print(phead);
break;
default:
printf("Sorry,Only 0--4 can be chosen,please input again \n");
}
if(menu_id !=0)
goto tag_start;
}
void load_menu()
{
clrscr();
printf("\n");
printf("\n");
printf(" Welcome to Use Student System \n");
printf(" =====================menu=====================\n");
printf(" Input a student's informations..................1\n");
printf(" Search student's informations...................2\n");
printf(" Modify student's informations...................3\n");
printf(" Print student's informations....................4\n");
printf(" exit system.....................................0\n");
printf("\n");
}
void func_menu_new(struct node *phead)
{
struct student *pnewstudent = (struct student *)(sizeof(struct
student));
printf("please input a new student's information\n");
scanf("%d",&(pnewstudent->id));
scanf("%s",pnewstudent->name);
student_add(phead,pnewstudent);
printf("you have input a student's informations successfully!\n");
free(pnewstudent);
pnewstudent = NULL;
}
void func_menu_print(struct node *phead)
{
struct node *ptemp;
int student_num;
ptemp = phead;
student_num = get_student_num(phead);
printf(" All Student Information\n");
printf(" =================================\n");
printf(" there are %d students' information.\n",student_num);
printf("| Student ID ");
printf("|");
printf(" Student name |\n");
while(ptemp->next != NULL)
{
printf("| %10d ",ptemp->next->stu_info.id);
printf("| %14s |\n",ptemp->next->stu_info.name);
ptemp = ptemp->next;
}
ptemp = NULL;
}
void func_menu_exit(struct node *phead)
{
if(save_to_datafile(phead,filename) == 0)
{
printf(" All students' informations are saved!");
}
free_memo(phead);
}
void func_menu_search(struct node *phead)
{
int search_type;
int search_id;
char search_name[20];
struct node *psearch;
printf("Search by what?\n");
printf(" 1: student ID\n");
printf(" 2: student name\n");
scanf("%d",&search_type);
while((search_type != 1) && (search_type != 2))
{
printf("sorry, please choose 1 or 2\n");
scanf("%d",&search_type);
}
switch (search_type)
{
case 1:
{
printf("please input the student's ID\n");
scanf("%d",&search_id);
psearch = search_by_id(search_id,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",search_id);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
}
break;
case 2:
{
printf("please input the student's name\n");
scanf("%s",search_name);
psearch = search_by_name(search_name,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with name =
%d\n",search_name);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
}
break;
default:
printf("error occurs");
}
psearch = NULL;
}
void func_menu_edit(struct node *phead)
{
int stu_id,stu_id_new;
char stu_name_new[20];
struct node *psearch;
printf(" Please input the ID of the student you want to edit.\n");
scanf("%d",&stu_id);
psearch = search_by_id(stu_id,phead);
if(psearch == NULL)
{
printf("sorry there are no student infomation with ID
=%d\n",stu_id);
}
else
{
printf(" %d :",psearch->stu_info.id);
printf(" %s\n",psearch->stu_info.name);
}
printf(" Please input the student's new information!\n");
scanf("%d",&stu_id_new);
scanf("%s",stu_name_new);
psearch->stu_info.id = stu_id_new;
strcpy(psearch->stu_info.name,stu_name_new);
printf("You update a student's information successfully!\n");
psearch = NULL;
}
--------------------------------------------------------
logic.c
-------------------------------------------------------
#include "logic.h"
#include <stdio.h>
struct node * init()
{
struct node * ptemp;
ptemp = (struct node*)malloc(sizeof(struct node));
ptemp->next = NULL;
return ptemp;
}
void student_add(struct node *phead,struct student *pnewstudent)
{
struct node *pnew,*ptemp;
ptemp = phead;
pnew = (struct node*)malloc(sizeof(struct node));
pnew->stu_info.id = pnewstudent->id;
strcpy(pnew->stu_info.name,pnewstudent->name);
while(ptemp->next != NULL)
{
ptemp = ptemp->next;
}
ptemp->next = pnew;
pnew->next = NULL;
pnew = NULL;
ptemp = NULL;
}
int get_student_num(struct node *phead)
{
struct node *ptemp;
int student_num = 0;
ptemp = phead->next;
while(ptemp != NULL)
{
ptemp = ptemp->next;
student_num ++;
}
ptemp = NULL;
return student_num;
}
int save_to_datafile(struct node *phead,char *filename)
{
struct node *ptemp;
int student_num;
FILE *fp;
ptemp = phead;
student_num = get_student_num(phead);
fp=fopen(filename, "w");
fprintf(fp,"%d\n",student_num);
while(ptemp->next != NULL)
{
fprintf(fp, "%d", ptemp->next->stu_info.id);
fprintf(fp, "%s\n", ptemp->next->stu_info.name);
ptemp = ptemp->next;
}
ptemp = NULL;
fclose(fp);
return 0;
}
int read_datafile(struct node *phead,char *filename)
{
struct node *pnew,*ptemp;
FILE *fp;
int student_num;
ptemp = phead;
fp = fopen(filename, "r");
if(fp == NULL)
{
return 1;
}
fscanf(fp,"%d",&student_num);
while(student_num > 0)
{
pnew = (struct node*)malloc(sizeof(struct node));
fscanf(fp,"%d",&(pnew->stu_info.id));
fscanf(fp,"%s",pnew->stu_info.name);
ptemp->next = pnew;
ptemp = ptemp->next;
student_num --;
}
ptemp = NULL;
pnew = NULL;
fclose(fp);
return 0;
}
void free_memo(struct node *phead)
{
struct node *ptemp,*pnext;
ptemp = phead->next;
while(ptemp != NULL)
{
pnext = ptemp->next;
free(ptemp);
ptemp = pnext;
}
free(phead);
phead = NULL;
ptemp = NULL;
pnext = NULL;
}
struct node * search_by_id(int search_id,struct node *phead)
{
int id = search_id;
struct node *ptemp,*psearch;
psearch = NULL;
ptemp = phead->next;
while(ptemp != NULL)
{
if(ptemp->stu_info.id == id)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}
return psearch;
}
struct node * search_by_name(char search_name[20],struct node *phead)
{
struct node *ptemp,*psearch;
psearch = NULL;
ptemp = phead->next;
while(ptemp != NULL)
{
if(strcmp(ptemp->stu_info.name,search_name) == 0)
{
psearch = ptemp;
}
ptemp = ptemp->next;
}
return psearch;
}
-----------------------------------------------------------
logic.h
-----------------------------------------------------
#ifndef LOGIC_H
#define LOGIC_H
#define LEN sizeof(struct student)
struct student
{
int id;
char name[20];
};
struct node
{
struct student stu_info;
struct node *next;
};
extern struct node * init();
extern int read_datafile(struct node *phead,char *filename);
extern int get_student_num(struct node *phead);
extern int save_to_datafile(struct node *phead,char *filename);
extern void student_add(struct node *phead,struct student
*pnewstudent);
extern void free_memo(struct node *phead);
extern struct node * search_by_id(int search_id,struct node *phead);
extern struct node * search_by_name(char search_name[20],struct node
*phead);
#endif