📄 function.cpp
字号:
#include "head.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//默认构造函数
Student::Student()
{
name = "XXXXXX";
no = "XXXXX";
score[0] = 0.0;
score[1] = 0.0;
score[2] = 0.0;
}
//菜单
char Student::inter_face()
{
system("cls");
cout <<endl <<"\t※※※※※※※※※※※※※※※※※※※※※※※※※" <<endl;
cout <<"\t※\t\t\t\t\t\t※";
cout<<"\n\t※\t\t 学生成绩管理系统\t\t※"<<endl
<<"\t※\t\t\t\t\t\t※"<<endl
<<"\t※\t1. 录入学生成绩.\t\t\t※"<<endl
<<"\t※\t2. 查询学生成绩.\t\t\t※"<<endl
<<"\t※\t3. 修改学生成绩.\t\t\t※"<<endl
<<"\t※\t4. 删除学生记录.\t\t\t※"<<endl
<<"\t※\t5. 显示所有学生成绩.\t\t\t※"<<endl
<<"\t※\t6. 还原初始成绩记录.\t\t\t※" <<endl
<<"\t※\t7. 退出系统.\t\t\t\t※"<<endl
<<"\t※\t\t\t\t\t\t※" <<endl
<<"\t※※※※※※※※※※※※※※※※※※※※※※※※※" <<endl <<endl
<<"\t\t选择: ";
char choose;
cin >>choose;
return choose;
}
//录入学生成绩
void Student::input()
{
char scor1[20];
char scor2[20];
char scor3[20];
cout <<endl <<endl <<"请根据提示输入学生相关信息:" <<endl <<endl;
cout <<"姓名: ";
cin >>name;
fflush(stdin);
cout <<"学号: ";
cin >>no;
fflush(stdin);
cout <<"三科分数:" <<endl;
cout <<"C 语言: ";
gets(scor1);
score[0] = atof(scor1);
fflush(stdin);
cout <<"数据对构: ";
gets(scor2);
score[1] = atof(scor2);
fflush(stdin);
cout <<"Java语言: ";
gets(scor3);
score[2] = atof(scor3);
fflush(stdin);
save_info();//保存信息
cout <<endl <<endl <<"你输入的学生信息已经保存!!!!!!!" <<endl <<endl;
system("pause");
}
//查询学生成绩
void Student::select()
{
char choose;
while (1)
{
system("cls");
cout <<endl <<"学生成绩查询: " <<endl <<endl
<<"\t1. 单个学生成绩查询. " <<endl <<endl
<<"\t2. 指定范围查询. " <<endl <<endl
<<"\t3. 返回. " <<endl <<endl
<<" 选择: ";
cin >>choose;
fflush(stdin);
switch(choose)
{
case '1':
sigle_sel();//单个学生成绩查询
break;
case '2':
many_sel();//指定范围查询
break;
case '3':
return;
default:
break;
}
}
}
//单个学生成绩查询
void Student::sigle_sel()
{
ifstream inData("input.txt", ios::in);
if (!inData)
{
cout <<endl <<endl <<"对不起!!不能打开这个文件!!!!" <<endl <<endl;
system("pause");
return;
}
bool flag = true;
string str1;
string str;
string sign;
cout <<endl <<"输入查询学生的姓名或学号: ";
cin >>sign;
fflush(stdin);
getline(inData, str1);
while (inData >>name >>no)
{
getline(inData, str);
if ((name==sign) || (no==sign))
{
cout <<endl <<str1 <<endl;
cout <<endl <<setiosflags(ios::left) <<setw(18) <<name
<<" "<<no <<str <<endl <<endl;
flag = false;
}
}
if (flag)
{
cout <<"文件中没有你查找的学生成绩!!!!" <<endl <<endl;
}
else
{
cout <<"该学生成绩已经找到!!!!!" <<endl <<endl;
}
system("pause");
inData.close();
}
//指定范围查询
void Student::many_sel()
{
ifstream inData("input.txt", ios::in);
if (!inData)
{
cout <<endl <<"对不起!!!不能打开这个文件!!!!" <<endl <<endl;
system("pause");
return;
}
string minno;
string maxno;
cout <<endl <<endl <<"请在下面输入学号范围 :" <<endl <<endl;
cout <<"最小学号: ";
cin >>minno;
fflush(stdin);
cout <<"最大学号: ";
cin >>maxno;
fflush(stdin);
string str1;
string str;
bool flag = true;
getline(inData, str1);
while (inData >>name >>no)
{
getline(inData, str);
if ((no<=maxno) && (no>=minno))
{
if (flag)
{
cout <<endl <<str1 <<endl <<endl;
flag = false;
}
cout <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<no <<str <<endl;
}
}
if (flag)
{
cout <<endl <<"文件中没有你查询的对象!!!!" <<endl <<endl;
}
else
{
cout <<endl <<"你确定范围内的学生都已查询完!!!!" <<endl <<endl;
}
system("pause");
inData.close();
}
//修改学生成绩
void Student::upData()
{
ifstream inData("input.txt", ios::in);
ofstream outData("temp.txt", ios::out);
if (!inData || !outData)
{
cout <<endl <<"对不起!!!!这个文件打不开!!!!" <<endl <<endl;
system("pause");
return;
}
char scor1[20];
char scor2[20];
char scor3[20];
string sign;
cout <<endl <<"输入你想修改的学生姓名或学号 : ";
cin >>sign;
fflush(stdin);
string str1;
string str;
getline(inData, str1);
outData <<str1 <<endl;
bool flag = true;
while (inData >>name >>no)
{
getline(inData, str);
if ((sign==name) || (sign==no))
{
if (flag)
{
cout <<endl <<"你想修改的学生的成绩如下 :" <<endl;
cout <<str1 <<endl <<endl;
flag = false;
}
cout <<setiosflags(ios::left) <<setw(18) <<name
<<" "<<no <<str <<endl <<endl;
cout <<"请按提示输入该生新的信息: " <<endl;
cout <<"姓名: ";
cin >>name;
fflush(stdin);
cout <<"三科分数:" <<endl;
cout <<"C 语言: ";
gets(scor1);
score[0] = atof(scor1);
fflush(stdin);
cout <<"数据对构: ";
gets(scor2);
score[1] = atof(scor2);
fflush(stdin);
cout <<"Java语言: ";
gets(scor3);
score[2] = atof(scor3);
fflush(stdin);
outData <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<setw(18) <<no <<" "
<<setw(11) <<score[0] <<" "
<<setw(12) <<score[1] <<" "
<<setw(11) <<score[2] <<" " <<endl;
break;
}
outData <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<no <<str <<endl;
}
while (getline(inData, str))
{
outData <<str <<endl;
}
inData.close();
outData.close();
if (flag)
{
cout <<endl <<"对不起!!!文件中没有你想修改的学生成绩!!!!" <<endl <<endl;
}
else
{
ofstream out("input.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!in || !out)
{
cout <<endl <<"对不起!!!!这个文件打不开!!!!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close();
in.close();
cout <<endl <<"该生的信息已经被修改,并保存!!!!" <<endl <<endl;
}
system("pause");
}
//删除学生成绩
void Student::delet()
{
char choose;
while (1)
{
system("cls");
cout <<endl <<"删除学生成绩记录 : " <<endl <<endl
<<"\t1. 删除单条记录. " <<endl <<endl
<<"\t2. 选定范围删除. " <<endl <<endl
<<"\t3. 删除所用学生记录. " <<endl <<endl
<<"\t4. 返回. " <<endl <<endl
<<" 选择: ";
cin >>choose;
fflush(stdin);
switch(choose)
{
case '1':
sigle_del();//删除单个学生记录
break;
case '2':
many_del();//指定范围删除
break;
case '3':
all_del();//删除所有学生记录
break;
case '4':
return;
default:
break;
}
}
}
//删除单个学生记录
void Student::sigle_del()
{
ofstream outData("temp.txt", ios::out);
ifstream inData("input.txt", ios::in);
if (!outData || !inData)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<"请输入你想删除学生的姓名或学号: ";
cin >>sign;
fflush(stdin);
string str1;
string str;
bool flag = true;
getline(inData, str1);
outData <<str1 <<endl;
while (inData >>name >>no)
{
getline(inData, str);
if ((name==sign) || (no==sign))
{
cout <<endl <<"你想删除的学生成绩如下 :" <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<no <<str <<endl <<endl;
flag = false;
}
else
{
outData <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<no <<str <<endl;
}
}
inData.close();
outData.close();
if (flag)
{
cout <<endl <<"对不起!!文件中没有这个学生的记录!!!" <<endl <<endl;
}
else
{
ofstream out("input.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
cout <<endl <<"该学生的信息已经全部删除!!!!" <<endl <<endl;
outData.close();
inData.close();
}
system("pause");
}
//指定范围删除
void Student::many_del()
{
ofstream outData("temp.txt", ios::out);
ifstream inData("input.txt", ios::in);
if (!outData || !inData)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
string minno;
string maxno;
cout <<endl <<endl <<"请在下面输入学号范围 :" <<endl <<endl;
cout <<"最小学号: ";
cin >>minno;
fflush(stdin);
cout <<"最大学号: ";
cin >>maxno;
fflush(stdin);
string str1;
string str;
getline(inData, str1);
outData <<str1 <<endl;
bool flag = true;
while (inData >>name >>no)
{
getline(inData, str);
if ((no<=maxno) && (no>=minno))
{
if (flag)
{
cout <<endl <<"你要删除的学生成绩如下 :" <<endl;
cout <<endl <<str1 <<endl <<endl;
flag = false;
}
cout <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<no <<str <<endl;
}
else
{
outData <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<setw(18) <<no <<str <<endl;
}
}
inData.close();
outData.close();
if (flag)
{
cout <<endl <<"文件中没有你想查询的学生记录!!!" <<endl <<endl;
}
else
{
ifstream in("temp.txt", ios::in);
ofstream out("input.txt", ios::out);
if (!out || !in)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
in.close();
out.close();
cout <<endl <<"这些学后的所的记录都已经被删除!!!!" <<endl <<endl;
}
system("pause");
}
//删除所有学生记录
void Student::all_del()
{
ifstream inData("input.txt", ios::in);
if (!inData)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
string record;
getline(inData, record);
inData.close();
ofstream out("input.txt", ios::out);
if (!out)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
out.close();
ofstream outData("input.txt", ios::out);
if (!outData)
{
cout <<endl <<"对不起!!!这文件不能打开!!!!" <<endl <<endl;
system("pause");
return;
}
outData <<record <<endl;
outData.close();
cout <<endl <<"所用学生记录都已经删除!!!!" <<endl <<endl;
system("pause");
}
//显示所有学生成绩
void Student::show_all()
{
ifstream inData("input.txt", ios::in);
if (!inData)
{
cout <<endl <<"对不起不能打开文件!!!!" <<endl <<endl;
system("pause");
return;
}
string str;
getline(inData, str);
string record;
bool flag = false;
while (getline(inData, record))
{
if (!flag)
{
cout <<endl <<"所用学生成绩如下: "<<endl <<endl;
cout <<str <<endl;
flag = true;
}
cout <<record <<endl;
}
if (!flag)
{
cout <<endl <<endl <<"文件中没有任学生记录!!!!" <<endl <<endl;
}
else
{
cout <<endl <<endl <<"所有学生成绩显示完成!!!!" <<endl <<endl;
}
system("pause");
inData.close();
}
//还原初始成绩记录
void Student::back_init()
{
ifstream inData("back.txt", ios::in);
ofstream outData("input.txt", ios::out);
if (!inData || !outData)
{
cout <<endl <<"对不起,你想打开的文件不存在!!!!" <<endl <<endl;
system("pause");
}
string record;
while (getline(inData, record))
{
outData <<record <<endl;
}
cout <<endl <<endl <<"学生成绩记录已经还原成初始记录!!!!!" <<endl <<endl;
system("pause");
inData.close();
outData.close();
}
//保存记录
void Student::save_info()
{
ofstream inData("input.txt", ios::out|ios::app);
if (!inData)
{
cout <<endl <<"对不起, 你想打开的文件不存在!!!!" <<endl <<endl;
system("pause");
}
inData <<setiosflags(ios::left) <<setw(18) <<name
<<" " <<setw(18) <<no
<<" " <<setw(11) <<score[0]
<<" " <<setw(12) <<score[1]
<<" " <<setw(11) <<score[2]
<<endl;
inData.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -