student.rar

  • PUDN用户
    了解作者
  • C/C++
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 2
    下载次数
  • 2013-03-09 15:00
    上传日期
MANAGEMENT C SOURCE code
student.rar
  • student.c
    5.6KB
内容介绍
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> typedef struct sinhvien{ int StudentID; char Name[30]; char Class[10]; char Department[30]; struct sinhvien *next; } sinhvien; sinhvien *danhsach; FILE *log; char temp[40]; char *thoigian; //loai bo dau xuong dong void docfile(); void ghifile(sinhvien *listsv); void in(sinhvien *listsv); void add(sinhvien **listsv,int StudentID,char Name[30],char Class[30],char Department[30]); void append(); sinhvien *find(sinhvien *listsv,int StudentID); void timkiem(); void laytime(); void sua(); sinhvien *del(sinhvien *currP,int StudentID); void xoa(); int main() { int n; laytime(); log=fopen("log.txt","w"); docfile(); do { printf("\n MENU\n"); printf("1-Add new student\n2-Search\n3-Moddify\n4-Delete\n5-Print list\n6-Exit\n"); printf("\nChoose:"); scanf("%d",&n); while(getchar()!='\n'); switch(n) { case 1: append();break; case 2: timkiem();break; case 3: sua();break; case 4: xoa();break; case 5: in(danhsach);break; case 6: fclose(log);ghifile(danhsach);return 0;break; default: return 0; } } while(n!=6); } void docfile() { FILE *f,*g; char s[1000]; char *ids,*ten,*lop,*khoa; int id; f=fopen("students.txt","r"); if (f==NULL) {g=fopen("students.txt","w"); printf("students.txt is not found. Created.\n"); fclose(g);return;} while(!feof(f)) { fgets(s,1000,f); if(feof(f)) break; ids=strtok(s,":"); if (ids==NULL) ids="-1"; id=atoi(ids); ten=strtok(NULL,":"); if (ten==NULL) ten="unknown"; lop=strtok(NULL,":"); if (lop==NULL) lop="unknown"; khoa=strtok(NULL,"\n"); if (khoa==NULL) khoa="unknown"; add(&danhsach,id,ten,lop,khoa); } fclose(f); } void ghifile(sinhvien *listsv) { FILE *ghi; sinhvien *curr; ghi=fopen("students.txt","w"); curr=listsv; while (curr!=NULL) { fprintf(ghi,"%d:%s:%s:%s\n",curr->StudentID,curr->Name,curr->Class,curr->Department); curr=curr->next; } fclose(ghi); } void add(sinhvien **listsv,int StudentID,char Name[30],char Class[30],char Department[30]) { sinhvien *newsv; sinhvien *curr; newsv=(sinhvien*)malloc(sizeof(sinhvien)); newsv->StudentID=StudentID; strcpy(newsv->Name,Name); strcpy(newsv->Class,Class); strcpy(newsv->Department,Department); newsv->next=NULL; curr=*listsv; if ((*listsv)==NULL) (*listsv)=newsv; else { while(curr->next!=NULL) curr=curr->next; curr->next=newsv; } } void append() { int StudentID; char Name[30]; char Class[10]; char Department[30]; printf("Enter student ID:"); scanf("%d",&StudentID); while(getchar()!='\n'); printf("Enter Name:"); gets(Name); printf("Enter Class:"); gets(Class); printf("Enter Department:"); gets(Department); add(&danhsach,StudentID,Name,Class,Department); laytime(); fprintf(log,"%s----Append----ID:%d\n",thoigian,StudentID); } void in(sinhvien *listsv) { sinhvien *curr; int length=0; curr=listsv; printf("List of students:\n\n"); while (curr!=NULL) { printf("%d---%s---%s---%s\n",curr->StudentID,curr->Name,curr->Class,curr->Department); length++; curr=curr->next; } } void laytime() { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); strcpy(temp,asctime (timeinfo)); thoigian=strtok(temp,"\n"); } sinhvien *find(sinhvien *listsv,int StudentID) { sinhvien *curr; curr=listsv; while(curr!=NULL) { if (curr->StudentID==StudentID) return curr; else curr=curr->next; } return NULL; } void timkiem() { sinhvien *cantim; int StudentID; printf("Enter the student ID to search:"); scanf("%d",&StudentID); while(getchar()!='\n'); cantim=find(danhsach,StudentID); if (cantim==NULL) {printf("Not found.\n");return;} else { printf("%d---%s---%s---%s\n",cantim->StudentID,cantim->Name,cantim->Class,cantim->Department); } } void sua() { sinhvien *cansua; int StudentID; int chon; printf("Enter the StudentID to moddify:"); scanf("%d",&StudentID); while(getchar()!='\n'); cansua=find(danhsach,StudentID); if (cansua==NULL) {printf("Not found.");return;} else do { printf("Want to moddify ?\n"); printf("\n1-ID\n2-Name\n3-Class\n4-Department\n5-Exit\n"); printf("Choose:");scanf("%d",&chon); while(getchar()!='\n'); switch(chon){ case 1: printf("Enter the student ID:");scanf("%d",&cansua->StudentID); while(getchar()!='\n'); laytime(); fprintf(log,"%s----Moddify----ID:%d\n",thoigian,StudentID);break; case 2: printf("Enter Name:");gets(cansua->Name); laytime(); fprintf(log,"%s----Moddify----ID:%d\n",thoigian,StudentID);break; case 3: printf("Enter Class:");gets(cansua->Class); laytime(); fprintf(log,"%s----Moddify----ID:%d\n",thoigian,StudentID);break; case 4: printf("Enter Department:");gets(cansua->Department); laytime(); fprintf(log,"%s----Moddify----ID:%d\n",thoigian,StudentID);break; case 5: break; default: break; } }while (chon!=5); } sinhvien *del(sinhvien *currP,int StudentID) { if (currP==NULL) return NULL; if (currP->StudentID==StudentID) { sinhvien *tempNextP; tempNextP=currP->next; free(currP); return tempNextP; } currP->next=del(currP->next,StudentID); return currP; } void xoa() { int StudentID; printf("Enter student ID to delete:");scanf("%d",&StudentID); while(getchar()!='\n'); danhsach=del(danhsach,StudentID); laytime(); fprintf(log,"%s----Delete----ID:%d\n",thoigian,StudentID); ghifile(danhsach); }
评论
    相关推荐