main.c文件
#include "Mylist.h"
void menu();
struct Node *list;
void keyDown();
int main(int argc, char *argv[]) {
list = createList();
readInfoFromFile(list, "1.txt");
while(1){
menu();
keyDown();
system("pause");
system("cls");
}
system("pause");
return 0;
}
void menu()
{
printf("--------------【學生管理系統】-----------------\n");
printf("\t\t0. 退出系統\n");
printf("\t\t1. 錄入信息\n");
printf("\t\t2. 插入信息\n");
printf("\t\t3. 瀏覽信息\n");
printf("\t\t4. 按學號瀏覽信息\n");
printf("\t\t5. 修改信息\n");
printf("\t\t6. 刪除信息\n");
printf("\t\t7. 查找信息\n");
printf("\t\t8. 成績排序\n");
printf("\t\t9. 成績統計\n");
printf("-----------------------------------------------\n");
}
void keyDown()
{
int choice = 0;
struct student data;
struct Node *pMove = NULL;
scanf("%d", &choice);
switch(choice){
case 0:
printf("正常退出\n");
system("pause");
exit(0);
break;
case 1:
printf("----------------【錄入信息】----------------\n");
printf("請輸入學生學號,姓名,成績:");
fflush(stdin);
scanf("%s%s%d",data.xuehao, data.name, &data.score ) ;
insertNodeByHead(list, data);
break;
case 2:
printf("----------------【插入信息】-------------------\n");
printf("請輸入插入學生學號,姓名,成績:");
fflush(stdin);
scanf("%s%s%d",data.xuehao, data.name, &data.score );
printf("插在學號爲多少的後面");
char xuehao1[20];
fflush(stdin);
scanf("%s", xuehao1) ;
insert(list, data, xuehao1);
break;
case 3:
printf("----------------【瀏覽信息】-------------------\n");
printList(list);
break;
case 4:
printf("----------------【按學號瀏覽信息】-------------------\n");
sort_score(list);
printList(list);
break;
case 5:
printf("----------------【修改信息】-------------------\n");
printf("請輸入需要修改的學生姓名:");
scanf("%s", data.name);
modification(list, data.name);
break;
case 6:
printf("----------------【刪除信息】-------------------\n");
printf("請輸入刪除的學生姓名:");
scanf("%s", data.name);
deleteAppoinNode(list, data.name);
break;
case 7:
printf("----------------【查找信息】-------------------\n");
printf("請輸入要查找學生的學號:");
scanf("%s", data.xuehao);
pMove = searchInfoByData(list, data.xuehao);
if((pMove) == NULL){
printf("未找到相關信息!\n");
system("pause");
}
else{
printf("學號\t姓名\t成績\n");
printf("%s\t%s\t%d\n", pMove->data.xuehao, pMove->data.name, pMove->data.score );
}
break;
case 8:
printf("----------------【成績排序】-------------------\n");
sort(list);
printList(list);
break;
case 9:
printf("----------------【成績統計】-------------------\n");
count(list);
break;
default:
printf("選擇錯誤,重新輸入\n");
system("pause");
break;
}
writeInfoToFile(list, "1.txt");
}
Mylist.h文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
char xuehao[20];
char name[50];
int score;
};
struct Node{
struct student data;
struct Node*next;
};
struct Node *createList(){
struct Node *headNode = (struct Node *)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
struct Node* createNode(struct student data){
struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNodeByHead(struct Node*headNode, struct student data){
struct Node*newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteAppoinNode(struct Node*headNode, char*name){
struct Node*posNode = headNode->next;
struct Node*posFrontNode = headNode;
if(posNode == NULL){
printf("數據爲空,無法刪除!\n");
return;
}
while(strcmp(posNode->data.name, name)){
posFrontNode = posNode;
posNode = posFrontNode->next;
if(posNode ==NULL){
printf("未找到指定位置無法刪除!\n");
return ;
}
}
posFrontNode->next = posNode->next;
free(posNode);
}
void readInfoFromFile(struct Node*headNode, char *fileName){
FILE *fp;
struct student data;
fp = fopen(fileName, "r");
if(fp == NULL){
fp = fopen(fileName, "w+");
}
while(fscanf(fp, "%s\t%s\t%d\n", data.xuehao, data.name, &data.score) != EOF){
insertNodeByHead(headNode, data);
}
fclose(fp);
}
void writeInfoToFile(struct Node*headNode, char*fileName){
FILE *fp;
fp = fopen(fileName, "w");
if(fp == NULL){
printf("文件打開失敗!");
return ;
}
struct Node *pMove = headNode->next;
while(pMove){
fprintf(fp, "%s\t%s\t%d\n",pMove->data.xuehao, pMove->data.name, pMove->data.score);
pMove = pMove->next;
}
fclose(fp);
}
struct Node *modification (struct Node* headNode, char* name){
struct student data;
deleteAppoinNode(headNode, name);
printf("請重新輸入學生學號,姓名,成績::");
fflush(stdin);
scanf("%s%s%d",data.xuehao, data.name, &data.score) ;
insertNodeByHead(headNode, data);
}
struct Node *searchInfoByData(struct Node* headNode, char *xuehao){
struct Node *pMove = headNode->next;
while(strcmp(pMove->data.xuehao, xuehao)){
pMove = pMove->next;
if(pMove == NULL){
return NULL;
}
}
return pMove;
}
void insert(struct Node*headNode, struct student data, char *xuehao) {
struct Node*newNode = createNode(data);
struct Node *pMove = searchInfoByData(headNode, xuehao);
newNode->next = pMove->next;
pMove->next = newNode;
}
void exchange(struct Node*now, struct Node*stop){
struct Node*temp = (struct Node*)malloc(sizeof(struct Node));
strcpy(temp->data.xuehao, stop->data.xuehao);
strcpy(temp->data.name, stop->data.name);
temp->data.score = stop->data.score;
strcpy(stop->data.xuehao, now->data.xuehao);
strcpy(stop->data.name, now->data.name);
stop->data.score = now->data.score;
strcpy(now->data.xuehao , temp->data.xuehao);
strcpy(now->data.name , temp->data.name);
now->data.score = temp->data.score;
free(temp);
}
void sort(struct Node*headNode){
struct Node*now = headNode->next->next;
struct Node*stop = headNode->next;
while(stop->next){
now = stop->next;
while(now){
if(stop->data.score < now->data.score){
exchange(now, stop);
}
now = now->next;
}
stop = stop->next;
}
}
void sort_score(struct Node*headNode){
struct Node*now = headNode->next->next;
struct Node*stop = headNode->next;
while(stop->next){
now = stop->next;
while(now){
long a = atol(stop->data.xuehao);
long b = atol(now->data.xuehao);
if(a > b){
exchange(now, stop);
}
now = now->next;
}
stop = stop->next;
}
}
int *count(struct Node*headNode){
struct Node*now = headNode->next;
sort(headNode);
int score[5]={0};
while( now ) {
if(now->data.score >= 90){
score[0]++;
}else if(now->data.score < 90 && now->data.score >= 80){
score[1]++;
}else if(now->data.score < 80 && now->data.score >=70){
score[2]++;
}else if(now->data.score < 70 && now->data.score >=60){
score[3]++;
}else if(now->data.score < 60){
score[4]++;
}
now = now->next;
}
printf("優秀: %d人\n", score[0]);
printf("良好: %d人\n", score[1]);
printf("中等: %d人\n", score[2]);
printf("及格: %d人\n", score[3]);
printf("不及格:%d人\n", score[4]);
}
void printList(struct Node*headNode){
struct Node*pMove = headNode->next;
printf("學號\t姓名\t成績\n");
while(pMove){
printf("%s\t%s\t%d\n", pMove->data.xuehao, pMove->data.name, pMove->data.score );
pMove = pMove->next;
}
printf("\n");
}