本文實例為大家分享了C語言實現學生成績管理系統的具體代碼,供大家參考,具體內容如下
C語言小項目
實現一個學生成績管理系統
系統功能:
1.實現所有學生成績的錄入(利用結構體數組),當輸入字符為end時候,結束成績的錄入;
2.實現所有學生信息的輸出
3.輸入指定學生姓名,并能輸出這名學生的信息
4.將學生成績按照語文和數學成績排序
思路:
1. 首先,先把這個小項目的框架打好??紤]要寫幾個文件,每一個文件里面實現怎樣的功能??紤]到這個小項目的代碼量以及程序的易讀性,我決定將寫三個文件。一個main.c,里面就寫需要用到的函數;一個teacher.c主要實現main.c函數里面具體的內容;一個teacher.h里面主要寫這個項目我們需要用到一些宏定義和函數,以便在另外兩個C文件里面調用。(注意:在兩個C文件里面要包括teacher.h文件)。
2. 其次,分析一下這個系統要實現的功能,有四個功能。我們可以每一個功能寫一個函數,分別實現全部錄入,全部輸出,指定學生信息輸出以及語文和數學成績的排序這四個函數。將這四個函數的具體實現放在teacher.c文件里面。Main.c就負責直接調用這些函數。
3. 最后,考慮到需要錄入的時學生信息,包括姓名,學號,性別,語文成績,數學成績這些內容。所以,考慮用結構體數組來實現。
Teacher.h文件
#ifndef _TEACHER_H_ #define _TEACHER_H_ struct student { char name[20]; int id; char sex; int chinese; int math; }; typedef struct student stu; void show_message(stu *st, int len); int input(stu *st); void find(stu *st, int len); void sort(stu *st, int len); void out(stu *st, int i); void welcome(); void showchoice(); #endif
Main.c文件
#include <stdio.h> #include <string.h> #include "teacher.h" #include <stdlib.h> int main() { int len; int m; stu st[100]; welcome(); while(1) { showchoice(); scanf("%d", &m); switch(m) { case 1: len = input(st);break; case 2: show_message(st , len);break; case 3: find(st , len);break; case 4: sort(st , len);break; default : exit(0); } } return 0; }
Teacher.c文件
#include <string.h> #include <stdio.h> #include "teacher.h" void welcome() //系統界面歡迎函數 { system("clear"); printf("*********************************/n"); printf("WELCOME TO TEACHER SYSTEM!/n"); printf("*********************************/n"); sleep(2); } void showchoice() //選擇提示函數 { //system("clear"); printf("*********************************/n"); printf("1 input! 2 showinfor!/n"); printf("3 find! 4 sort!/n"); printf("*********************************/n"); printf("Please input your choice :/n"); } void out(stu *st, int i) //輸出第i個學生的信息 { printf("%s ",st[i].name); printf("%d ",st[i].id); printf("%c ",st[i].sex); printf("%d ",st[i].chinese); printf("%d",st[i].math); printf("/n"); } int input(stu *st) //錄入學生信息 { int i; printf("Input name, id, sex, Chinese score, Math score:/n"); for(i = 0; i < 100; i++) { scanf("%s", st[i].name); if((!strcmp(st[i].name , "end"))) { return i; } scanf("%d", &st[i].id); scanf("%s", &st[i].sex); scanf("%d", &st[i].chinese); scanf("%d", &st[i].math); } return i; } void show_message(stu *st, int len) //輸出全部學生信息 { int i; printf("name, id, sex, Chinese score, Math score:/n"); for(i = 0; i < len; i++) { out(st, i); } } void find(stu *st,int len) //查找出特定學生信息 { char tmp[20]; int i; printf("Please input the target student:/n"); scanf("%s", tmp); for(i = 0; i < len; i++ ) { if(!strcmp(st[i].name,tmp)) { out(st, i); } } } void sort(stu *st, int len) //將數學,語文成績冒泡排序 { int tmp; int i,j,k; int id,sex,chinese,math; char name[20]; int choice; printf("/n"); printf("Please input your sort choice:/n"); //選擇提示:1 數學成績排序 2 語文成績排序 printf("1 math grade! 2 chinese grade!/n"); printf("/n"); scanf("%d",&choice); if(1 == choice) { for(i = 0;i < len-1;i++) { for(j = 0;j < len-1-i;j++) { if(st[j].math > st[j+1].math) //將成績較大的學生信息放到成績較低的后面 { tmp = st[j].math; st[j].math = st[j+1].math; st[j+1].math = tmp; strcpy(name,st[j].name); strcpy(st[j].name,st[j+1].name); strcpy(st[j+1].name,name); id = st[j].id; st[j].id = st[j+1].id; st[j+1].id = id; sex = st[j].sex; st[j].sex = st[j+1].sex; st[j+1].sex = sex; chinese = st[j].chinese; st[j].chinese = st[j+1].chinese; st[j+1].chinese = chinese; } } } printf("After sort math grade :/n"); for(k = 0;k < len;k++) { out(st, k); } } else if(2 == choice) { for(i = 0;i < len-1;i++) { for(j = 0;j < len-1-i;j++) { if(st[j].chinese > st[j+1].chinese) { tmp = st[j].chinese; st[j].chinese = st[j+1].chinese; st[j+1].chinese = tmp; math = st[j].math; st[j].math = st[j+1].math; st[j+1].math = math; strcpy(name,st[j].name); strcpy(st[j].name,st[j+1].name); strcpy(st[j+1].name,name); id = st[j].id; st[j].id = st[j+1].id; st[j+1].id = id; sex = st[j].sex; st[j].sex = st[j+1].sex; st[j+1].sex = sex; } } } printf("After sort chinese grade :/n"); for(k = 0;k < len;k++) { out(st,k); } } else { printf("Input error!/nPlease input again!/n"); } }
小項目程序我自己測試過,如果大家發現有不對的地方請多多指出。大家一起學習,一起進步!
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選