日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言編寫實現學生管理系統_C 語言

作者:百香果先森 ? 更新時間: 2022-09-15 編程語言

本文實例為大家分享了C語言實現學生管理系統的具體代碼,供大家參考,具體內容如下

項目介紹

學生信息管理系統是一個基于C語言開發的系統,其中有用到冒泡排序、指針、結構體、二位數組等知識。通過模塊化的方法編寫各個函數,其中在主界面函數調用各個模塊的函數的實現以下具體功能:

1、學生信息的增刪改查
2、學生成績的排序
3、統計學生人數
4、顯示所有學生的信息。
5、對學生信息存檔

總體設計

本實驗通過在main函數打開保存數據結果的文檔和調用主界面函數;在主界面函數welcom()中實現邊框的繪制,以及顯示各個功能及各個功能對應的函數實現方法。

錄入信息函數addInfo():此函數通過結構體student來保存錄入的信息,其中為了確保數據的后續操作的準確性,通過學號的唯一性來標識每個學生的信息,通過編寫及調用一個isIdSame()函數,該函數通過遍歷所有學號確認學號來保障學號的唯一性,學號重復會提示用戶需要重新輸入函數。

查找學生信息函數:通過學號查找學生的信息,編寫并調用一個findIndex()函數,該函數會遍歷結構體的學號信息,查詢到會返回該學號的坐標,沒有找到該學號則返回-1;通過變量target來保存返回的結果。如果target不等于-1,則程序找到了該學號,通過編寫并調用一個showInfo()函數來輸出所有該學生的信息;否則輸出查詢此人,因為下標不可能為負數。

更新學生信息函數update():通過學號來找到該學生,調用findIndex()函數來確定該學生的位置,如果返回結果是小于0則函數結束,查無此人;若大于0則找到該學生,通過do…while函數switch選擇語句的嵌套來進行用戶需要求改某一項的內容。

刪除函數del():查找學生的步驟跟更新學生信息函數的流程一樣,如果findIndex()函數小于0則函數結束,否則通過一個for循環把結構體的數組從光標開始往前覆蓋從而達到刪除效果。

插入學生信息函數inserInfo():通過要求用戶輸入位置來定位插入到位置,輸入用戶輸入的大于結構體數組的總數則插入到最后一個數組。否則通過一個for循環,把數組從最后開始往后移一位,把用戶輸入的位置的結果移到后一數組就編寫并調用插入函數inserCurrentInfo()對當前位置數組進行覆蓋插入。inserCurrentInfo()函數只負責對接收到的該位置的元素所有信息的寫入。

排序函數sortTotal():創建一個臨時變量提供元素與元素之間交換信息。通過雙循環嵌套結構進行結構體的分數進行大小對比、交換位置來進行冒泡排序。最后排序完成之后輸出分數由高到低排序的所有學生的信息。

顯示學生信息函數showAllInfo():該函數通過全局變量count(該變量記錄了所有添加、插入或刪除過的學生信息,能準確記錄學生的總人數)通過for循環去遍歷student結構體,從而輸出所有的所生信息。

學生數據存檔函數writeData():該函數定義一個指針,以寫入方式打開”stu.txt文本”,并把該文本的地址賦給指針fp。通過一個for循環遍歷結構體里的元素,把結構體里的元素的屬性輸入到”stu.txt文本”。

詳細代碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void addInfo();// 添加
void welcom(); //主界面?
void showAllInfo();//展示所有信息?
void showInfo(struct Student student);//展示學生信息?
int findIndex(struct Student student[],int id);// 根據學號 返回對應下標?
void del(); //刪除?
void search();// 查找學生信息?
void updata();//更新?
void sortTotal();//按總分排序?
void writeData();//數據寫入文件中?
void initData();//初始化數據 從文件中讀取數據,初始化數組
void showCount(); // 展示存儲學生個數?
void inserInfo();//插入學生信息?
void inserCurrentInfo(int site); //當前位置插入?
void con();//按任意鍵繼續?
int find1(struct Student student[],int id); //判斷學號是否有重復 重復返回1 不重復返回0?
void isIdSame(int x); //校驗所輸入學號是否重復?
void gotoxy(int x,int y);//光標定位
int color(int c); //設置顏色輸出?
struct Student{
?? ?int id;
?? ?char name[20];
?? ?int _math;
?? ?int _chinese;
?? ?int _english;
?? ?int total;// 總分?
} student[500];?
int count=0;// 記錄當前數組中存儲學生個數?
//主函數?
int main(){
?? ?initData();?
?? ?welcom();?
?? ?return 0;
}
// 光標定位?
void gotoxy(int x, int y)
{
?? ?COORD pos;
??? ?pos.X = x; ??? ?//橫坐標
??? ?pos.Y = y; ??? ?//縱坐標
??? ?SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//設置顏色輸出?
int color(int c)
{
?? ?SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); ? ? ? ?//更改文字顏色
?? ?return 0;
};?
// 主界面?
void welcom()
{
?? ?while(1){
?? ??? ?system("cls");
?? ??? ??? ?int n;
?? ??? ??? ?int i,j = 1;
?? ??? ??? ?color(10); ? ? ? ? ??? ??? ??? ??? ??? ??? ??? ??? ?//淡綠色邊框
?? ??? ??? ?for (i = 5; i <= 35; i++) ? ?? ??? ??? ??? ??? ??? ??? ?//循環y軸坐標,打印輸出上下邊框===
?? ??? ??? ?{
?? ??? ??? ??? ?for (j = 10; j <= 57; j++) ??? ??? ??? ??? ??? ?//循環x軸坐標,打印輸出左右邊框||
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?gotoxy(j, i);
?? ??? ??? ??? ??? ?if (i == 5 || i == 35) printf("=");?? ??? ??? ?//輸出上下邊框===
?? ??? ??? ??? ??? ?else if (j == 10 || j == 56) printf("||");?? ?//輸出左右邊框||
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ? ?? ?color(15);//白色?
?? ??? ? ?? ?gotoxy(25,8);
?? ??? ? ?? ?printf("學生信息管理系統");
?? ??? ??? ?color(14);?? ??? ??? ??? ?//設置字體顏色為黃色
?? ??? ??? ?gotoxy(15, 12);
?? ??? ??? ?printf("1:錄入學生信息");
?? ??? ??? ?gotoxy(35, 12);
?? ??? ??? ?printf("2.查找學生信息");
?? ??? ??? ?gotoxy(15, 16);
?? ??? ??? ?printf("3.刪除學生信息");
?? ??? ??? ?gotoxy(35,16);
?? ??? ??? ?printf("4.修改學生信息");
?? ??? ??? ?gotoxy(15, 20);
?? ??? ??? ?printf("5.插入學生信息");
?? ??? ??? ?gotoxy(35,20);
?? ??? ??? ?printf("6.按照學生成績排序");?? ??
?? ??? ??? ?gotoxy(15, 24);
?? ??? ??? ?printf("7.統計學生總數");
?? ??? ??? ?gotoxy(35,24);
?? ??? ??? ?printf("8.顯示所有學生信息");
?? ??? ??? ?gotoxy(15, 28);
?? ??? ??? ?printf("9.學生數據存檔并退出");
?? ??? ??? ?gotoxy(25,32);?
?? ??? ??? ?int choose;
?? ??? ??? ?printf("請選擇:[ ]\b\b"); //\b 光標回退一格 ?
?? ??? ??? ?color(15); // ?顏色變回白色?
?? ??? ? ?? ?scanf("%d", &choose);
?? ??? ? ?? ??? ?switch (choose){
?? ??? ? ?? ? ?? ??? ?case 1:addInfo(); break;
?? ??? ? ?? ? ?? ??? ?case 2:search(); break;
?? ??? ? ?? ? ?? ??? ?case 3:del(); break;
?? ??? ? ?? ? ?? ??? ?case 4:updata(); break;
?? ??? ? ?? ? ?? ??? ?case 5:inserInfo();break;?
?? ??? ? ?? ? ?? ??? ?case 6:sortTotal(); break;
?? ??? ? ?? ? ?? ??? ?case 7:showCount(); break;
?? ??? ? ?? ? ?? ??? ?case 8:showAllInfo(); break;
?? ??? ? ?? ? ?? ??? ?case 9:writeData();exit(0);
?? ??? ? ?? ? ?? ??? ?}?
?? ?}?? ??? ??? ??? ?
}
// 添加?
void addInfo(){
?? ?system("cls");
?? ?printf("\t添加學生信息\n");
?? ?printf("請輸入學號\n");
?? ?isIdSame(count);
?? ?printf("請輸入姓名\n");
?? ?scanf("%s",&student[count].name);
?? ?printf("請輸入語文成績\n");
?? ?scanf("%d",&student[count]._chinese);
?? ?printf("請輸入數學成績\n");
?? ?scanf("%d",&student[count]._math);
?? ?printf("請輸入英語成績\n");
?? ?scanf("%d",&student[count]._english);
?? ?student[count].total=student[count]._chinese+student[count]._english+student[count]._math;
?? ?printf("%s的信息錄入成功\n\n",student[count].name);
?? ?int choose;
?? ?printf("1繼續 2返回主界面\n");?
?? ?count++; ?
?? ?scanf("%d",&choose);
?? ?if(choose==1){
?? ??? ?addInfo();
?? ?}?
?? ?system("cls");?
?
}?
?
// 查找 展示結果?
void search(){
?? ?system("cls");
?? ?int id;
?? ?printf("請輸入你想查找學生的學號\n");
?? ?scanf("%d",&id);
?? ?int target = findIndex(student,id); ?//目標下表
?? ?int flag=1;//是否存在要查詢的學號?
?? ?
?? ?//for循環對比?
?? ?if(target != -1)?
?? ?{
?? ??? ?printf("\n\t查詢結果\n\n");
?? ??? ?showInfo(student[target]);
?? ??? ?con(); ?
?? ??? ?
?? ?}
?
?? ?else{ // 輸出查詢結果?
?? ??? ??? ?printf("\n查無此人\n");?
?? ??? ??? ??? ?con();
?? ?}?
}
?
?
// 更新?
void updata(){
?? ?system("cls");
?? ?int id;
?? ?printf("請輸入你要修改學生的學號\n");
?? ?scanf("%d",&id);
?? ?int?? ?target = findIndex(student,id);
?
?? ?if(target<0){
?? ??? ?printf("查無此人");
?? ??? ?con();?
?? ?}else{
?? ??? ?int flag=1;?? ?
?? ?do{
?? ??? ?int choose=0;
?? ??? ?printf("請輸入需要修改的選項\t(1.學號\t2.姓名\t3.語文\t4.數學\t5.英語):\n");
?? ??? ?scanf("%d",&choose);?
?? ??? ??? ?switch (choose) {
?? ??? ??? ??? ?case 1:
?? ??? ??? ??? ??? ?printf("請輸入學號\n");
//?? ??? ??? ??? ??? ?scanf("%d",&student[target].id);
?? ??? ??? ??? ??? ?isIdSame(target);?? ??? ??? ?
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?case 2:
?? ??? ??? ??? ??? ?printf("請輸入姓名\n");
?? ??? ??? ??? ??? ?scanf("%s",&student[target].name);
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?case 3:
?? ??? ??? ??? ??? ?printf("請輸入語文成績\n");
?? ??? ??? ??? ??? ?scanf("%d",&student[target]._chinese);
?? ??? ??? ??? ??? ?break;?
?? ??? ??? ??? ?case 4:
?? ??? ??? ??? ??? ?printf("請輸入數學成績\n");
?? ??? ??? ??? ??? ?scanf("%d",&student[target]._math);
?? ??? ??? ??? ??? ?break;?? ?
?? ??? ??? ??? ?case 5:
?? ??? ??? ??? ??? ?printf("請輸入英語成績\n");
?? ??? ??? ??? ??? ?scanf("%d",&student[target]._english);
?? ??? ??? ??? ??? ?break;?? ??? ?
?
?? ??? ??? ?}?
?? ??? ??? ?student[target].total=student[target]._chinese+student[target]._english+student[target]._math;
?? ??? ??? ?printf("%s的信息修改成功\n",student[target].name);
?? ??? ??? ?printf("\n按1繼續 按2退出修改\n");
?? ??? ??? ?int choose2;?
?? ??? ??? ?scanf("%d",&choose2);
?? ??? ??? ?if(choose2==1){
?? ??? ??? ??? ?flag=1;
?? ??? ??? ?}else{
?? ??? ??? ??? ?flag=0;
?? ??? ??? ?}?
?? ??? ??? ?
?? ?}while(flag);
?
?? ?}?? ?
}?
//刪除
void del(){
?? ?system("cls");?? ?
?? ?int id;
?? ?int target;//目標元素的下標?
?? ?printf("\n請輸入你想刪除學生的學號\n");
?? ?scanf("%d",&id);
?? ?target=findIndex(student, id);
?? ?if(target<0){
?? ??? ?printf("\n查無此人\n");
?? ??? ?con();
?? ??? ?
?? ?} else{
?? ??? ? for(int i=target;i<count;i++){
?? ??? ? ?? ?student[i]=student[i+1]; //刪除操作 后一位元素覆蓋前一位元素?
?? ??? ? }
?? ??? ?printf("刪除成功\n");
?? ??? ?con();
?? ?count--;?
?? ?}?
}
?
//插入學生信息
void inserInfo(){
?? ?system("cls");
?? ?int site; //位置?
?? ?printf("請輸入你要插入學生信息的位置(從0開始):\n");
?? ?scanf("%d",&site);
?? ?//插入位置大于總數,則插入在數組最后一位
?? ?if ( site > count){
?? ??? ?inserCurrentInfo(count);?
?? ??? ?printf("%s的信息插入成功\n", student[count].name);
?? ??? ?
?? ?}else{
?? ??? ?//不是最后一位 從當前位置 數組全部后移一位?
?? ??? ?for (int i = count; i >= site; i--){
?? ??? ??? ??? ?student[i + 1] = student[i];
?? ??? ??? ?}
?? ??? ?//在當前位置添加學員
?? ??? ?inserCurrentInfo(site);
?? ??? ?printf("%s同學的信息插入成功\n", student[site].name);
?? ??? ?con();?
?? ??? ?}
?
}?
//當前位置插入
void inserCurrentInfo(int site){
??? ?printf("請輸入學號\n");
??? ?isIdSame(site);
??? ?printf("請輸入姓名\n");
??? ?scanf("%s", student[site].name);
??? ?printf("請輸入語文成績\n");
??? ?scanf("%d", &student[site]._chinese);
??? ?printf("請輸入數學成績\n");
??? ?scanf("%d", &student[site]._math);
??? ?printf("請輸入英語成績\n");
??? ?scanf("%d", &student[site]._english);
?? ?student[site].total= student[site]._chinese+student[site]._english+student[site]._math;
??? ?count++;
??? ?con();
?}
// 判斷學號是否重復 重復返回1 否則返回0?
int find1(struct Student student[],int id)
{
?int temp = 0;
?for(int i=0;i<count;i++)
? {
? ?if(student[i].id==id)
? ? {
? ? ?temp=1;
? ? ?break;
? ? }
? }?
?return temp;
}?
//校驗所添加學號是否重復
void isIdSame(int x){
?
?? ?int inputId;?? ?
?? ?scanf("%d",&inputId);
?? ? do{
?? ? ? if(find1(student,inputId)){
?? ? ? ? printf("學號有重復,請重新輸入\n");
?? ? ? ? scanf("%d",&inputId);
?? ? ? ?}
?? ? ? else
?? ? ? ?{
?? ? ? ? student[x].id=inputId;
?? ? ? ? break;
?? ? ? ?}
?? ? ?}while(1);
}
?
// 根據學號 返回下標?
int findIndex (struct Student student[],int id){
?? ?int temp;
?? ?for(int i=0;i<count;i++){
?? ??? ?if(student[i].id==id){
?? ??? ??? ?temp=i;
?? ??? ??? ?break;?
?? ??? ?} else {
?? ??? ??? ?temp = -1;?
?? ??? ?}?
?? ?}
?? ?
?? ?return temp;
}
//按總分排序
?void sortTotal(){
??? ?//冒泡排序?
?? ?struct Student temp;// 元素與元素交換的臨時容器?
?? ?for (int i = 0; i < count - 1; i++){
?? ??? ?for (int j = 0; j < count - 1 - i; j++){
?? ??? ??? ?if (student[j].total<student[j+1].total){
?? ??? ??? ??? ?temp = student[j + 1];
?? ??? ??? ??? ?student[j + 1] = student[j];
?? ??? ??? ??? ?student[j]= temp;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?printf("排序完成");
?? ?showAllInfo();?? ??
?}
?//按任意鍵繼續?
?void con(){
??? ?printf("\n按任意鍵繼續\n");?
??? ?getch();
?};?
?//展示學生總個數?
?void showCount(){
??? ?system("cls");?
??? ?printf("\n\t學生總個數為:%d個\n",count);?
?? ?con();?
?}?
?//初始化數據?
?void initData(){
??? ?FILE * fp = NULL;
??? ?fp = fopen("stu.txt", "r");
??? ?if (!fp){
??? ??? ?printf("文件打開失敗\n");
??? ??? ?exit(0);// 退出程序?
??? ?}
??? ?while (1){ ? ?//讀取數據 賦值給數組?
??? ??? ?fscanf(fp, "%d%s%d%d%d%d", &student[count].id, student[count].name, &student[count]._chinese, &student[count]._math, &student[count]._english, &student[count].total);
??? ??? ?if (feof(fp)){ //文件末尾 跳出循環?
??? ??? ??? ?break;
??? ??? ?}
??? ??? ?count++;
??? ?}
?}
?//數據寫入文件中?
?void writeData(){
?? ?FILE * fp = NULL;
?? ?fp = fopen("stu.txt", "w");
?? ?for (int i = 0; i < count; i++){
?? ??? ?fprintf(fp, "%d\t%s\t%d\t%d\t%d\t%d\n", student[i].id, student[i].name, student[i]._chinese, student[i]._math, student[i]._english,student[i].total);
?? ?}
?? ?printf("數據保存成功\n"); ?? ?
?}
?
?
// 展示所有信息
void showAllInfo(){
?? ?system("cls");//清屏?
?? ?for(int i=0;i<count;i++){
?? ??? ?showInfo(student[i]);?? ??
?? ?}?
?? ?con();?
}?
// 展示學生信息
void showInfo(struct Student stu){//傳入數組里的元素?
?? ?printf("學號:%d\t姓名:%s\t語文:%d\t數學:%d\t英語:%d\t總分:%d",stu.id,stu.name,stu._chinese,stu._math,stu._english,stu.total);
?? ?printf("\n-----------------分割線-----------------------\n");?
}?

主界面

原文鏈接:https://blog.csdn.net/weixin_46308934/article/details/122255352

欄目分類
最近更新