学生管理系统-C++版

今天下午有个面试,但是不想复习,就将写好的这个简单学生关系系统发布出来。
另:祝愿自己面试顺利,加油!

============================================================
本篇涉及到的知识点:
1、单链表的遍历、插入、删除
2、switch分支处理键盘输入
3、类外实现、多文件编程
4、ofstream操作文件、写文件

  • ofstream打开文件
    • 使用对象打开文件 outfile.open(“student.txt”, ios::out || ios::app);
    • 默认方式打开文件 ofstream out(“student.txt”);
    • 参数如下
      学生管理系统-C++版_第1张图片
  • 关闭文件 outfile.close();

========================================================
说明:
1、Student类中有学号、姓名、分数三个公有成员,私有成员next用于组织链表
2、系统的功能:

  • 录入学生信息(学生信息以链表形式存储)
  • 修改学生信息
  • 删除学生信息(链表删除节点)
  • 查看学生信息
  • 将全部的学生信息写入student.txt文件(遍历链表,写入文件)
  • 根据学生成绩排名(对链表节点排序,已经更新实现)
    • 单链表的排序一般针对带头节点的而言(交换节点、交换数据域)
    • 本篇采用了一个交换节点的方法实现
    • 单链表排序、逆置以后可能会在某一个小项目中具体再讨论

3|、代码中可能会缺少注释,有时间再补上。

===========================================================
student.h

#ifndef _STUDENT_H_
#define _STUDENT_H_

class Student{
public:
	/*******构造***********/
	Student(char* num, char* name, float score);
	Student();
	
	char stu_num[15];
	char stu_name[30];
	float stu_score;
	
	void afterInsert(Student* p);	//后插
	void afterDelete(); //后删
	Student* getNext();

	/*******查询学生信息**********/
	void getMesg();
	/*******修改学生信息**********/
	void changeMesg(int n,char* ptr);
	void changeGrade(float  f);

private:
	Student* next;
};
#endif

student.cpp(负责类外实现)

#define  _CRT_SECURE_NO_WARNINGS
#include "student.h"
#include 
#include 
using namespace std;

Student::Student(){
	strcpy(this->stu_num, "");
	strcpy(this->stu_name, "");
	this->stu_score = 0;
	this->next = NULL;
}
Student::Student(char* num, char* name, float score){
	strcpy(this->stu_num, num);
	strcpy(this->stu_name, name);
	this->stu_score = score;
	this->next = NULL;
}

void Student::afterInsert(Student* p){	//后插
	p->next = this->next;
	this->next = p;
}
void Student::afterDelete(){ //后删
	Student* pTmp = this->next;
	this->next = pTmp->next;
	delete pTmp;

}
Student* Student::getNext(){
	return this->next;
}

void Student::getMesg(){
	cout << "学号:" << stu_num << "  姓名" << stu_name << "  成绩" << stu_score<stu_score = f;
}



主函数main(主菜单、构建链表、查询学生、修改信息、删除学生)

#define  _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include "student.h"


using namespace std;


//建立链表函数(创建学生)
void creatList(Student* stu){
	Student* p = new Student;
	char str[30];//学号或者姓名
	float score;//分数
	cout << "输入学号:";
	cin >> str;
	p->changeMesg(1, str);
	memset(str, 0, sizeof(str));//清空str


	cout << "输入姓名:";
	cin >> str;
	p->changeMesg(2, str);
	memset(str, 0, sizeof(str));//清空str

	cout << "输入得分:";
	cin >> score;
	p->changeGrade(score);

	system("cls");
	cout << "信息输入完成!" << endl;

	while (stu->getNext())		//寻找尾节点
		stu = stu->getNext();

	stu->afterInsert(p);

	//TODO...文件处理
}

//学生信息查询
Student* findMesg(Student* stu){
loop:
	cout << "1、按姓名查询\t2、按学号查询\tq、返回上一级菜单" << endl;
	char p[5], ptr[30];
	Student* pCur = stu;
	cin >> p;
	if (p[0] != '1' && p[0] != '2' && p[0] != 'q' || strlen(p) > 1){
		cout << "您的输入有误,重新输入" << endl;
		goto loop;
	}
	switch (p[0]){
	case '1':
		cout << "输入要查找的姓名:";
		cin >> ptr;
		for (; strcmp(ptr, pCur->stu_name) != 0;){
			if (pCur->getNext() == NULL){
				cout << "对不起,您要查找的人不存在!" << endl;
				goto loop;
			}
			pCur = pCur->getNext();
		}

		return pCur;
	case '2':
		cout << "输入要查找的学号:";
		cin >> ptr;
		for (; strcmp(ptr, pCur->stu_num) != 0;){
			if (pCur->getNext() == NULL){
				cout << "对不起,您要查找的学号不存在!" << endl;
				goto loop;
			}
			pCur = pCur->getNext();
		}

		return pCur;
	case 'q':
		system("cls");
		cout << "您的输入有误,请重新输入!" << endl;
		goto loop;
	}
}
//删除链表节点
void delList(Student* stu){
	Student* p = NULL;
	char selet[4];
	//system("cls");
	cout << "删除前,会根据您的提示找到您要删除的学生信息" << endl;
	p = findMesg(stu);
	if (p != NULL){
		cout << "确认删除吗?(yes/任意键返回)" << endl;
		cin >> selet;
		if (strcmp(selet, "yes") == 0){
			for (; stu->getNext() != p;){
				stu = stu->getNext();
			}
			stu->afterDelete();
			cout << "该学生信息删除成功!" << endl;
		}

	}
}

//修改节点信息
void changeInfo(Student* stu){
	system("cls");
	cout << "修改前,会根据您提供的信息查找到您要修改的信息" << endl;
	Student* p = NULL;

	float score;
	p = findMesg(stu);
	if (p != NULL){
		cout << "请输入成绩" << endl;
		cin >> score;
		p->changeGrade(score);
		cout << "修改成功" << endl;
	}
}
void putStu(Student* stu){
	system("cls");
	cout << "1、查看指定学生信息\t2、查看所有学生信息\t3、分段输出学生信息" << endl;
	char ch;
	int n = 0;

	stu = stu->getNext();
	cin >> ch;
	switch (ch){
	case '1':
		stu = findMesg(stu);
		if (stu == NULL){
			break;
		}
		stu->getMesg();
		break;
	case'2':
		while (stu){
			stu->getMesg();
			stu = stu->getNext();
		}
		break;
	case'3':
		cout << "a-60分以下;b-60~70分之间;c-70~80分之间;d-80~90分之间;e-90~100分之间:" << endl;
		cin >> ch;
		switch (ch){
		case'a':
			while (stu){
				if (stu->stu_score <= 60){
					stu->getMesg();
					++n;
				}
				stu = stu->getNext();
			}
			break;
		case'b':
			while (stu){
				if (stu->stu_score > 60 && stu->stu_score <= 70){
					stu->getMesg();
					++n;
				}
				stu = stu->getNext();
			}
			break;
		case'c':
			while (stu){
				if (stu->stu_score > 70 && stu->stu_score <= 80){
					stu->getMesg();
					++n;
				}
				stu = stu->getNext();
			}
			break;
		case'd':
			while (stu){
				if (stu->stu_score > 80 && stu->stu_score <= 90){
					stu->getMesg();
					++n;
				}
				stu = stu->getNext();
			}
			break;
		case'e':
			while (stu){
				if (stu->stu_score > 90 && stu->stu_score <= 100){
					stu->getMesg();
					++n;
				}
				stu = stu->getNext();
			}
			break;
		}
		if (n == 0){
			cout << "没有改分数段的学生" << endl;
		}
	}
}

//对学生成绩排序(2018.11.15更新)
void insertSort(Student* stu){
	Student* list = NULL;
	if (stu == NULL)
		return;
	else{
		list = (Student*)malloc(sizeof(Student));
		strcpy(list->stu_name, "");
		strcpy(list->stu_num, "");
		list->stu_score = 0;
		list->setNext(stu);
	}
	//保证有2个数据才排序
	if (list->getNext() == NULL || (list->getNext()->getNext() == NULL))
		return;
	Student *head, *p1, *p2, *prep1, *prep2, *temp;
	head = list;
	prep1 = head->getNext();
	p1 = prep1->getNext();

	bool flag;
	while (p1 != NULL){
		flag = true;
		temp = p1;
		for (prep2 = head, p2 = prep2->getNext(); p2 != p1; prep2 = prep2->getNext(), p2 = p2->getNext()){
			//从小打到排序
			if (p2->stu_score > p1->stu_score){
				//将p1节点拿出来
				p1 = p1->getNext();
				prep1->setNext(p1) ;
				//p1节点插入到p2之前
				prep2->setNext(temp);
				temp->setNext(p2);
				flag = false;//一个数据插入完成
				break;
			}
		}
		if (flag){			//处理后续数据
			prep1 = prep1->getNext();
			p1 = p1->getNext();
		}
	}
}




//主菜单
void menu(Student* stu){
	char selet[10];
	int n = 1;
	ofstream outfile;
	//ifstream infile;
	Student *p, *ptr,*mid;

	cout << "*************************欢迎进入学生信息管理系统*************************" << endl;
	do {
		cout << "**************************************************************************" << endl;
		cout << "1.插入信息;   2.删除信息;  3.修改信息; 4.查看信息; 5.保存  " << endl;
		cout << "按'q'键退出      " << endl;
		cout << "**************************************************************************" << endl;

		cin >> selet;
		if (((selet[0]<'1' || selet[0]>'6') && selet[0] != 'q') || strlen(selet) > 1){
			//system("cls");
			cout << "您的输入有误,请重新输入!" << endl;
			break;

		}
		switch (selet[0]){
		case'1':
			creatList(stu);
			break;
		case'2':
			delList(stu);
			break;
		case'3':
			changeInfo(stu);
			break;
		case'4':
			putStu(stu);
			break;
		case'5':
			outfile.open("student.txt", ios::out || ios::app);
			for (p = stu->getNext(); p != NULL; p = p->getNext()){
				outfile << p->stu_num << " ";
				outfile << p->stu_name << " ";
				outfile << p->stu_score << " ";
				outfile << endl;
			}
			outfile.close();
			system("cls");
			cout << "保存成功!" << endl;
			break;
		case'q':
			break;
		}

	} while (selet[0] != 'q');
}
int main(){
	Student head;
	menu(&head);
	system("pause");
}

你可能感兴趣的:(学生管理系统)