学生成绩管理系统——C++实现

主要实现以下功能:
1、添加学生;
2、查找学生;
3、删除学生;
4、修改学生;
5、重新排序;
6、显示全部;
7、退出。

C++代码:

#include<iostream>
#include<string>
#include<iomanip>
#include<stdlib.h>//用到system()
#include<algorithm>
using namespace std;

const short MAX_SIZE = 50;

class Student {
	//学生类,完成个人信息的输出和返回
	/*
	SetMath();//输入数学成绩
	SetEnglish();//输入英语成绩
	SetComputer();//输入电脑成绩
	SetName();//输入姓名
	GetSum();//总分
	Match();//输入字符串信息与名字或学号是否匹配
	Print();//显示该学生信息
	*/
public:
	Student() {};//分号加与不加均可通过编译
	Student(char *n) {
		//待实参传入时,形参就会指向实参的地址,不再是野指针
		strcpy_s(name, n);
	}
	Student(char *n, char *i) {
		strcpy_s(name, n);
		strcpy_s(id, i);
	}
	void SetMath(float m) {
		math = m;
	}
	void SetEnglish(float e) {
		english = e;
	}
	void SetComputer(float c) {
		computer = c;
	}
	void SetName(char *n) {
		strcpy_s(name, n);
	}
	string GetName() {
		return name;
	}
	string GetId() {
		return id;
	}
	float GetSum() {
		return math + english + computer;
	}
	void SetAllScore(float m, float e, float c) {
		math = m;
		english = e;
		computer = c;
	}
	bool Match(char *str, short flag) {
		//flag为1,与姓名匹配;flag为2,与学号匹配
		return flag == 1 ? strcmp(name, str) == 0 : strcmp(id, str) == 0;
	}
	float GetMath() {
		return math;
	}
	float GetEnglish() {
		return english;
	}
	float GetComputer() {
		return computer;
	}
	void Print() {
		cout << name << '\t' << id << endl;
		cout << "MathScore:" << setprecision(1) << setiosflags(ios::fixed) << math << '\t' << "EnglishScore:" << english << '\t' << "ComputerScore:" << computer << endl;
	}
private:
	char name[12];
	char id[10];
	float math;
	float english;
	float computer;
};

bool Compare1(Student stu1, Student stu2) {
	return stu1.GetId() < stu2.GetId();
}

bool Compare2(Student stu1, Student stu2) {
	return stu1.GetSum() > stu2.GetSum();
}

class DataBase {
	//数据采集类,将学生类的数组作为一个成员,封装对数组添加、插入、查找、删除元素等操作
	/*
	Push();//添加
	Search();//查找
	Alter();//修改
	Delete();//删除
	Display();//显示所有学生信息
	ShowMenu();//显示功能菜单
	*/
public:
	DataBase() {
		size = 0;
	}
	bool Push(char *n,char *i,float m,float e,float c) {
		if (size == MAX_SIZE) {
			return false;
		}
		Student st(n, i);
		st.SetAllScore(m, e, c);
		stu[size++] = st;
		return true;
	}
	bool Push() {
		if (size == MAX_SIZE) {
			cout << "系统不能容纳更多学生。" << endl;
			system("pause");
			return false;
		}
		char n[12], i[10];
		cout << "Please input name:";
		cin >> n;
		int idx;
		do {
			cout << "Please input id:";
			cin >> i;
			for (idx = 0; idx < size; idx++) {
				if (stu[idx].Match(i, 2)) {
					cout << "该学号已存在,不能重复输入。" << endl;
					break;
				}
			}
		} while (idx < size);
		Student stu_tmp(n, i);
		float m, e, c;
		cout << "Please input math score:";
		cin >> m;
		cout << "Please input english score:";
		cin >> e;
		cout << "Please input computer score:";
		cin >> c;
		stu_tmp.SetAllScore(m, e, c);
		stu[size] = stu_tmp;
		size++;
		cout << "添加成功!" << endl;
		system("pause");
		return true;
	}
	short AimedSearch(short start_id, char *str, short flag) {
		//start_id为起始下标
		for (short i = start_id; i < size; i++) {
			if (stu[i].Match(str, flag)) {
				stu[i].Print();
				return i;
			}
		}
		return -1;//未找到
	}
	short Search() {
		//实现查找,提供输入接口
		short choice;
		do {
			cout << "请问按什么条件搜索?1、姓名  2、学号" << endl;
			cin >> choice;
		} while (choice != 1 && choice != 2);
		char match[12];
		cout << "请输入你要找的" << (choice == 1 ? "姓名:" : "学号:");
		cin >> match;
		short result = 0;
		char nod;
		while (true) {
			//若数组中有重复姓名,且先出现的不是自己想找的那个学生
			result = AimedSearch(result, match, choice);
			if (choice == -1) {
				cout << "未找到匹配信息。" << endl;
				system("pause");
				return result;
			}
			cout << "这是你要找的人吗?(y/n)";
			cin >> nod;
			if (nod == 'y' || nod == 'Y')
				return result;
			else
				result++;
		}
	}
	bool Delete() {
		short result = 0;
		result = Search();
		char nod;
		short idx;
		if (result == -1)
			return false;
		cout << "是否要进行删除?(y/n)" << endl;
		cin >> nod;
		if (nod == 'y' || nod == 'Y') {
			for (idx = result; idx < size; idx++) {
				stu[idx] = stu[idx + 1];
			}
			size--;
			cout << "删除成功!" << endl;
			system("pause");
			return true;
		}
		else {
			cout << "删除失败!" << endl;
			system("pause");
			return false;
		}
	}
	bool Alter() {
		short result = 0;
		result = Search();
		char nod;//选择是否修改
		char choice;//选择要修改什么
		char newname[12];
		float newscore;
		if (result == -1) {
			cout << "未找到想要修改的元素。" << endl;
			system("pause");
			return false;
		}
		cout << "是否要进行修改?(y/n)" << endl;
		cin >> nod;
		if (nod == 'Y' || nod == 'y') {
			cout << "修改什么?1、Name 2、MathScore 3、EnglishScore 4、ComputerScore" << endl;
			cin >> choice;
			switch (choice) {
			case '1':
				cout << "Please input a new name:";
				cin >> newname;
				stu[result].SetName(newname);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			case '2':
				cout << "Please input a new mathscore:";
				cin >> newscore;
				stu[result].SetMath(newscore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			case '3':
				cout << "Please input a new englishscore:";
				cin >> newscore;
				stu[result].SetEnglish(newscore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			case '4':
				cout << "Please input a new computerscore:";
				cin >> newscore;
				stu[result].SetComputer(newscore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			default:
				break;
			}
			return true;
		}
		cout << "未修改!" << endl;
		system("pause");
		return false;
	}
	void Display() {
		cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl;
		cout << endl;
		cout << setprecision(1) << setiosflags(ios::fixed);
		for (int i = 0; i < size; i++) {
			cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetId() << setw(8) << stu[i].GetMath() << setw(8) << stu[i].GetEnglish() << setw(8) << stu[i].GetComputer() << endl;
		}
		cout << resetiosflags(ios::left);
		system("pause");
	}
	void Sort() {
		char choice;
		do {
			cout << "请选择排序方式:1、学号升序 2、总成绩降序";
			cin >> choice;
		} while (choice != '1'&&choice != '2');
		if (choice == '1') {
			sort(&stu[0], &stu[0] + size, Compare1);
		}
		else {
			sort(&stu[0], &stu[0] + size, Compare2);
		}
		cout << "排序完成!" << endl;
		system("pause");
	}
	char ShowMenu() {
		char choice;
		do {
			system("cls");
			cout << " --------欢迎使用学生成绩管理系统-------- " << endl;
			cout << endl;
			cout << "   1、添加学生" << endl;
			cout << "   2、查找学生" << endl;
			cout << "   3、删除学生" << endl;
			cout << "   4、修改学生" << endl;
			cout << "   5、重新排序" << endl;
			cout << "   6、显示全部" << endl;
			cout << "   7、退出" << endl;
			cin >> choice;

		} while (choice < '1' && choice>'7');
		return choice;
	}
private:
	Student stu[MAX_SIZE];
	short size;
};

int main() {
	DataBase db;
	bool quit = false;
	char choice;
	db.Push("张三", "00006", 70, 72, 76);
	db.Push("李四", "00003", 62, 60, 70);
	db.Push("王五", "00001", 61.5, 74, 68.5);
	while (!quit) {
		choice = db.ShowMenu();
		switch (choice) {
		case '1':
			db.Push();
			break;
		case '2':
			db.Search();
			break;
		case '3':
			db.Delete();
			break;
		case '4':
			db.Alter();
			break;
		case '5':
			db.Sort();
			break;
		case '6':
			db.Display();
			break;
		case '7':
			quit = true;
			break;
		}
	}
	return 0;
}

C++运行结果:
在这里插入图片描述

关于c语言和c++的课程成绩信息管理系统,共有将近6000行代码,建议使用vs2012或2010便于管理也可使用VC6.0++环境修改运行但查找麻烦,所有的语言没有脱离c和c++,主要采用模块思想,也可以转换成面向对象型的语言,只要将模块函数写进类中。同时学c语言的也可以使用,除了使用cout,cin一些很容易上手的c++代码,相当于printf,scanf,主要为了方便输入输出,不用写%d%c... 详细细节也可以访问,百度文库网址 使用注意事项 有着强大的报错功能。 1 全部采用鼠标点击功能,可以看百度网址图片。 2 录用学生信息的细节选项中,如果点击错误信息,再次点击将会取消。 3 附加功能中的高级打印功能中,如果想改变选项,只需要点击另一个即可,当前的状态就会消失。 4 输入学号为53120101--531215**(其中不包括****00,例如53120700)。(可以设置) 5 所有成绩范围为0--99。(可以设置) 6 如果想去掉钢琴曲,直接删除MP3,或者改成其他名字即可。 7 打印直方图可以根据班级的不同,向后移动。 7 如果打印不规范,可能窗口较小,可通过调节窗口大小来打印排名...... 8 请包含student.txt默认文件(文件中至少一名学生信息),否则将会程序在进行实质功能作用时意外退出(已在包中)。 头文件student.h #ifndef _STUDENT_H_ #define _STUDENT_H_ #include #include HWND hWnd; //来自msdn #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \ on line %d\n", __FILE__, GetLastError(), api, __LINE__);} void cls( HANDLE hConsole ) { COORD coordScreen = { 0, 0 }; /* here's where we'll home thecursor */ BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ DWORD dwConSize; /* number of character cells inthe current buffer *//* get the number of character cells in the current buffer */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "GetConsoleScreenBufferInfo" ); dwConSize = csbi.dwSize.X * csbi.dwSize.Y;/* fill the entire screen with blanks */ bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputCharacter" );/* get the current text attribute */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "ConsoleScreenBufferInfo" );/* now set the buffer's attributes accordingly */ bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputAttribute" );/* put the cursor at (0, 0) */ bSuccess = SetConsoleCursorPosition( hConsole, coordScreen ); PERR( bSuccess, "SetConsoleCursorPosition" ); return; } HANDLE hOut; HANDLE hIn; void enter(); void ReadFile(char*str="student.txt"); typedef enum grade{you=95,liang=85,zhong=75,pass=65,nopass=0 } Grade; Grade g1=you; Grade g2=liang; Grade g3=zhong; Grade g4=pass; Grade g5=nopass; void DelClass(); //学生类结构 typedef struct student{ int studentid; char name[20]; char sex[5]; char nation[20]; int biryear; int birmonth; char post[10]; int cyu; int cyushe; int cshe; int cdui; int cduishe; struct student* next; double ave; double wave; } Student; Student *stubegin=NULL; Student* stulast=NULL; int total=0; //课程类结构 typedef struct course{ char obj[30]; int time; int xuefen; int mark; Grade rank; } Course; Course c1; Course c2; Course c3; Course c4; Course c5; void InitCourse(); void AddData(Student*); void AltData(); void AddShuju(int *,Student*); void IntEro(int& ,int,int,int,int); //功能介绍 /*****************************************Loading页面*******************************************/ void input();//输入信息功能 void output();//输出信息功能 void addition();//附加功能 /*****************************************输入信息功能*******************************************/ void AddStudent(); void DelStudent(int); void AltStudent(int); void SaveMessage(); void readfile(); /*****************************************输出信息功能*******************************************/ void CalRankAve(int); void CalRankWave(int); void PrintStudent(int); void PrintCourse(int); void PrintWell(); /*****************************************打印总成绩*******************************************/ void PrintTotal(); void PrintStudentID(); void PrintAve(); void PrintWave(); /*****************************************打印科目成绩*******************************************/ void PrintObj(); void PrintCyu(); void PrintCyushe(); void PrintCshe(); void PrintCdui(); void PrintCuishe(); /*****************************************打印优秀职务单科成绩*******************************************/ void PrintObjYou(); void PrintCyuYou(); void PrintCyusheYou(); void PrintCsheYou(); void PrintCduiYou(); void PrintCuisheYou(); /*********************辅助函数**********************/ void printmark(Student *); void AddStudent1(); void SortAve(); void SortWave(); void SortId(); void SortCyu(); void SortCyushe(); void SortCshe(); void SortCdui(); void SortCduishe(); int GetInt(char* ); int Search(int); void PrintGrade(int); int GetXuefen(int,int); /****计算平均成绩和加权成绩**/ void CalAve(Student*); void CalWave(Student*); /*****************************************附加功能*******************************************/ void PrintHistogram(); void PrintTotalHistogram();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值