C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序_提取排序规则
生活随笔
收集整理的這篇文章主要介紹了
C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序_提取排序规则
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h> #include <string.h> #include <stdlib.h> void Save();struct SUser {int nNumb;char sName[20];float fMath; }g_user[100];void Print() {puts("\n學(xué)號\t姓名\t數(shù)學(xué)");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條學(xué)生信息\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("請輸入學(xué)號:");scanf_s("%d", &nNumb);while (Check(nNumb)){printf("該學(xué)號已存在,請重新輸入(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("請輸入要刪除的學(xué)號:");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("你輸入的學(xué)號不存在!");else{Save();Print();} }void Modify() {printf("請輸入要修改的學(xué)號:");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("保存數(shù)據(jù)時失敗!");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); }int Judge(int nIndex,int j) {if (nIndex == 1)return g_user[j].nNumb > g_user[j + 1].nNumb;if (nIndex == 2)return strcmp(g_user[j].sName, g_user[j + 1].sName) > 0;if (nIndex == 3)return g_user[j].fMath < g_user[j + 1].fMath;return 0; }//int Judge(int nIndex,SUser* p) // p是指向第j個結(jié)構(gòu)體的地址void Sort(int nIndex) {int i = 0, n = 0;while (g_user[i++].nNumb)++n;i = 0;int flag = 0;while (i < n - 1){int j = 0;while (j < n - 1 - i){if(Judge(nIndex,j)){SUser t = g_user[j];g_user[j] = g_user[j + 1];g_user[j + 1] = t;}++j;}++i;} }int DispMenu() {puts("1.按學(xué)號排序");puts("2.按姓名排序");puts("3.按成績排序");puts("0.返回主菜單");printf("請選擇:");int i = 0;scanf_s("%d", &i);switch (i){case 1:case 2:case 3:Sort(i);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() {system("color 3f");Load();while (Menu());return 0; }運行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的C语言实用算法系列之学生管理系统_对整个结构体操作_冒泡排序_提取排序规则的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天猫商城电脑音箱(天猫音箱多少钱)
- 下一篇: 如何映射路由器内网IP路由器如何做内网映