⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 school.cpp

📁 课程设计 学籍管理系统 这个比较简单
💻 CPP
字号:
#include "School.h"
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>

void School::load_total(int *total)
{
	ifstream datafile("total.txt", ios::nocreate);		//如果是第一次进入该系统,则total.txt文件应该不存在
	if (datafile.fail())								//创建文件total.txt,并写入total的值
	{
		*total = 0;
	}
	else												//文件total.txt已经存在,则读取total的值
	{
		datafile >> *total;
		datafile.close();
	}
}

void School::save_total()
{
	ofstream datafile("total.txt");
	datafile << total;
	datafile.close();
}

void School::save_info()
{
	ofstream datafile("stu_info.txt");
	CsStuInfo *p = head->next;

	while (p != NULL)
	{
		p->foutput(datafile);
		p = p->next;
	}

	datafile.close();
}

void School::load_info()
{
	ifstream datafile("stu_info.txt");
	CsStuInfo *p1 = head, *p2;						//p1指向链表的最后一个结点

	int i;
	for (i=1; i<=total; i++)
	{
		p2 = new CsStuInfo;
		p2->finput(p2, datafile);
		p1->next = p2;
		p2->next = NULL;
		p1 = p2;
	}
	datafile.close();
}

School::School()
{
	head = new CsStuInfo;
	head->next = NULL;
	load_total(&total);
	load_info();
}

School::~School()
{
	CsStuInfo *p;

	while (head)
	{
		p = head;
		head = head->next;
		delete p;
	}
}

void School::input()
{
	CsStuInfo *p1, *p2 = head;		//p2指向当前链表的最后一个结点
	bool t = true, temp = true;
	char ch;

	while (p2->next != NULL)
	{
		p2 = p2->next;
	}

	while (t)
	{
		p1 = new CsStuInfo;
		total++;
		p1->input();
		p2->next = p1;
		p1->next = NULL;
		p2 = p2->next;

		while (temp)
		{
			cout << "\t\t要继续录入下一学生信息吗?(Y/N):" << flush;
			ch = getch();
			ch = toupper(ch);
			cout << ch << endl;
			switch(ch)
			{
				case 'Y':
					system("cls");
					temp = false;
					break;
				case 'N':
					t = false;
					temp = false;
					break;
				default:
					break;
			}
		}
	}
	cout << "\t\t已成功录入!" << endl;
	ofstream datafile("total.txt");
	datafile << total;
	datafile.close();
}
void School::output()
{
	CsStuInfo *p = head->next;

	if (p == NULL )
	{
		cout << "\n\n\t\t暂无任何学生信息!" << endl;
		return ;
	}
	else
	{
		while (p != NULL)
		{
			p->output();
			p = p->next;
			cout << "\n\t\t按任意键显示下一学生信息..." << flush;
			getch();
			system("cls");
		}
	}
	cout << "\n\n\t\t显示完毕!" << endl;
}

void School::search_stu()
{
	char ch;
	bool t = true;
	
	
	while (t)
	{	
		cout << "\n\n\t\t\t查询\n\t\t1.按姓名查找\n\t\t2.按学号查找" << endl;
		cout << "\t\t请选择:" << flush;
		ch = getch();
		switch(ch)
		{
			case '1':
				system("cls");
				search_by_name();
				t = false;
				break;
			case '2':
				system("cls");
				search_by_number();
				t = false;
				break;
			default:
				system("cls");
				break;
		}
	}
}

void School::search_by_name()
{
	char name[20];
	CsStuInfo *p = head->next;

	cout << "\n\t\t请输入要查询的学生姓名:" << flush;
	cin >> name;

	system("cls");
	while( p != NULL )
	{
		if(strcmp(p->getname(), name) == 0)
		{
			p->output();
			break;
		}
		p = p->next;
	}
	if (p == NULL)
	{
		cout << "\n\t\t不存在该学生!" << endl;//未找到进行操作
	}
}

void School::search_by_number()
{
	char number[20];
	CsStuInfo *p = head->next;

	cout << "\n\t\t请输入要查询的学生学号:" << flush;
	cin >> number;

	system("cls");
	while( p != NULL )
	{
		if(strcmp(p->getnumber(), number) == 0)
		{
			p->output();
			break;
		}
		p = p->next;
	}
	if (p == NULL)
	{
		cout << "\n\t\t不存在该学生!" << endl;//未找到进行操作
	}
}

void School::delete_stu_info()
{
	char ch;
	bool t = true;
	
	
	while (t)
	{	
		cout << "\n\n\t\t\t删除\n\t\t1.按姓名删除\n\t\t2.按学号删除" << endl;
		cout << "\t\t请选择:" << flush;
		ch = getch();
		switch(ch)
		{
			case '1':
				system("cls");
				delete_by_name();
				t = false;
				break;
			case '2':
				system("cls");
				delete_by_number();
				t = false;
				break;
			default:
				system("cls");
				break;
		}
	}
}

void School::delete_by_name()
{
	char name[20];
	CsStuInfo *p = head, *temp;		//p指向要删除结点的前一结点
	char ch;
	bool t = true;

	cout << "\n\t\t请输入要删除的学生姓名:" << flush;
	cin >> name;

	system("cls");
	while ((p->next != NULL) && (strcmp(p->next->getname(), name) != 0))
	{
		p = p->next;
	}
	temp = p->next;
	if ( p->next != NULL )
	{
		p->next->output();
		while (t)
		{
			cout << "\n\t\t确定删除吗?(Y/N)a" << flush;
			ch = getch();
			ch = toupper(ch);
			switch(ch)
			{
				case 'Y':
					p->next = temp->next;
					delete temp;
					total--;
					system("cls");
					cout << "\n\n\t\t删除成功!" << endl;
					t = false;
					break;
				case 'N':
					t = false;
					break;
				default:
					break;
			}
		}
	}
	else 
	{
		cout << "\n\n\t\t该学生不存在!" << endl;
	}
}

void School::delete_by_number()
{
	char number[20];
	CsStuInfo *p = head, *temp;		//p指向要删除结点的前一结点
	char ch;
	bool t = true;

	cout << "\n\t\t请输入要删除的学生学号:" << flush;
	cin >> number;

	system("cls");
	while ((p->next != NULL) && (strcmp(p->next->getnumber(), number) != 0))
	{
		p = p->next;
	}
	temp = p->next;
	if ( p->next != NULL )
	{
		p->next->output();
		while (t)
		{
			cout << "\n\t\t确定删除吗?(Y/N)" << flush;
			ch = getch();
			ch = toupper(ch);
			switch(ch)
			{
				case 'Y':
					p->next = temp->next;
					delete temp;
					total--;
					system("cls");
					cout << "\n\n\t\t删除成功!" << endl;
					t = false;
					break;
				case 'N':
					t = false;
					break;
				default:
					break;
			}
		}
	}
	else 
	{
		cout << "\n\n\t\t该学生不存在!" << endl;
	}
}

struct info
{
	char name[20], number[20];
	float gpa;
};

void qs(info stu[], int low, int high)
{
	int i=low, j=high;
	info temp = stu[low];

	while (i<j)
	{
		while (i<j && stu[j].gpa>temp.gpa)j--;
		if (i<j)
		{
			stu[i] = stu[j];
			i++;
		}
		while (i<j && stu[i].gpa<temp.gpa)i++;
		if (i<j)
		{
			stu[j] = stu[i];
			j--;
		}
	}
	stu[i] = temp;

	if (i>low)qs(stu, low, i-1);
	if (i<high)qs(stu, i+1, high);
}

void School::calc_stu_score()
{
	info stu[50];
	int i = 0;
	CsStuInfo *p = head->next;
	
	if (p == NULL)
	{
		cout << "\n\n\t\t暂无任何学生信息!" << endl;
		return ;
	}
	else
	{
		while (p != NULL)
		{
			i++;
			strcpy(stu[i].name, p->getname());
			strcpy(stu[i].number, p->getnumber());
			stu[i].gpa = p->getavegpa();
			p = p->next;
		}
	}

	qs(stu, 1, i);
	int k;
	cout << "\n\n\t\t\t     学生排名" << endl;

	for (k=i; k>=1; k--)
	{
		cout << "\t\t+---+-----------+--------+-------+" << endl;
		cout << "\t\t" << "| " << total+1-k << " "
					   << "| " << stu[k].number << " " 
					   << "| " << setw(6) << stu[k].name << " "
					   << "| " << setprecision(3) << setiosflags(ios::fixed) << stu[k].gpa << " |" << endl;
	}
	cout << "\t\t+---+-----------+--------+-------+" << endl << endl << '\t' << flush;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -