C语言:班级成绩管理系统
生活随笔
收集整理的這篇文章主要介紹了
C语言:班级成绩管理系统
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 前言:
- 程序要求
- 說明
- 代碼
- main.c
- def.c
- myIO.c
- file.c
- menu.c
- function.c
前言:
有朋友最近在做c語言課設(shè),要求寫一個班級成績管理系統(tǒng),便寫份簡單的代碼來玩。代碼原創(chuàng),未參考任何其他人的代碼
程序要求
說明
- 本程序主要采用結(jié)構(gòu)體數(shù)組
- 本文件采用多文件編寫,由于程序規(guī)模小,故未采用編寫頭文件的方式
- 使用 #pragma once 來防止頭文件重復(fù)包含
代碼
怎么使用本程序看看注釋應(yīng)該就知道了。run main.c 就行。其他各文件作用:
- def.c 定義了一些常量和全局變量,結(jié)構(gòu)體
- myIO.c 實現(xiàn)了成績錄入和成績打印輸出
- file.c 實現(xiàn)了將成績保存為文件
- menu.c 實現(xiàn)了菜單功能
- function.c 包含其他一些要用的函數(shù)
main.c
#include "menu.c"int main() {select();return 0; }def.c
// 相同頭文件只包含一次,后不贅述 #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define Status int// 課程 typedef struct Course {char name[30];int score; } Course, *pCourse;// 學(xué)生 typedef struct Student {char number[30];char name[30];pCourse pC; } Student, *pStudent;// n是學(xué)生數(shù), m是課程數(shù) int n, m; char courseName[30], studentName[30], course[20][30]; pStudent pS = NULL;myIO.c
#pragma once #include "def.c"pStudent inputStudentInfo(void); void printStudentInfo(pStudent pS);// 錄入學(xué)生信息 pStudent inputStudentInfo(void) {int i, j;printf("Please input the number of students and courses: ");scanf("%d %d", &n, &m);printf("Please input the name of courses: ");for (i = 0; i < m; i++){scanf("%s", course[i]);}pStudent pS = (pStudent)malloc(sizeof(Student) * n);if (!pS)return NULL;printf("Please input the info: \n");for (i = 0; i < n; i++){pS[i].pC = (pCourse)malloc(sizeof(Course) * m);if (!pS[i].pC)return NULL;scanf("%s %s", pS[i].name, pS[i].number);for (j = 0; j < m; j++){strcpy(pS[i].pC[j].name, course[j]);scanf("%d", &pS[i].pC[j].score);}}return pS; }// 打印所有學(xué)生信息 void printStudentInfo(pStudent pS) {int i, j;// 打印標(biāo)題printf("Name\tnumber\t");for (i = 0; i < m - 1; i++)printf("%s\t", course[i]);printf("%s\n", course[i]);// 顯示信息for (i = 0; i < n; i++){printf("%s\t%s\t", pS[i].name, pS[i].number);for (j = 0; j < m - 1; j++)printf("%d\t", pS[i].pC[j].score);printf("%d\n", pS[i].pC[j].score);} }file.c
#pragma once #include "def.c" Status saveStudentInfo(pStudent pS); Status saveStudentInfo(pStudent pS) {FILE *fp;int i, j;char filename[30], str[100] = "student number";printf("please input the filename: ");scanf("%s", filename);fp = fopen(filename, "w");if (!fp)return ERROR;for (i = 0; i < m; i++){strcat(str, " ");strcat(str, course[i]);}strcat(str, "\n");for (i = 0; i < n; i++){strcat(str, pS[i].name);strcat(str, " ");strcat(str, pS[i].number);for (j = 0; j < m; j++){char score[30];itoa(pS[i].pC[j].score, score, 10);strcat(str, " ");strcat(str, score);}strcat(str, "\n");}fputs(str, fp);fclose(fp);return OK; }menu.c
#pragma once #include "def.c" #include "myIO.c" #include "file.c" #include "function.c" void menu(); void select(); // 菜單 void menu() {printf("------------------------------------\n");printf("| Menu |\n");printf("| 1. input |\n");printf("| 2. show |\n");printf("| 3. save |\n");printf("| 4. sort |\n");printf("| 5. modify |\n");printf("| 6. count |\n");printf("| 0. exit |\n");printf("------------------------------------\n"); }void select() {int branch;while (TRUE){system("cls");menu();printf("[Input]: ");scanf("%d", &branch);if (!branch)break;switch (branch){case 1:{pS = inputStudentInfo();if (pS == NULL)printf("input error! please input again\n");elseprintf("Input success!\n");system("pause");break;}case 2:{printStudentInfo(pS);system("pause");break;}case 3:{if (OK == saveStudentInfo(pS))printf("Save success!\n");elseprintf("Save fail!\n");system("pause");break;}case 4:{sort(pS);printf("sort success\n");system("pause");break;}case 5:{int res = modify(pS);if (res){printf("change success!\n");}else{printf("change fail!\n");}system("pause");break;}case 6:{int choose;// 輸入1 顯示每門課程最高成績信息// 輸入2 顯示每門課程平均成績信息printf("choose 1 for the highest score: \n ");printf("choose 2 for the average score: \n");printf("[Input]: ");scanf("%d", &choose);if (choose == 1){showMax(pS);}else if (choose == 2){showAverage(pS);}else{// 輸入非法提示信息printf("Input error!\n");}system("pause");break;}}} }function.c
#include "def.c"void sort(pStudent pS); void Swap(pStudent s1, pStudent s2); void showAverage(pStudent pS); void showMax(pStudent pS); Status modify(pStudent pS);// 按課程成績排序 void sort(pStudent pS) {int courseNumber, i, j, k;char courseName[30];printf("please input the course name which you want to sort: ");scanf("%s", courseName);for (courseNumber = 0; courseNumber < m; courseNumber++)if (strcmp(course[courseNumber], courseName) == 0)break;// 如果找不到課程,則認(rèn)為是按總分排序if (courseNumber == m){printf("Sort as total score: \n");// 選擇排序for (i = 0; i < n - 1; i++){int flag = i;for (j = i + 1; j < n; j++){int totalScore_1 = 0, totalScore_2 = 0;for (k = 0; k < m; k++){totalScore_1 += pS[j].pC[k].score;totalScore_2 += pS[flag].pC[k].score;}if (totalScore_1 > totalScore_2){flag = j;}}Swap(&pS[i], &pS[flag]);}}else{// 選擇排序for (i = 0; i < n - 1; i++){int flag = i;for (j = i + 1; j < n; j++){if (pS[j].pC[courseNumber].score > pS[flag].pC[courseNumber].score){flag = j;}}Swap(&pS[i], &pS[flag]);}} }// 修改學(xué)生信息 Status modify(pStudent pS) {// 密碼是1314char password[30] = "1314", psd[30];char number[30];int score, i, j;printf("please input password: ");scanf("%s", psd);// 密碼正確才繼續(xù),否則返回ERRORif (strcmp(password, psd) == 0){printf("please input the student's number: ");scanf("%s", number);for (i = 0; i < n; i++){// 找到學(xué)生則繼續(xù),否則返回ERRORif (strcmp(pS[i].number, number) == 0){printf("please input the course and score one by one: \n");scanf("%s %d", courseName, &score);for (j = 0; j < m; j++){// 找到課程才繼續(xù),否則返回ERRORif (strcmp(pS[i].pC[j].name, courseName) == 0){// 修改課程成績pS[i].pC[j].score = score;return OK;}}return ERROR;}}return ERROR;}elsereturn ERROR; }// 輸出各課程最高分的學(xué)生 void showMax(pStudent pS) {int i, j, max;for (i = 0; i < m; i++){max = 0;for (j = 0; j < n; j++){if (pS[j].pC[i].score > pS[max].pC[i].score)max = j;}printf("%s\t%s\t%s\t%d\n", course[i], pS[max].name, pS[max].number, pS[max].pC[i].score);} }// 顯示各課程的平均成績 void showAverage(pStudent pS) {int i, j;double ave;for (i = 0; i < m; i++){ave = 0;for (j = 0; j < n; j++){ave += pS[j].pC[i].score;}printf("%s\t%.2lf\n", course[i], ave / n);} }void Swap(pStudent s1, pStudent s2) {int i;char studentName[30], number[30];// 交換姓名strcpy(studentName, s1->name);strcpy(s1->name, s2->name);strcpy(s2->name, studentName);// 交換學(xué)號strcpy(number, s1->number);strcpy(s1->number, s2->number);strcpy(s2->number, number);// 交換成績for (i = 0; i < m; i++){int temp = s1->pC[i].score;s1->pC[i].score = s2->pC[i].score;s2->pC[i].score = temp;} }總結(jié)
以上是生活随笔為你收集整理的C语言:班级成绩管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC项目添加日志
- 下一篇: 156 - ZOJ Monthly, J