若想查看C语言版,请移步主页查看《C语言版-学生考勤系统 不能运行私信骂我》
有问题私信问我,也可以查看C语言的相关代码注释,可以帮助你更好理解C++版
如是有别的需求改动,可直接私信我帮你解决!
#include <iostream>
#include <string>
struct Student
{
std::string name;
int age;
int num;
int c;
float date;
std::string Attandance_Result;
};
void PrintfStudentFunction();
int numCount = 0;
int count = 0;
int Num()
{
int num;
std::cout << "请输入要录入学生的人数" << std::endl;
std::cin >> num;
return num;
}
Student* Input(Student* pArr, int len)
{
count = len;
pArr = new Student[len];
for (int i = 0; i < len; i++)
{
numCount++;
std::cout << "请输入第" << i + 1 << "个学生的名字:" << std::endl;
std::cin >> pArr[i].name;
std::cout << "请输入第" << i + 1 << "个学生的年龄" << std::endl;
std::cin >> pArr[i].age;
std::cout << "请输入第" << i + 1 << "个学生的学号" << std::endl;
std::cin >> pArr[i].num;
std::cout << "请输入第" << i + 1 << "个学生的班级" << std::endl;
std::cin >> pArr[i].c;
std::cout << "请输入第" << i + 1 << "个学生的打卡时间(xx.xx)" << std::endl;
std::cin >> pArr[i].date;
std::cout << "请输入第" << i + 1 << "个学生的考勤结果(出勤 旷课 事假 病假 迟到 早退)" << std::endl;
std::cin >> pArr[i].Attandance_Result;
}
std::cout << "学生信息录入完毕!!!!!!!" << std::endl;
PrintfStudentFunction();
return pArr;
}
void Output(Student* pArr)
{
std::cout << "姓名\t年龄\t学号\t班级\t打卡时间 出勤结果" << std::endl;
for (int i = 0; i < numCount; i++)
{
std::cout << pArr[i].name << "\t" << pArr[i].age << "\t" << pArr[i].num << "\t" << pArr[i].c
<< "\t" << pArr[i].date << "\t " << pArr[i].Attandance_Result << std::endl;
}
PrintfStudentFunction();
}
void Delete(Student* pArr)
{
int num;
std::cout << "请输入你要删除学生的学号" << std::endl;
std::cin >> num;
int count = 0; // 记录匹配学号的学生数量
int* indices = new int[numCount]; // 用于存储匹配学号的学生索引
for (int i = 0; i < numCount; i++)
{
if (pArr[i].num == num)
{
indices[count] = i;
count++;
}
}
if (count > 0)
{
for (int i = 0; i < count; i++)
{
int index = indices[i];
for (int j = index; j < numCount - 1; j++)
{
pArr[j] = pArr[j + 1];
}
}
numCount -= count;
Output(pArr);
}
else
{
std::cout << "未找到匹配学号的学生" << std::endl;
}
delete[] indices; // 释放动态数组内存
}
void Search(Student* pArr)
{
std::cout << "请输入要查找学生的名字" << std::endl;
std::string name;
std::cin >> name;
std::cout << "姓名\t年龄\t学号\t班级\t打卡时间 出勤结果" << std::endl;
for (int i = 0; i < numCount; i++)
{
if (pArr[i].name == name)
{
std::cout << pArr[i].name << "\t" << pArr[i].age << "\t" << pArr[i].num << "\t"
<< pArr[i].c << "\t" << pArr[i].date << "\t " << pArr[i].Attandance_Result << std::endl;
}
}
PrintfStudentFunction();
}
void Change(Student* pArr)
{
std::string name;
std::cout << "请输入你要修改学生的名字:" << std::endl;
std::cin >> name;
int num = 0;
for (int i = 0; i < numCount; i++)
{
if (pArr[i].name == name)
{
num = i;
break;
}
}
if (num == numCount)
{
std::cout << "对不起没有要修改的学生信息" << std::endl;
}
else
{
std::cout << "请输入修改后的学生名字:" << std::endl;
std::cin >> pArr[num].name;
std::cout << "请输入学生的年龄:" << std::endl;
std::cin >> pArr[num].age;
std::cout << "请输入学生的学号:" << std::endl;
std::cin >> pArr[num].num;
std::cout << "请输入学生的班级:" << std::endl;
std::cin >> pArr[num].c;
std::cout << "请输入学生的打卡时间:" << std::endl;
std::cin >> pArr[num].date;
std::cout << "请输入学生的考勤结果(出勤 旷课 事假 病假 迟到 早退):" << std::endl;
std::cin >> pArr[num].Attandance_Result;
}
PrintfStudentFunction();
}
Student* Add(Student* pArr)
{
std::cout << "请输入要添加的人数" << std::endl;
int num;
std::cin >> num;
if (numCount + num > count)
{
Student* p = pArr;
pArr = new Student[numCount + num];
count = numCount + num;
for (int i = 0; i < numCount; i++)
{
pArr[i] = p[i];
}
}
for (int i = 0; i < num; i++)
{
std::cout << "请输入第" << i + 1 << "个添加学生的名字:" << std::endl;
std::cin >> pArr[numCount].name;
std::cout << "请输入第" << i + 1 << "个添加学生的年龄:" << std::endl;
std::cin >> pArr[numCount].age;
std::cout << "请输入第" << i + 1 << "个添加学生的学号:" << std::endl;
std::cin >> pArr[numCount].num;
std::cout << "请输入第" << i + 1 << "个添加学生的班级:" << std::endl;
std::cin >> pArr[numCount].c;
std::cout << "请输入第" << i + 1 << "个添加学生的打卡时间:" << std::endl;
std::cin >> pArr[numCount].date;
std::cout << "请输入第" << i + 1 << "个添加学生的考勤结果(出勤 旷课 事假 病假 迟到 早退):" << std::endl;
std::cin >> pArr[numCount].Attandance_Result;
numCount++;
}
std::cout << "数据添加成功" << std::endl;
PrintfStudentFunction();
return pArr;
}
void Order(Student* pArr)
{
int op;
std::cout << "<1>学号升序 <2>姓名升序" << std::endl;
std::cin >> op;
if(op == 1)
{
for(int i = 0; i < numCount - 1; i++)
{
for(int j = 0; j < numCount - i - 1; j++)
{
if(pArr[j].num > pArr[j+1].num)
{
Student temp = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = temp;
}
}
}
}
else if(op == 2)
{
for(int i = 0; i < numCount - 1; i++)
{
for(int j = 0; j < numCount - i - 1; j++)
{
if(pArr[j].name > pArr[j+1].name)
{
Student temp = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = temp;
}
}
}
}
Output(pArr);
}
void PrintfStudentFunction()
{
std::cout << "-------------------------------------------------" << std::endl;
std::cout << "-----------------学生考勤管理系统----------------" << std::endl;
std::cout << "-----------------1.录入学生信息------------------" << std::endl;
std::cout << "-----------------2.输出学生信息------------------" << std::endl;
std::cout << "-----------------3.删除学生信息------------------" << std::endl;
std::cout << "-----------------4.修改学生信息------------------" << std::endl;
std::cout << "-----------------5.查询学生信息------------------" << std::endl;
std::cout << "-----------------6.添加学生信息------------------" << std::endl;
std::cout << "-----------------7.排序(学号、姓名)------------" << std::endl;
std::cout << "-----------------8.退出系统----------------------" << std::endl;
std::cout << "-------------------------------------------------" << std::endl;
}
int main()
{
// 修改为
Student* pArr = 0;
PrintfStudentFunction();
while (true)
{
std::cout << "请选择操作的命令:" << std::endl;
int item, num;
std::cin >> item;
switch (item)
{
case 1:
num = Num();
pArr = Input(pArr, num);
break;
case 2:
Output(pArr);
break;
case 3:
Delete(pArr);
break;
case 4:
Change(pArr);
break;
case 5:
Search(pArr);
break;
case 6:
pArr = Add(pArr);
break;
case 7:
Order(pArr);
break;
case 8:
delete[] pArr;
return 0;
default:
std::cout << "用户输入错误,请重新输入" << std::endl;
break;
}
}
return 0;
}