#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;
struct Student {
string name;
string gender;
string class_name;
string grade;
string id;
};
vector<Student> students;
int student_count = 0;
void display_menu() {
cout << "1. 录入学生" << endl;
cout << "2. 删除学生" << endl;
cout << "3. 查看/录入成绩" << endl;
cout << "4. 退出" << endl;
}
void enter_student() {
string name, gender, class_name, grade, id;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生班级:";
cin >> class_name;
cout << "请输入学生年级:";
cin >> grade;
cout << "请输入学生学号:";
cin >> id;
Student new_student = {name, gender, class_name, grade, id};
students.push_back(new_student);
student_count++;
cout << "录入成功!当前学生人数:" << student_count << endl;
}
void delete_student() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (size_t i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "确认删除吗?(Y/N): ";
char confirm;
cin >> confirm;
if (confirm == 'Y' || confirm == 'y') {
students.erase(students.begin() + i);
student_count--;
cout << "删除成功!" << endl;
return;
} else {
cout << "取消删除。" << endl;
return;
}
}
}
cout << "未找到该学生。" << endl;
}
void enter_scores() {
string id;
cout << "请输入学生学号:";
cin >> id;
for (const auto& student : students) {
if (student.id == id) {
cout << "请输入近5次的成绩:" << endl;
for (int i = 0; i < 5; i++) {
int score;
cin >> score;
// 保存成绩的逻辑,这里省略
}
cout << "成绩录入成功!" << endl;
return;
}
}
cout << "未找到该学生。" << endl;
}
int main() {
string password;
int attempts = 0;
while (attempts < 3) {
cout << "请输入密码:";
cin >> password;
if (password == "802329") {
break;
} else {
attempts++;
cout << "密码错误!剩余尝试次数:" << 3 - attempts << endl;
}
}
if (attempts == 3) {
cout << "密码错误次数过多,程序即将关闭。" << endl;
system("shutdown /s /t 1");
return 0;
}
HWND hwnd = GetConsoleWindow();
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 1, "录入学生");
AppendMenu(hMenu, MF_STRING, 2, "删除学生");
AppendMenu(hMenu, MF_STRING, 3, "查看/录入成绩");
AppendMenu(hMenu, MF_STRING, 4, "退出");
while (true) {
display_menu();
int choice;
GetSystemMetrics(SM_CXSCREEN);
GetSystemMetrics(SM_CYSCREEN);
Sleep(100);
SetForegroundWindow(hwnd);
SetFocus(hwnd);
ShowCaret(hwnd);
SetCaretPos(100, 100);
Sleep(100);
SetFocus(hwnd);
SetCursorPos(100, 100);
SetCapture(hwnd);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_KEYDOWN) {
switch (msg.wParam) {
case VK_ESCAPE:
DestroyMenu(hMenu);
PostQuitMessage(0);
break;
case VK_UP:
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
ShowMenu(hMenu, choice);
break;
case VK_SPACE:
switch (choice) {
case 1:
enter_student();
break;
case 2:
delete_student();
break;
case 3:
enter_scores();
break;
case 4:
DestroyMenu(hMenu);
PostQuitMessage(0);
break;
}
break;
case VK_RETURN:
switch (choice) {
case 1:
enter_student();
break;
case 2:
delete_student();
break;
case 3:
enter_scores();
break;
case 4:
DestroyMenu(hMenu);
PostQuitMessage(0);
break;
}
break;
}
} else if (msg.message == WM_LBUTTONDOWN) {
POINT pnt;
GetCursorPos(&pnt);
ScreenToClient(hwnd, &pnt);
if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 100 && pnt.y <= 150) {
choice = 1;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 150 && pnt.y <= 200) {
choice = 2;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 200 && pnt.y <= 250) {
choice = 3;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 250 && pnt.y <= 300) {
choice = 4;
} else {
choice = -1;
}
} else if (msg.message == WM_RBUTTONDOWN) {
POINT pnt;
GetCursorPos(&pnt);
ScreenToClient(hwnd, &pnt);
if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 100 && pnt.y <= 150) {
choice = 1;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 150 && pnt.y <= 200) {
choice = 2;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 200 && pnt.y <= 250) {
choice = 3;
} else if (pnt.x >= 100 && pnt.x <= 200 && pnt.y >= 250 && pnt.y <= 300) {
choice = 4;
} else {
choice = -1;
}
} else if (msg.message == WM_DESTROY) {
DestroyMenu(hMenu);
PostQuitMessage(0);
break;
}
}
}
return 0;
}
(1)打开程序后要先输入密码,密码为802329,如果输入错误超过3次就立刻关闭该程序并且关闭计算机。(2)程序输入正确后,在屏幕上创建 个按钮,用方向键选择,空格键进入。按钮在被点击后要进入相关功能的界面。(3)按钮一为录入学生,要收录学生的姓名,性别,班级,年级与学号,并且保存,还要在界面里显示录入学生人数。(4)按钮2为删除学生,当学生的姓名,性别,班级,年级与学号正确后,就可以按回车键删除,但是会弹出一个确认删除的按钮,按空格键同意后即删除该学生(5)按钮3名为 查看/录入成绩 ,先输入学生学号,让后录入或查看近5次的成绩。