The function of the C language campus card simulation system is as shown in the picture. You can take it by yourself.

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const char* STUDENT_DB_FILE = "students.txt";

struct Student {
	char ID[10];
	char name[10];
	char number[10];
	double recharge;
	double remain;
	int state;
};

struct Record {
	char ID[10];
	char Date[10];
	char start_time[10];
	int duration;
	char state;
	char address[10];
};

// 登录模块
bool login(Student& student) {
	char inputID[10];
	char password[10];
	int attempts = 0;
	bool successfulLogin = false;
	
	while (attempts < 3) {
		cout << "请输入学生卡号: ";
		cin >> inputID;
		cout << "请输入密码: ";
		cin >> password;
		
		ifstream studentDB(STUDENT_DB_FILE, ios::in | ios::binary);
		
		while (studentDB.read((char*)&student, sizeof(Student))) {
			if (strcmp(student.ID, inputID) == 0 && strcmp(student.name, password) == 0) {
				successfulLogin = true;
				break;
			}
		}
		
		studentDB.close();
		
		if (successfulLogin) {
			cout << "登录成功!" << endl;
			return true;
		}
		else {
			attempts++;
			cout << "登录失败!剩余尝试次数: " << 3 - attempts << endl;
		}
	}
	
	cout << "账户已冻结,请联系管理员解锁。" << endl;
	return false;
}
bool registerStudent(Student& student) {
	char newID[10];
	char newPassword[10];
	
	ofstream studentDB(STUDENT_DB_FILE, ios::out | ios::binary | ios::app);
	
	cout << "请输入新学生卡号: ";
	cin >> newID;
	cout << "请输入新密码: ";
	cin >> newPassword;
	
	Student newStudent;
	strcpy(newStudent.ID, newID);
	strcpy(newStudent.name, newPassword);
	
	studentDB.write((char*)&newStudent, sizeof(Student));
	
	studentDB.close();
	
	cout << "注册成功!" << endl;
	return true;
}

// 管理员模块
bool adminLogin() {
	char password[10];
	cout << "请输入管理员密码: ";
	cin >> password;
	
	// 假设管理员密码为 "admin123"
	if (strcmp(password, "admin123") == 0) {
		cout << "管理员登录成功!" << endl;
		return true;
	}
	else {
		cout << "管理员登录失败!" << endl;
		return false;
	}
}

void addStudent() {
	ofstream studentDB(STUDENT_DB_FILE, ios::app | ios::binary);
	Student student;
	
	cout << "请输入学生卡号: ";
	cin >> student.ID;
	cout << "请输入姓名: ";
	cin >> student.name;
	cout << "请输入学号: ";
	cin >> student.number;
	cout << "请输入充值数额: ";
	cin >> student.recharge;
	cout << "请输入账户状态(0-正常, 1-挂失, 2-冻结): ";
	cin >> student.state;
	
	// 初始化余额为充值数额
	student.remain = student.recharge;
	
	studentDB.write((char*)&student, sizeof(Student));
	studentDB.close();
	
	cout << "学生信息已添加!" << endl;
}

void recharge(Student& student) {
	double amount;
	cout << "请输入充值数额: ";
	cin >> amount;
	student.recharge += amount;
	student.remain += amount;
	
	fstream studentDB(STUDENT_DB_FILE, ios::in | ios::out | ios::binary);
	Student temp;
	
	while (studentDB.read((char*)&temp, sizeof(Student))) {
		if (strcmp(temp.ID, student.ID) == 0) {
			studentDB.seekp(-sizeof(Student), ios::cur);
			studentDB.write((char*)&student, sizeof(Student));
			break;
		}
	}
	
	studentDB.close();
	
	cout << "充值成功!" << endl;
}

void reportLost(Student& student) {
	student.state = 1; // 设置状态为挂失
	
	fstream studentDB(STUDENT_DB_FILE, ios::in | ios::out | ios::binary);
	Student temp;
	
	while (studentDB.read((char*)&temp, sizeof(Student))) {
		if (strcmp(temp.ID, student.ID) == 0) {
			studentDB.seekp(-sizeof(Student), ios::cur);
			studentDB.write((char*)&student, sizeof(Student));
			break;
		}
	}
	
	studentDB.close();
	
	cout << "挂失成功!" << endl;
}

void resetPassword(Student& student) {
	cout << "请输入新密码: ";
	cin >> student.name;
	
	fstream studentDB(STUDENT_DB_FILE, ios::in | ios::out | ios::binary);
	Student temp;
	
	while (studentDB.read((char*)&temp, sizeof(Student))) {
		if (strcmp(temp.ID, student.ID) == 0) {
			studentDB.seekp(-sizeof(Student), ios::cur);
			studentDB.write((char*)&student, sizeof(Student));
			break;
		}
	}
	
	studentDB.close();
	
	cout << "密码重置成功!" << endl;
}

// 学生模块
void showBalance(Student& student) {
	cout << "账户余额: " << student.remain << " 元" << endl;
}

void showUsageHistory(Student& student) {
	ifstream recordFile(student.ID);
	
	if (!recordFile) {
		cout << "无上机记录" << endl;
		return;
	}
	
	Record record;
	
	cout << "上机记录: " << endl;
	
	while (recordFile.read((char*)&record, sizeof(Record))) {
		cout << "日期: " << record.Date << " 开始时间: " << record.start_time << " 地点: " << record.address << endl;
	}
	
	recordFile.close();
}

void startUsingComputer(Student& student) {
	Record record;
	
	strcpy(record.ID, student.ID);
	cout << "请输入上机日期: ";
	cin >> record.Date;
	cout << "请输入开始时间: ";
	cin >> record.start_time;
	cout << "请输入上机时长: ";
	cin >> record.duration;
	cout << "请输入上机地点: ";
	cin >> record.address;
	
	record.state = '1'; // 标记为上机中
	
	ofstream recordFile(student.ID, ios::app | ios::binary);
	recordFile.write((char*)&record, sizeof(Record));
	recordFile.close();
	
	cout << "上机记录已保存!" << endl;
}

void menu() {
	cout << "------------------------" << endl;
	cout << "1. 登录" << endl;
	cout << "2. 管理员登录" << endl;
	cout << "3. 退出" << endl;
	cout << "4. 注册" << endl;
	cout << "------------------------" << endl;
	cout << "请选择菜单项: ";
}

int main() {
	int choice;
	Student student;
	
	do {
		menu();
		cin >> choice;
		
		switch (choice) {
		case 1:
			if (login(student)) {
				int studentChoice;
				
				do {
					cout << "------------------------" << endl;
					cout << "1. 查询余额" << endl;
					cout << "2. 查询使用记录" << endl;
					cout << "3. 上机" << endl;
					cout << "4. 返回" << endl;
					cout << "------------------------" << endl;
					cout << "请选择菜单项: ";
					cin >> studentChoice;
					
					switch (studentChoice) {
					case 1:
						showBalance(student);
						break;
					case 2:
						showUsageHistory(student);
						break;
					case 3:
						startUsingComputer(student);
						break;
					case 4:
						break;
					default:
						cout << "无效的选项!" << endl;
					}
				} while (studentChoice != 4);
			}
			break;
		case 2:
			if (adminLogin()) {
				int adminChoice;
				
				do {
					cout << "------------------------" << endl;
					cout << "1. 添加学生信息" << endl;
					cout << "2. 充值" << endl;
					cout << "3. 挂失" << endl;
					cout << "4. 重置密码" << endl;
					cout << "5. 返回" << endl;
					cout << "------------------------" << endl;
					cout << "请选择菜单项: ";
					cin >> adminChoice;
					
					switch (adminChoice) {
					case 1:
						addStudent();
						break;
					case 2:
						cout << "请输入学生卡号: ";
						cin >> student.ID;
						recharge(student);
						break;
					case 3:
						cout << "请输入学生卡号: ";
						cin >> student.ID;
						reportLost(student);
						break;
					case 4:
						cout << "请输入学生卡号: ";
						cin >> student.ID;
						resetPassword(student);
						break;
					case 5:
						break;
					default:
						cout << "无效的选项!" << endl;
					}
				} while (adminChoice != 5);
			}
			break;
		case 3:
			cout << "再见!" << endl;
			break;
		case 4:
     		Student student;
			registerStudent(student);
				break;
		default:
			cout << "无效的选项!" << endl;
		}
	} while (choice != 3);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_62088638/article/details/133743285