C语言—学生管理系统

2018.11.14
将链表作为内存数据模型,将文件作为数据库,将终端作为交互界面。
读文件生成链表,修改链表写入文件。
1.初始化现有数据。
2.读文件生成链表。
3.操作链表(增、查、改、删、排序)。
4.写链表到文件。
(Linux下vim编辑器)

  1 #include <stdio.h>                                        
  2 #include <stdlib.h>
  3 #include <string.h>
  4 typedef struct student
  5 {
  6     char name[100];
  7     char sex;
  8     int age;
  9     double score;
 10 }Stu;
 11 typedef struct stuList
 12 {
 13     Stu data;
 14     struct stuList *next;
 15 }Node;
 16 void creList(Node*head,char *filePath)
 17 {
 18     FILE*pr=fopen(filePath,"r+");
 19     if(NULL==pr)
 20         exit(-1);
 21     Node*cur=(Node*)malloc(sizeof(Node));
 22     if(NULL==cur)
 23         exit(-1);
 24     while(fread(&cur->data,sizeof(Stu),1,pr))
 25     {
 26         cur->next=head->next;
 27         head->next=cur;
 28         cur=(Node*)malloc(sizeof(Node));
 29     }
 30     free(cur);
 31     fclose(pr);
 32 }
 33 void initFile()
 34 {
 35     Stu s[3]=
 36     {
 37         {"wang",'x',21,99},
 38         {"zhao",'x',22,98},
 39         {"han",'y',23,100}
 40     };
 41     FILE*pw=fopen("management.txt","w+");        
 42     if(NULL==pw)
 43         exit(-1);
 44     fwrite(s,sizeof(s),1,pw);
 45     fclose(pw);
 46 }
 47 
 48 void show(Node *head)
 49 {
 50     head=head->next;
 51     while(head)
 52     {   
 53         printf("\t%s\t%c\t%d\t%.2f\n",head->data.name,head    ->data.sex,head->data.age,head->data.score);
 54         head=head->next;
 55     }
 56 }
 57 
 58 void addListStu(Node*head)
 59 {
 60     Node*cur=(Node *)malloc(sizeof(Node));
 61     if(NULL==cur)
 62         exit(-1);
 63     printf("name:\n");
 64     scanf("%s",cur->data.name);
 65     getchar();
 66     printf("sex:\n");
 67     scanf("%c",&cur->data.sex);
 68     printf("age:\n");
 69     scanf("%d",&cur->data.age);
 70     printf("score:\n");
 71     scanf("%lf",&cur->data.score);
 72     
 73     cur->next=head->next;
 74     head->next=cur;
 75 }
 76 
 77 Node *searchListStu(Node *head)
 78 {
 79     char name[100];
 80     scanf("%s",name);
 81     head=head->next;
 82     while(head)
 83     {
 84         if(!strcmp(head->data.name,name))   
 85         {
 86             break;
 87         }
 88         head=head->next;
 89     }
 90     return head;
 91 }
 92 void deleteListStu(Node *head)
 93 {
 94     Node*phead=searchListStu(head);
 95     if(phead->next==NULL)
 96     {
 97         while(head->next!=phead)
 98         {
 99             head=head->next;
100         }
101         head->next=phead->next;
102         free(phead);
103         phead=NULL;
104     }
105     else
106     {
107         Node*t=phead->next;
108         phead->data=phead->next->data;
109         phead->next=phead->next->next;
110         free(t);
111     }
112 }
113 int lenListStu(Node*head)
114 {
115     int len=0;
116     head=head->next;
117     while(head)
118     {
119         len++;
120         head=head->next;
121     }                
122    return len;
123 }
124 void sortListStu(Node*head)
125 {
126     int len=lenListStu(head);
127     Node*prep,*p,*q,*t;
128     for(int i=0;i<len-1;i++)
129     {   
130         prep=head;
131         p=prep->next;
132         q=p->next;
133         
134         for(int j=0;j<len-1-i;j++)
135         {   
136             if((p->data.score)<(q->data.score))
137             {   
138                 prep->next=q;
139                 p->next=q->next;
140                 q->next=p;
141                 
142                 t=p;
143                 p=q;
144                 q=t;
145             }
146             prep=prep->next;
147             p=p->next;
148             q=q->next;
149         }
150     }
151 }
152 void saveListStu(Node*head,char *fileName)
153 {
154     FILE*pr=fopen(fileName,"w+");
155     if(NULL==pr)
156         exit(-1);
157     head=head->next;
158     while(head)
159     {
160         fwrite((&head->data),sizeof(Stu),1,pr);
161         head=head->next;
 162    }
163     fclose(pr);
164 }
165 void freeList(Node*head) 
166 {
167     Node *t=NULL;
168     head=head->next;
169     while(head)
170     {
171         t=head;
172         head=head->next;
173         free(t);
174     }
175 }
176 void modifyListStu(Node*head)
177 {
178     Node*phead=searchListStu(head);
179     printf("input score:\n");
180     scanf("%lf",&phead->data.score);
181 }
182 int main()
183 {
184     Node*head=(Node*)malloc(sizeof(Node));
185     if(NULL==head)
186         exit(-1);
187     head->next=NULL;
188     Node*phead;
189 
190     initFile();//结构体初始化以及写入
191     creList(head,"management.txt");//将文件内容添加到链表
192     while(1)
193     {
194         printf("\t\tStudent Management!\n");
195         printf("\tname\tsex\tage\tscore\n");
196         show(head);
197         printf("1.add\t2.search\t3.delete\t4.modify\t5.sor    t\t6.exit\n");
198 
199         int choice=0;
200         scanf("%d",&choice);
201         switch(choice)   
202          {
203             case 1:
204                 addListStu(head);
205                 break;
206             case 2:
207                 if(phead=searchListStu(head))
208                 {
209                     printf("\t%s\t%c\t%d\t%.2f\n",phead->d    ata.name,phead->data.sex,phead-    >data.age,phead->data.score);
210                 }
211                 else
212                 {
213                     printf("NO peole!");
214                 }
215                 break;
216             case 3:
217                 deleteListStu(head);
218                 break;
219             case 4:
220                 modifyListStu(head);
221                 break;
222             case 5:
223                 sortListStu(head);
224                 break;
225             case 6:
226                 saveListStu(head,"management.txt");
227                 freeList(head);
228                 return 0;
229                 break;
230             default:
231                 printf("输入错误!\n");
232         }
233     }
234     return 0;
235 }

在这里插入代码片

1.

猜你喜欢

转载自blog.csdn.net/qq_42972267/article/details/84072244