c++导入txt文件并依次显示内容(学生管理系统)

2024-12-22 22:36:50
推荐回答(1个)
回答1:

student头文件

[html] view plain copy
#pragma once
#include
#include

using namespace std;

class Student
{
public:
Student() :num(0),name(""),score(0) {}
Student(int num,string name,double score);
~Student();
void setStudent(int n,string nam,double s);
void showStudent();

friend class Teacher;
private:
int num;
string name;
double score;
};

teacher.h

[html] view plain copy
#pragma once
#include
#include
#include "student.h"

using namespace std;

const int upNum = 1000;

class Teacher
{
public:
Teacher();
virtual ~Teacher();
void work();
void displayStudent();
void insert();
void deleteStudent();
void searchStudent();
void saveStudent();
private:
int N;//用户数目
Student students[upNum];//student()
};

int pass();
int chooseInMenu();
int inputPassword();

student.cpp

[html] view plain copy
#include"student.h"
#include"teacher.h"

Student::Student(int num,string name,double score):num(num),name(name),score(score){}

Student::~Student(){}

void Student::setStudent(int n, string nam, double s)
{
this->num = n;
this->name = nam;
this->score = s;
}

void Student::showStudent()
{
cout << num << " " << name << " " << score << endl;
}

teacher.cpp

[html] view plain copy
#include "teacher.h"

#include
#include
#include
#include
#include
using namespace std;

Teacher::Teacher()
{
ifstream infile("student.txt", ios::in);
if (!infile)
{
cerr << "open error" << endl;

exit(1);
}
int i = 0;
int n;
string nam;

double sco;

while (infile >> n >> nam >> sco )
{
students[i].setStudent(n,nam,sco);
i++;
}
N = i;
infile.close();
}

Teacher::~Teacher()
{
}

void Teacher::saveStudent()
{
ofstream outfile("student.txt", ios::out);
if (!outfile)
{
cerr << "open error" << endl;
exit(1);
}

int i;
int k = N;
for (i = 0; i < k; ++i)
{
outfile << students[i].num << " ";
outfile << students[i].name << " ";
outfile << students[i].score << " ";
}
getchar();
getchar();
outfile.close();
}

void Teacher::work()
{
int iChoice;
int j = 0;

while (1)
{
iChoice = chooseInMenu();
switch (iChoice)
{
case 1:insert();break;
case 2:deleteStudent();break;
case 3:searchStudent();break;
case 4:displayStudent();break;
case 0:j = 1;break;
default:
break;
}
if (j == 1)
break;
}
}

void Teacher::displayStudent()
{
int i;
cout << "学号" << " " << "姓名" << " " << "操作系统成绩" << endl;
for (i = 0; i < N; i++)
{
students[i].showStudent();
}
}

void Teacher::insert()
{
int n;
string nam;
double sco;

cout << "请输入学号:";
cin >> n;
cout << endl;
cout << "请输入名字:";
cin >> nam;
cout << "请输入操作系统成绩:";
cin >> sco;
students[N].setStudent(n, nam, sco);
N++;

}

void Teacher::deleteStudent()
{
int i = 0;
string nam;
int j = -1;

cout << "请输入要删除人的名字:";
cin >> nam;
for (i = 0; i < N; i++)
{
if (students[i].name == nam)
{
students[i].showStudent();
j = i;
}

}
if (j == -1)
{
cout << "查无此人:" << endl;
}
else
{
for (i = j; j < N; j++)
{
students[i].name = students[i + 1].name;
students[i].num = students[i + 1].num;
students[i].score = students[i + 1].score;
}
N--;
}
}

void Teacher::searchStudent()
{
int i = 0;
string nam;
int j = -1;

cout << "请输入要查找人的名字:";
cin >> nam;
for (i = 0; i < N; i++)
{
if (students[i].name == nam)
{
students[i].showStudent();
j = i;
}

}

}

work.cpp

[html] view plain copy
#include
#include
#include
#include
#include
#include

#include"teacher.h"
using namespace std;

int pass()//验证用户密码,正确返回1,错误返回0
{
char sNameInfile[20];
char sPassInfile[20];
ifstream infile("teacher.txt", ios::in);
if (!infile)
{
cout << "password file cannot open!" << endl;
system("pause");
exit(1);
}
infile >> sNameInfile >> sPassInfile;
infile.close();

char sName[20];
char sPass[20];
char ch;
int iTry = 3;
int right = 0;

do
{
cout << "请输入您的姓名:";
cin >> sName;
cout << "请输入密码:";
int i = 0;
while ((ch = getch()) != '\r')
{
sPass[i++] = ch;
putchar('*');
}
sPass[i] = '\0';
fflush(stdin);
cout << endl;
if (strcmp(sPass, sPassInfile) == 0 && strcmp(sName, sNameInfile) == 0)
{
right = 1;
break;
}
else
{
iTry--;
if (iTry < 0)
cout << "对不起,您已输入超过三次 " << endl;

}

} while (iTry);
return right;
}

int chooseInMenu()
{
int i;
while (1)
{
cout << endl;
cout << "+------------------------------------+" << endl;
cout << "+ 1.插入信息 2.删除信息 +" << endl;
cout << "+ 3.查找信息 4.显示信息 +" << endl;
cout << "+-------------------------------------+" << endl;
cout << "+ 0.退出 +" << endl;
cout << "+------------------------------------+" << endl;
cout << "请输入操作指令:";
cin >> i;
if (i >= 0 && i <= 9)
break;
else
cout << "请重新输入" << endl;

}
return i;
}

int inputPassword()
{
char ch;
int iPass = 0;
int i;
while (1)
{
for (i = 0; i < 6; i++)
{
ch = getch();
putchar('*');
if (isdigit(ch))
iPass = iPass * 10 + (ch - '0');
else
{
iPass = 0;
break;
}
}
fflush(stdin);
cout << endl;
if (iPass == 0)
{
cout << "密码要求全为数字" << endl;
cout << "请重新输入" << endl;
}
else
break;
}
return iPass;
}

main.cpp

[html] view plain copy
#include
#include"teacher.h"
using namespace std;

int main()
{
cout << "+-------------------------------------------+" << endl;
cout << " 学生成绩管理系统 " << endl;
cout << "+-------------------------------------------+" << endl;

Teacher t;
if (pass())
{
Teacher t;
t.work();
t.saveStudent();
}
}