代码
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <string.h>
#include <stdlib.h>enum
{READ_ONLY,HIDE,SYSTEM = 55,TEST
};typedef struct SUser
{int nNumb;char sName[20];float fMath;
}DATA;typedef struct SNode
{DATA data;SNode* pNext;
}NODE,*PNODE;SNode* g_pHead = NULL;void Save();
void Print();
void AddTail(DATA data)
{SNode* pNew = (SNode*)malloc(sizeof(SNode));pNew->data = data;pNew->pNext = NULL;if (!g_pHead){g_pHead = pNew;return;}SNode* p = g_pHead;while (p->pNext)p = p->pNext;p->pNext = pNew;
}void AddHead(DATA data)
{SNode* p = (SNode*)malloc(sizeof(SNode));p->data = data;p->pNext = g_pHead;g_pHead = p;
}void Modify()
{int nNumb;printf("请输入要修改的学号:");scanf_s("%d", &nNumb);SNode* p = g_pHead;while (p){if (p->data.nNumb == nNumb)break;p = p->pNext;}if (!p){puts("你输入的学号不存在!");system("pause");return;}printf("%d\t%s\t%0.1f\n", p->data.nNumb, p->data.sName, p->data.fMath);printf("请输入姓名和成绩:");scanf_s("%s", p->data.sName, sizeof(p->data.sName));scanf_s("%f", &p->data.fMath);Save();Print();
}int DelNumber(int nNumb)
{SNode* p = g_pHead, *p1 = NULL;if (!p)return 0;if (p->data.nNumb == nNumb){g_pHead = p->pNext;free(p);return 1;}while (p){if (p->data.nNumb == nNumb){p1->pNext = p->pNext;free(p);return 1;}p1 = p;p = p->pNext;}return 0;
}void Delete()
{char c;do{int nNumb;printf("请输入要删除的学号:");scanf_s("%d", &nNumb);if (DelNumber(nNumb)){Save();Print();}else{puts("你输入的学号不存在!");}printf("是否继续删除?[y/n]");rewind(stdin);c = getchar();} while (c == 'y' || c == 'Y');
}void Load()
{DATA data;FILE* pf = fopen("stud.lv", "r");if (!pf){puts("加载信息时失败!");return;}while (fread(&data, 1, sizeof(DATA), pf) == sizeof(DATA)){AddTail(data);}fclose(pf);
}void Save()
{SNode* p = g_pHead;FILE* pf = fopen("stud.lv", "w");if (!pf){puts("保存文件时失败!");return;}while (p){fwrite(p, 1, sizeof(p->data), pf);p = p->pNext;}fclose(pf);
}void Print()
{system("cls");puts("学号\t姓名\t成绩");int i = 0;SNode* p = g_pHead;while (p){printf("%d\t%s\t%0.1f\n", p->data.nNumb,p->data.sName,p->data.fMath);p = p->pNext;++i;}printf("\t总共有 %d 条记录\n", i);system("pause");
}void Input(int nDir)
{int nNumb;printf("请输入学号:");scanf_s("%d", &nNumb);DATA data;data.nNumb = nNumb;printf("请输入姓名:");scanf_s("%s", data.sName, sizeof(data.sName));printf("请输入成绩:");scanf_s("%f", &data.fMath);if (1 == nDir)AddHead(data);elseAddTail(data);Save();Print();
}int AddMenu()
{puts("1.向头部插入");puts("2.向尾部插入");puts("0.返回主菜单");int i = 0;scanf_s("%d", &i);switch (i){case 1:case 2:Input(i);break;}return i;
}int Judge(SNode* q, SNode* m, int nIndex)
{if (1 == nIndex)return q->data.nNumb < m->data.nNumb;if (2 == nIndex)return strcmp(q->data.sName, m->data.sName) < 0;if (3 == nIndex)return q->data.fMath > m->data.fMath;return 0;
}void PrintS(SNode* *ps)
{system("cls");puts("学号\t姓名\t成绩");int i = 0;while (ps[i]){SNode* p = ps[i]; printf("%d\t%s\t%0.1f\n", p->data.nNumb, p->data.sName, p->data.fMath);++i;}printf("\t总共有 %d 条记录\n", i);system("pause");}void Sort(int nIndex)
{SNode* p = g_pHead;if (!p)return;SNode* ps[100] = { 0 }; int n = 0, i = 0;while (p){ps[n] = p;p = p->pNext;++n;}while (i < n-1){int j = i + 1;int m = i;while (j < n){if(Judge(ps[j],ps[m],nIndex))m = j;++j;}if (m != i) {SNode* t = ps[i];ps[i] = ps[m];ps[m] = t;}++i;}PrintS(ps);
}int SortMenu()
{system("cls");puts("1.按学号排序");puts("2.按姓名排序");puts("3.按成绩排序");puts("4.不排序");puts("0.返回主菜单");int i = 0;scanf_s("%d", &i);switch (i){case 1:case 2:case 3:Sort(i);break;case 4:Print();default:return i;}return i;
}int Menu()
{int i = 0;system("cls");puts("1.浏览所有信息");puts("2.添加信息");puts("3.删除信息");puts("4.修改信息");puts("5.查找信息");puts("6.颜色设置");puts("0.退出");printf("请选择:");scanf_s("%d", &i);switch (i){case 0:break;case 1:while (SortMenu());break;case 2:while (AddMenu());break;case 3:Delete();break;case 4:Modify();break;}return i;
}int main()
{Load();while (Menu());return 0;
}