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

📄 function.cpp

📁 学生基本信息管理
💻 CPP
字号:
#include "head.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cassert>

using namespace std;
#include <conio.h>

//生日类构造函数
birthday::birthday()
{
	year = 0;
	month = 0;
	day = 0;
}

//得到年份 
int birthday::get_year()
{
	return year;
}

//得到月份
int birthday::get_month()
{
	return month;
}

//得到日
int birthday::get_day()
{
	return day;
}

//设置出生日期
void birthday::set_birth()
{
	cin >>year >>month >>day;
}

//学生类默认构造函数
student::student():birth()
{
	name = "\0";
	number = "\0";
	sex = '\0';
	bornPlace = " \0";
    age = 0;
	address = " \0";
}

//首页
char student::face()
{
	char choose;
	
	system("cls");
	system("color 3e");
	cout <<endl;
	cout <<"\t     ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇           学 生 基 本 信 息 管 理 系 统          ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              1. 新生登记                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              2. 浏览信息                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              3. 查询信息                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              4. 修改信息                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              5. 删除信息                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇              6. 关闭系统                         ◇" <<endl
		<<"\t     ||                                                  ||" <<endl
		<<"\t     ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl <<"\t\t";
	
	choose = getch();
	fflush(stdin);
	
	return choose;
}

//新学生登记
void student::inster_new()
{
	system("cls");
	cout <<endl <<"\t新学生登记" <<endl <<endl;
	cout <<"\t     姓名 : ";
	cin >>name;
	cout <<endl <<"\t     学号 : ";
	cin >>number;
	cout <<endl <<"\t性别(m/w) : ";
	cin >>sex;
	cout <<endl <<"\t     年龄 : ";
	cin >>age;
	cout <<endl <<"\t 出生日期 : ";
	birth.set_birth();
	cout <<endl <<"\t     籍贯 : ";
	cin >>bornPlace;
	cout <<endl <<"\t     住址 : ";
	cin >>address;
	
	ofstream stuFile("students\\stu.txt", ios::app);
	assert(stuFile);
	stuFile << setiosflags(ios::left) << setw(30) << number << " " << setw(30) << name << endl;
	stuFile.close();
	
	string stuFileName = "students\\"+number+".txt";
	ofstream stuFile1(stuFileName.c_str());
	assert(stuFile1);

	stuFile1 <<"姓名 : " <<name <<endl <<endl
		<<"学号 : "<<number <<endl <<endl
		<<"性别 : "<<sex <<endl <<endl
		<<"年龄 : "<<age <<endl <<endl
		<<"出生日期 : " <<birth.get_year() <<"." <<birth.get_month() <<"." <<birth.get_day() <<endl <<endl
		<<"籍贯 : " <<bornPlace <<endl <<endl
		<<"住址 : " <<address <<endl;
	stuFile1.close();
	
	cout <<endl <<endl <<"\t新生登记完成!!!!" <<endl<<endl <<"\t";
	system("pause");
}

//显示所有学生信息
void student::show_all()
{
	system("cls");
	cout <<endl <<"所有学生信息如下 : " <<endl <<endl;
	
	ifstream stuFile("students\\stu.txt");
	if (!stuFile)
	{
		cout << endl << endl << "学生信息为空!!" << endl << endl;
		system("pause");
		return;
	}
	string str1;
	string str2;
	string str3;

	while (stuFile >>number >> name)
	{
		number = "students\\"+number+".txt";
		ifstream stuFile1(number.c_str());
		assert(stuFile1);
        cout <<stuFile1.rdbuf() <<endl <<endl;
		stuFile1.close();
	}
	stuFile.close();
	
	cout <<endl <<endl <<"所有学生信息显示完成!!!!" <<endl <<endl;
	system("pause");
}

//学生信息查询
void student::check()
{
	while (1)
	{
		system("cls");
		cout <<endl <<"\t学生信息查询" <<endl <<endl;
		cout << "\t●\t1. 按学号查询" << endl << endl
			<< "\t●\t2. 按姓名查询" << endl << endl
			<< "\t●\t3. 返回" << endl << endl << "\t";

		switch (getch())
		{
		case '1':
            select_no();
			break;
		case '2':
			select_name();
			break;
		case '3':
			return;
			break;
		default:
			break;
		}
	}
}

//按姓名查询
void student::select_name()
{
	cout <<"请输入查询姓名 : ";
	string tempName;
	cin >> tempName;

	ifstream stuFile("students\\stu.txt");
	assert(stuFile);
	while (stuFile >> number >> name)
	{
		if (name == tempName)
		{
			cout <<endl <<number <<"的信息如下 : " <<endl;
			number = "students\\"+number+".txt";
			ifstream stuFile1(number.c_str());
			assert(stuFile1);
			cout <<endl <<stuFile1.rdbuf() <<endl <<endl;
			cout <<"查询完毕!!!!" <<endl <<endl;
			system("pause");
			return;
		}
	}
}

//按学号查询
void student::select_no()
{
	cout <<"请输入查询学号 : ";
	string no;
	cin >>no;
	ifstream stuFile("students\\stu.txt");
	assert(stuFile);
	while (stuFile >>number >> name)
	{
		if (no == number)
		{
			cout <<endl <<number <<"的信息如下 : " <<endl;
			number = "students\\"+number+".txt";
			ifstream stuFile1(number.c_str());
			assert(stuFile1);
			cout <<endl <<stuFile1.rdbuf() <<endl <<endl;
			cout <<"查询完毕!!!!" <<endl <<endl;
			system("pause");
			return;
		}
	}
}

//修改学生信息
void student::alter()
{
	system("cls");
	cout <<endl <<"\t信息修改" <<endl <<endl;
	cout <<"\t请输入学号 : ";
	string no;
	cin >>no;
	
	ifstream stuFile("students\\stu.txt");
	while (stuFile >>number)
	{
		if (no == number)
		{
			cout <<endl <<number <<"以前的信息如下 : " <<endl <<endl;
			number = "students\\"+number+".txt";
			ifstream stuFile1(number.c_str());
			assert(stuFile1);
			cout <<stuFile1.rdbuf() <<endl <<endl;
			cout <<endl <<"请根据提示输入新的信息 : " <<endl <<endl;
			cout <<"\t     姓名 : ";
			cin >>name;
			cout <<endl <<"\t性别(m/w) : ";
			cin >>sex;
			cout <<endl <<"\t     年龄 : ";
			cin >>age;
			cout <<endl <<"\t 出生日期 : ";
			birth.set_birth();
			cout <<endl <<"\t     籍贯 : ";
			cin >>bornPlace;
			cout <<endl <<"\t     住址 : ";
			cin >>address;
			
			string stuFileName = "students\\"+no+".txt";
			ofstream stuFile2(stuFileName.c_str());
			assert(stuFile2);

			stuFile2 <<"姓名 : " <<name <<endl <<endl
				<<"学号 : "<<no <<endl <<endl
				<<"性别 : "<<sex <<endl <<endl
				<<"年龄 : "<<age <<endl <<endl
				<<"出生日期 : " <<birth.get_year() <<"." <<birth.get_month() <<"." <<birth.get_day() <<endl <<endl
				<<"籍贯 : " <<bornPlace <<endl <<endl
				<<"住址 : " <<address <<endl;
			stuFile2.close();
			
			cout <<endl <<endl <<"\t信息修改完成!!!!" <<endl<<endl <<"\t";
			system("pause");
			return;
		}
	}
}

//删除学生信息
void student::deletes()
{
	char choose;
	
	while (1)
	{
		system("cls");
		cout <<endl <<"\t删除学生信息" <<endl <<endl;
		cout <<endl <<"\t●\t1.  删除除一个学生记录" <<endl
			<<endl <<"\t●\t2.  删除所有学生记录" <<endl 
			<<endl <<"\t●\t3.  返回首页" <<endl <<endl <<"\t\t";
		
		choose = getch();
		fflush(stdin);
		
		switch (choose)
		{
		case '1':
			delete_sigle();//删除一个学生记录
			break;
		case '2':
			delete_all();//删除所有学生信息
			break;
		case '3':
			return;
		default:
			break;
		}
	}
}

//删除一个学生记录
void student::delete_sigle()
{
	system("cls");
	cout <<endl <<"\t删除学生信息" <<endl <<endl;
	cout <<"\t请输入学号 : ";
	string no;
	cin >>no;
	fflush(stdin);

	ifstream stuFile("students\\stu.txt");
	assert(stuFile);
    while (stuFile >>number)
	{
		if (no == number)
		{
			number = "students\\"+number+".txt";
			ifstream stuFile1(number.c_str());
			assert(stuFile1);
			cout <<endl <<"你想删除的学生信息如下 : " <<endl <<endl;
			cout <<stuFile1.rdbuf() <<endl;
			stuFile1.close();

			cout <<endl <<"你确定要删除吗?(y/n) : ";
			if (getch() == 'y')
			{
				ifstream stuFile2("students\\stu.txt");
				assert(stuFile2);
				ofstream tempFile("students\\temp.txt");
				assert(tempFile);

				while (stuFile2 >>number)
				{
				    if (no != number)
					{
						tempFile <<number <<endl;
					}
				}
				stuFile2.close();
				tempFile.close();

				ifstream tempFile1("students\\temp.txt");
				assert(tempFile1);
				ofstream stuFile3("students\\stu.txt");
				assert(stuFile3);
				stuFile3 <<tempFile1.rdbuf();
				tempFile1.close();
				stuFile3.close();

				number = "students\\"+no+".txt";
				ofstream stuFile4(number.c_str());
				stuFile4.close();
				cout <<endl <<endl <<"该生信息已经删除!!" <<endl <<endl <<"\t";
				system("pause");
			}
			break;
		}
	}
}

//删除所有学生记录
void student::delete_all()
{
	cout <<endl <<endl <<"\t你确定在删除所有学生信息吗? 此操作不可恢复.(y/n) : ";

	if (getch() == 'y')
	{
		ifstream stuFile("students\\stu.txt");
		if (!stuFile)
		{
			cout << endl << endl << "学生信息为空!!" << endl << endl;
			system("pause");
			return;
		}

		string fileName;
		while (stuFile >> number >> name)
		{
			fileName = "students\\" + number + ".txt";
        	string str = "del students\\" + number + ".txt";
			system(str.c_str());
		}

		stuFile.close();
		system("del students\\stu.txt");
		cout << endl << endl << "\t所用学生信息已经删除!!!" << endl << endl << "\t";
	}
	else
	{
		cout << endl << endl << "\t你取消了此操作!!!" << endl << endl << "\t";
	}

	system("pause");
}

⌨️ 快捷键说明

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