C++ 学籍管理系统的简单实现

仅为大家提供思路

能跑,但似乎不太对,有时间改

如果有问题希望能帮我指出

目录

题目要求:

代码实现:

源.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<iostream>
#include<fstream>
#include<string>
#include<windows.h>
#include<iomanip>
#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<char*>(&student), sizeof(Student));
	while (!x.eof())//从0遍历x文件
	{
		if (student.getID() != 0)
		{
			sum += student.getGrade();
			num++;
			outputline(printStudentRecords, student);
		}
		x.read(reinterpret_cast<char*>(&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<char*>(&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<const char*>(&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<char*>(&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<const char*>(&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<char*>(&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<const char*>(&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<const char*>(&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<const char*>(&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;
}
/*
1 Barker Doug 95
2 Brown Nancy 85
3 Stone Sam 80
4 Smith Dave 100
5 Dunn Stacey 60
*/

Student.h:

#pragma once
#include<string>
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<string>
#include<iostream>
#include<iomanip>
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
		<<setw(10)<<setprecision(2)<<right<<fixed<< showpoint << grade << endl;//right使后续输出右对齐,setprecision控制保留浮点数个数
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值