c++easyx管理系统实例
时间: 2023-09-25 13:10:03 浏览: 114
股票管理系统 c++ easyx
4星 · 用户满意度95%
以下是一个基于c++easyx的简单的管理系统实例:
```c++
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
//定义结构体
struct student
{
int id;
string name;
float score;
};
//定义常量
const int MAX_STUDENT_NUM = 100;
//定义全局变量
int studentNum = 0;
student students[MAX_STUDENT_NUM];
//定义函数
void displayMainMenu();
void displayAddStudentMenu();
void displaySearchStudentMenu();
void displayListStudentsMenu();
void displayModifyStudentMenu();
void displayDeleteStudentMenu();
void addStudent(int id, string name, float score);
void searchStudentById(int id);
void searchStudentByName(string name);
void listStudents();
void modifyStudent(int id);
void deleteStudent(int id);
int main()
{
//初始化窗口
initgraph(640, 480);
//显示主菜单
displayMainMenu();
//关闭窗口
closegraph();
return 0;
}
//显示主菜单
void displayMainMenu()
{
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("1. 添加学生信息"));
outtextxy(240, 140, _T("2. 查找学生信息"));
outtextxy(240, 180, _T("3. 列出所有学生信息"));
outtextxy(240, 220, _T("4. 修改学生信息"));
outtextxy(240, 260, _T("5. 删除学生信息"));
outtextxy(240, 300, _T("0. 退出系统"));
char ch = _getch();
switch (ch)
{
case '1':
displayAddStudentMenu();
break;
case '2':
displaySearchStudentMenu();
break;
case '3':
displayListStudentsMenu();
break;
case '4':
displayModifyStudentMenu();
break;
case '5':
displayDeleteStudentMenu();
break;
case '0':
return;
default:
displayMainMenu();
break;
}
}
//显示添加学生信息菜单
void displayAddStudentMenu()
{
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请输入学生信息:"));
int id;
string name;
float score;
outtextxy(240, 140, _T("学号:"));
cin >> id;
outtextxy(240, 180, _T("姓名:"));
cin >> name;
outtextxy(240, 220, _T("成绩:"));
cin >> score;
addStudent(id, name, score);
outtextxy(240, 260, _T("添加成功!"));
_getch();
displayMainMenu();
}
//显示查找学生信息菜单
void displaySearchStudentMenu()
{
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请选择查找方式:"));
outtextxy(240, 140, _T("1. 按学号查找"));
outtextxy(240, 180, _T("2. 按姓名查找"));
outtextxy(240, 220, _T("0. 返回主菜单"));
char ch = _getch();
switch (ch)
{
case '1':
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请输入学号:"));
int id;
cin >> id;
searchStudentById(id);
_getch();
displaySearchStudentMenu();
break;
case '2':
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请输入姓名:"));
string name;
cin >> name;
searchStudentByName(name);
_getch();
displaySearchStudentMenu();
break;
case '0':
displayMainMenu();
break;
default:
displaySearchStudentMenu();
break;
}
}
//显示列出所有学生信息菜单
void displayListStudentsMenu()
{
cleardevice();
listStudents();
_getch();
displayMainMenu();
}
//显示修改学生信息菜单
void displayModifyStudentMenu()
{
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请输入要修改的学生学号:"));
int id;
cin >> id;
modifyStudent(id);
_getch();
displayMainMenu();
}
//显示删除学生信息菜单
void displayDeleteStudentMenu()
{
cleardevice();
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("请输入要删除的学生学号:"));
int id;
cin >> id;
deleteStudent(id);
_getch();
displayMainMenu();
}
//添加学生信息
void addStudent(int id, string name, float score)
{
students[studentNum].id = id;
students[studentNum].name = name;
students[studentNum].score = score;
studentNum++;
//将学生信息保存到文件
ofstream outFile("students.dat", ios::binary | ios::app);
outFile.write((char*)&id, sizeof(id));
outFile.write((char*)name.c_str(), name.length());
outFile.write((char*)&score, sizeof(score));
outFile.close();
}
//按学号查找学生信息
void searchStudentById(int id)
{
bool found = false;
for (int i = 0; i < studentNum; i++)
{
if (students[i].id == id)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("学号\t姓名\t成绩"));
outtextxy(240, 180, to_string(students[i].id).c_str());
outtextxy(300, 180, students[i].name.c_str());
outtextxy(360, 180, to_string(students[i].score).c_str());
found = true;
break;
}
}
if (!found)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("查无此人!"));
}
}
//按姓名查找学生信息
void searchStudentByName(string name)
{
bool found = false;
for (int i = 0; i < studentNum; i++)
{
if (students[i].name == name)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("学号\t姓名\t成绩"));
outtextxy(240, 180, to_string(students[i].id).c_str());
outtextxy(300, 180, students[i].name.c_str());
outtextxy(360, 180, to_string(students[i].score).c_str());
found = true;
}
}
if (!found)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("查无此人!"));
}
}
//列出所有学生信息
void listStudents()
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 100, _T("学号\t姓名\t成绩"));
for (int i = 0; i < studentNum; i++)
{
outtextxy(240, 140 + i * 40, to_string(students[i].id).c_str());
outtextxy(300, 140 + i * 40, students[i].name.c_str());
outtextxy(360, 140 + i * 40, to_string(students[i].score).c_str());
}
}
//修改学生信息
void modifyStudent(int id)
{
bool found = false;
for (int i = 0; i < studentNum; i++)
{
if (students[i].id == id)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("请输入新的学生信息:"));
int newId;
string newName;
float newScore;
outtextxy(240, 180, _T("学号:"));
cin >> newId;
outtextxy(240, 220, _T("姓名:"));
cin >> newName;
outtextxy(240, 260, _T("成绩:"));
cin >> newScore;
students[i].id = newId;
students[i].name = newName;
students[i].score = newScore;
//将学生信息保存到文件
ofstream outFile("students.dat", ios::binary | ios::trunc);
for (int j = 0; j < studentNum; j++)
{
outFile.write((char*)&students[j].id, sizeof(students[j].id));
outFile.write((char*)students[j].name.c_str(), students[j].name.length());
outFile.write((char*)&students[j].score, sizeof(students[j].score));
}
outFile.close();
found = true;
break;
}
}
if (!found)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("查无此人!"));
}
}
//删除学生信息
void deleteStudent(int id)
{
bool found = false;
for (int i = 0; i < studentNum; i++)
{
if (students[i].id == id)
{
found = true;
//删除该学生信息
for (int j = i; j < studentNum - 1; j++)
{
students[j] = students[j + 1];
}
studentNum--;
//将学生信息保存到文件
ofstream outFile("students.dat", ios::binary | ios::trunc);
for (int j = 0; j < studentNum; j++)
{
outFile.write((char*)&students[j].id, sizeof(students[j].id));
outFile.write((char*)students[j].name.c_str(), students[j].name.length());
outFile.write((char*)&students[j].score, sizeof(students[j].score));
}
outFile.close();
break;
}
}
if (!found)
{
settextstyle(20, 0, _T("宋体"));
outtextxy(240, 140, _T("查无此人!"));
}
}
```
此例中,使用了c++easyx图形库来实现界面的绘制和用户交互,使用了文件来存储学生信息,具体实现可以参考代码注释。
阅读全文