C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序
生活随笔
收集整理的這篇文章主要介紹了
C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h> #include <string.h> void Save();struct SUser // 結構體定義 {int nNumb; // 學號char sName[20]; // 姓名float fMath; // 數學成績 }g_user[100];void Print() // 打印子函數 {puts("\n學號\t姓名\t數學");int i = 0;while (g_user[i].nNumb){if(g_user[i].nNumb>0)printf("%d\t%s\t%0.1f\n", g_user[i].nNumb, g_user[i].sName, g_user[i].fMath);++i;}printf("\n\t總共有%d條學生信息\n\n", i); }int Check(int nNumb) // 查找 {int i = 0;while (g_user[i].nNumb){if (nNumb == g_user[i].nNumb)return 1;++i;}return 0; }void Add() // 增加 {int nNumb;printf("請輸入學號:");scanf_s("%d", &nNumb);while (Check(nNumb)){printf("該學號已存在,請重新輸入(0取消輸入):");scanf_s("%d", &nNumb);if (nNumb == 0)return;}int i = 0;while (g_user[i].nNumb>0)++i;//scanf("%d%s%f",&g_user[i].nNumb,)g_user[i].nNumb = nNumb;printf("請輸入姓名:");scanf_s("%s", g_user[i].sName,sizeof(g_user[i].sName));printf("請輸入成績:");scanf_s("%f", &g_user[i].fMath);Save();Print(); }void Delete() // 刪除 {printf("請輸入要刪除的學號:");int nNumb;scanf_s("%d", &nNumb);int i = 0;//int flag = 0;while (g_user[i].nNumb){if (g_user[i].nNumb == nNumb){//flag = 1;g_user[i].nNumb = -1;break;}++i;}//if (flag == 0)if (g_user[i].nNumb!=-1)//if (g_user[i].nNumb==0)puts("你輸入的學號不存在!");else{Save();Print();} }void Modify() // 修改 {printf("請輸入要修改的學號:");int nNumb;scanf_s("%d", &nNumb);int i = 0;while (g_user[i].nNumb){if (g_user[i].nNumb == nNumb){printf("%d\t%s\t%0.1f\n", g_user[i].nNumb, g_user[i].sName, g_user[i].fMath);printf("請輸入新的姓名:");scanf_s("%s", g_user[i].sName, sizeof(g_user[i].sName));printf("請輸入新的成績:");scanf_s("%f", &g_user[i].fMath);return;}++i;}Save();Print(); }void Load() // 加載保存的數據文件 {FILE* pf = fopen("data.lv", "r");if (!pf)return;int i = 0;while (fread(&g_user[i], 1, sizeof(SUser), pf) == sizeof(SUser))++i;fclose(pf); }void Save() // 保存結構體數據到文件 {FILE* pf = fopen("data.lv", "w");if (!pf){puts("保存數據時失敗!");return;}int i = 0;while (g_user[i].nNumb){if (-1 != g_user[i].nNumb)fwrite(&g_user[i], 1, sizeof(SUser), pf);++i;}fclose(pf); }void SortByNumb() // 按學號排序 {int i = 0, n = 0;while (g_user[i++].nNumb > 0)++n;i = 0;while (i < n - 1){int j = 0;while (j < n - 1 - i){if (g_user[j].nNumb > g_user[j + 1].nNumb){SUser t = g_user[j];g_user[j] = g_user[j + 1];g_user[j + 1] = t;}++j;}++i;} }void SortByName() // 按姓名排序 {int i = 0, n = 0;while (g_user[i++].nNumb)++n;i = 0;while (i < n - 1){int j = 0;while (j < n - 1 - i){if (strcmp(g_user[j].sName , g_user[j + 1].sName)>0){SUser t = g_user[j];g_user[j] = g_user[j + 1];g_user[j + 1] = t;}++j;}++i;} }void SortByMath() // 按數學成績排序 {int i = 0, n = 0;while (g_user[i++].nNumb)++n;i = 0;while (i < n - 1){int j = 0;while (j < n - 1 - i){if (g_user[j].fMath < g_user[j + 1].fMath){SUser t = g_user[j];g_user[j] = g_user[j + 1];g_user[j + 1] = t;}++j;}++i;} }int DispMenu() {puts("1.按學號排序");puts("2.按姓名排序");puts("3.按成績排序");puts("0.返回主菜單");printf("請選擇:");int i = 0;scanf_s("%d", &i);switch (i){case 1:SortByNumb();Print();break;case 2:SortByName();Print();break;case 3:SortByMath();Print();break;}return i; }int Menu() {int i = 0;puts("1.瀏覽所有信息");puts("2.添加信息");puts("3.刪除信息");puts("4.修改信息");puts("5.查找信息");puts("0.退出");printf("請選擇:");scanf_s("%d", &i);switch (i){case 0:break;case 1:while (DispMenu());Print();break;case 2:Add();break;case 3:Delete();break;case 4:Modify();break;}return i; }int main() {Load();while (Menu());return 0; }運行結果
總結
以上是生活随笔為你收集整理的C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谁给个恶魔6点的账号啊
- 下一篇: C语言实用算法系列之学生管理系统_对整个