仅为大家提供思路
能跑,但似乎不太对,有时间改
如果有问题希望能帮我指出
目录
题目要求:
代码实现:
源.cpp:
Student.h:
Student.cpp:
题目要求: Create a simple random-access file-processing program that might be used by professors to help manage their student records. For each student, the program should obtain an ID number, the student’s first name, the student’s last name and the student’s grade. The data obtained for each student constitutes a record for the student and should be stored in an object of a class called Student. The program should save the records in a binary file specified by the user (for example “file.dat”). The program should also be able to: (1) Display all records (together with students’ average score) (2) Add/delete the record
(3) Edit each record (i.e. change the ID, name and/or grade of each record)
代码实现:
源.cpp:
#include
#include
#include
#include
#include
#include"Student.h"
using namespace std;
enum Choices { Print = 1, Update, New, Delete, End };//枚举类第一个赋值为1,后几位值顺序递增
int enterChoice()//选择操作类型
{
cout << "nEnter your choice" << endl
<< " 1 - store a formatted text file of student records and average scores " << endl
<< " called"print.txt" for printing " << endl
<< " 2 - update a student record " << endl
<< " 3 - add a student record" << endl
<< " 4 - delete a student record" << endl
<< " 5 - end program " << endl;
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
void outputline(ostream& output, const Student &x)//读入txt文件
{
output << left << setw(10) << x.getID() //left使后续输出左对齐
<< setw(16) << x.getLastName() << setw(11) << x.getFirstName()
<< setw(10) << setprecision(2) << right << fixed << showpoint << x.getGrade() << endl;
}
void creatTextFile(fstream& x)//输出学生信息并求平均分
{
ofstream printStudentRecords("print.txt", ios::out);//输出文件
double sum = 0.0, num = 0.0;
if (!printStudentRecords)
{
cerr << "File could not be oppened" << endl;
exit(EXIT_FAILURE);
}
printStudentRecords << left << setw(10) << "ID" //left使后续输出左对齐
<< setw(16) << "Last Name" << setw(11) << "First Name"
<< setw(10) << right << "Grade" << endl;
Student student;
x.read(reinterpret_cast(&student), sizeof(Student));
while (!x.eof())//从0遍历x文件
{
if (student.getID() != 0)
{
sum += student.getGrade();
num++;
outputline(printStudentRecords, student);
}
x.read(reinterpret_cast(&student), sizeof(Student));
}
sum /= num;
printStudentRecords << "Average:" << sum;
}
void updateRecord(fstream& x)
{
int id;
cout << "Enter ID to update (1-30)";
cin >> id;
x.seekg((id - 1) * sizeof(Student));
Student student;
x.read(reinterpret_cast(&student), sizeof(Student));
if (student.getID() != 0)
{
outputline(cout, student);
cout << "nEnter changed grade: ";
double cgrade;
cin >> cgrade;
student.setGrade(cgrade);
outputline(cout, student);
x.seekp((id - 1) * sizeof(Student));
x.write(reinterpret_cast(&student), sizeof(Student));
}
//seekp:seekput,ostream 的指针移动方法
//seekg:seekget,istream 的指针移动方法
else
cerr << "ID # " << id << " has no information. " << endl;
}
void newRecord(fstream& x)
{
int id;
cout << "Enter ID to add (1-30)";
cin >> id;
x.seekg((id - 1) * sizeof(Student));
Student student;
x.read(reinterpret_cast(&student), sizeof(Student));
if (student.getID() == 0)
{
int g;
string f, l;
cout << "Enter LastName ,FirstName , Grade:n";
cin >> setw(15) >> l;
cin >> setw(10) >> f;
cin >> g;
student.setID(id);
student.setFirstName(f);
student.setLastName(l);
student.setGrade(g);
x.seekp((id - 1) * sizeof(Student));
x.write(reinterpret_cast(&student), sizeof(Student));
}
else
cerr << "ID # " << id << "already contains information. " << endl;
}
void deleteRecord(fstream& x)
{
int id;
cout << "Enter ID to delete (1-30)";
cin >> id;
x.seekg((id - 1) * sizeof(Student));
Student student;
x.read(reinterpret_cast(&student), sizeof(Student));
if (student.getID() != 0)
{
student.setID(0);
student.setFirstName(" ");
student.setLastName(" ");
student.setGrade(0);
x.seekp((id - 1) * sizeof(Student));
x.write(reinterpret_cast(&student), sizeof(Student));
cout << " ID # " << id << " delete." << endl;
}
else
cerr << "ID # " << id << " is empty. " << endl;
}
int main()
{
int id, g;
string f, l;
fstream inOutFile("file.dat", ios::in | ios::out | ios::binary);//创建一个文件以二进制写入数据
if (!inOutFile)
{
cerr << "File could not be oppened" << endl;
exit(EXIT_FAILURE);
}
//创建有30个顺序空记录的随机存取文件
Student studentEmpty;
for (int i = 0; i < 30; i++)
{
inOutFile.write(reinterpret_cast(&studentEmpty), sizeof(Student));
}
for (int i = 0; i < 5; i++)
{
cout << "ID , LastName ,FirstName , Grade:" << endl;
cin >> id >> l >> f >> g;
Student studentData(id, f, l, g);
inOutFile.seekp((studentData.getID() - 1) * sizeof(Student));//计算记录的字节的地址及设置put文件定位指针
inOutFile.write(reinterpret_cast(&studentData), sizeof(Student));
}
int choice;
while ((choice = enterChoice()) != End)
{
switch (choice)
{
case Print:
creatTextFile(inOutFile);
break;
case Update:
updateRecord(inOutFile);
break;
case New:
newRecord(inOutFile);
break;
case Delete:
deleteRecord(inOutFile);
break;
default:
cerr << "Incorrect choice" << endl;
}
}
return 0;
}
Student.h:
#pragma once
#include
using namespace std;
class Student
{
public:
Student(int = 0, string = "", string = "", double = 0.0);
void setID(int);
void setGrade(double);
void setFirstName(string);
void setLastName(string);
int getID() const;
double getGrade() const;
string getFirstName() const;
string getLastName() const;
void print()const;
private:
int ID;
double grade;
string firstName, lastName;
};
Student.cpp:
#include "Student.h"
#include
#include
#include
using namespace std;
Student::Student(int id, string f, string l, double g)
{
setID(id);
setFirstName(f);
setLastName(l);
setGrade(g);
}
void Student::setID(int id)
{
ID = id;
}
void Student::setGrade(double g)
{
grade = g;
}
void Student::setFirstName(string f)
{
firstName = f;
}
void Student::setLastName(string l)
{
lastName = l;
}
int Student::getID() const
{
return ID;
}
double Student::getGrade() const
{
return grade;
}
string Student::getFirstName() const
{
return firstName;
}
string Student::getLastName() const
{
return lastName;
}
void Student::print()const
{
cout << left << setw(10) << ID //left使后续输出左对齐
<< setw(16) << lastName << setw(11) << firstName
<
#include#include #include #include #include #include"Student.h" using namespace std; enum Choices { Print = 1, Update, New, Delete, End };//枚举类第一个赋值为1,后几位值顺序递增 int enterChoice()//选择操作类型 { cout << "nEnter your choice" << endl << " 1 - store a formatted text file of student records and average scores " << endl << " called"print.txt" for printing " << endl << " 2 - update a student record " << endl << " 3 - add a student record" << endl << " 4 - delete a student record" << endl << " 5 - end program " << endl; int menuChoice; cin >> menuChoice; return menuChoice; } void outputline(ostream& output, const Student &x)//读入txt文件 { output << left << setw(10) << x.getID() //left使后续输出左对齐 << setw(16) << x.getLastName() << setw(11) << x.getFirstName() << setw(10) << setprecision(2) << right << fixed << showpoint << x.getGrade() << endl; } void creatTextFile(fstream& x)//输出学生信息并求平均分 { ofstream printStudentRecords("print.txt", ios::out);//输出文件 double sum = 0.0, num = 0.0; if (!printStudentRecords) { cerr << "File could not be oppened" << endl; exit(EXIT_FAILURE); } printStudentRecords << left << setw(10) << "ID" //left使后续输出左对齐 << setw(16) << "Last Name" << setw(11) << "First Name" << setw(10) << right << "Grade" << endl; Student student; x.read(reinterpret_cast (&student), sizeof(Student)); while (!x.eof())//从0遍历x文件 { if (student.getID() != 0) { sum += student.getGrade(); num++; outputline(printStudentRecords, student); } x.read(reinterpret_cast (&student), sizeof(Student)); } sum /= num; printStudentRecords << "Average:" << sum; } void updateRecord(fstream& x) { int id; cout << "Enter ID to update (1-30)"; cin >> id; x.seekg((id - 1) * sizeof(Student)); Student student; x.read(reinterpret_cast (&student), sizeof(Student)); if (student.getID() != 0) { outputline(cout, student); cout << "nEnter changed grade: "; double cgrade; cin >> cgrade; student.setGrade(cgrade); outputline(cout, student); x.seekp((id - 1) * sizeof(Student)); x.write(reinterpret_cast (&student), sizeof(Student)); } //seekp:seekput,ostream 的指针移动方法 //seekg:seekget,istream 的指针移动方法 else cerr << "ID # " << id << " has no information. " << endl; } void newRecord(fstream& x) { int id; cout << "Enter ID to add (1-30)"; cin >> id; x.seekg((id - 1) * sizeof(Student)); Student student; x.read(reinterpret_cast (&student), sizeof(Student)); if (student.getID() == 0) { int g; string f, l; cout << "Enter LastName ,FirstName , Grade:n"; cin >> setw(15) >> l; cin >> setw(10) >> f; cin >> g; student.setID(id); student.setFirstName(f); student.setLastName(l); student.setGrade(g); x.seekp((id - 1) * sizeof(Student)); x.write(reinterpret_cast (&student), sizeof(Student)); } else cerr << "ID # " << id << "already contains information. " << endl; } void deleteRecord(fstream& x) { int id; cout << "Enter ID to delete (1-30)"; cin >> id; x.seekg((id - 1) * sizeof(Student)); Student student; x.read(reinterpret_cast (&student), sizeof(Student)); if (student.getID() != 0) { student.setID(0); student.setFirstName(" "); student.setLastName(" "); student.setGrade(0); x.seekp((id - 1) * sizeof(Student)); x.write(reinterpret_cast (&student), sizeof(Student)); cout << " ID # " << id << " delete." << endl; } else cerr << "ID # " << id << " is empty. " << endl; } int main() { int id, g; string f, l; fstream inOutFile("file.dat", ios::in | ios::out | ios::binary);//创建一个文件以二进制写入数据 if (!inOutFile) { cerr << "File could not be oppened" << endl; exit(EXIT_FAILURE); } //创建有30个顺序空记录的随机存取文件 Student studentEmpty; for (int i = 0; i < 30; i++) { inOutFile.write(reinterpret_cast (&studentEmpty), sizeof(Student)); } for (int i = 0; i < 5; i++) { cout << "ID , LastName ,FirstName , Grade:" << endl; cin >> id >> l >> f >> g; Student studentData(id, f, l, g); inOutFile.seekp((studentData.getID() - 1) * sizeof(Student));//计算记录的字节的地址及设置put文件定位指针 inOutFile.write(reinterpret_cast (&studentData), sizeof(Student)); } int choice; while ((choice = enterChoice()) != End) { switch (choice) { case Print: creatTextFile(inOutFile); break; case Update: updateRecord(inOutFile); break; case New: newRecord(inOutFile); break; case Delete: deleteRecord(inOutFile); break; default: cerr << "Incorrect choice" << endl; } } return 0; }
Student.h:
#pragma once
#include
using namespace std;
class Student
{
public:
Student(int = 0, string = "", string = "", double = 0.0);
void setID(int);
void setGrade(double);
void setFirstName(string);
void setLastName(string);
int getID() const;
double getGrade() const;
string getFirstName() const;
string getLastName() const;
void print()const;
private:
int ID;
double grade;
string firstName, lastName;
};
Student.cpp:
#include "Student.h"
#include
#include
#include
using namespace std;
Student::Student(int id, string f, string l, double g)
{
setID(id);
setFirstName(f);
setLastName(l);
setGrade(g);
}
void Student::setID(int id)
{
ID = id;
}
void Student::setGrade(double g)
{
grade = g;
}
void Student::setFirstName(string f)
{
firstName = f;
}
void Student::setLastName(string l)
{
lastName = l;
}
int Student::getID() const
{
return ID;
}
double Student::getGrade() const
{
return grade;
}
string Student::getFirstName() const
{
return firstName;
}
string Student::getLastName() const
{
return lastName;
}
void Student::print()const
{
cout << left << setw(10) << ID //left使后续输出左对齐
<< setw(16) << lastName << setw(11) << firstName
<
#include "Student.h" #include#include #include using namespace std; Student::Student(int id, string f, string l, double g) { setID(id); setFirstName(f); setLastName(l); setGrade(g); } void Student::setID(int id) { ID = id; } void Student::setGrade(double g) { grade = g; } void Student::setFirstName(string f) { firstName = f; } void Student::setLastName(string l) { lastName = l; } int Student::getID() const { return ID; } double Student::getGrade() const { return grade; } string Student::getFirstName() const { return firstName; } string Student::getLastName() const { return lastName; } void Student::print()const { cout << left << setw(10) << ID //left使后续输出左对齐 << setw(16) << lastName << setw(11) << firstName <



