#include<iostream>
#include<string>
using namespace std;
struct Student {
string m_Name;//学生姓名
string m_Number;//学生学号
int m_Sex;//学生性别 0男1女
int m_Age;//学生年龄
string m_Class;//学生班级
Student* lnext;//指针 指向下一个学生
Student()
{
m_Sex = 0;
m_Age = 0;
lnext = NULL;
};
Student(string name, string stunumber, int sex, int age, string clss, Student* lnext = NULL) {//学生类含参构造
this->m_Name = name;
this->m_Number = stunumber;
this->m_Sex = sex;
this->m_Age = age;
this->m_Class = clss;
this->lnext = lnext;
}
};
//学生链表头节点
Student* First;
//按学生学号进行查询 不用这个 直接调用下面的Searchstunum()函数
void SearchByStuNum(string number, bool& isfind) {
Student* p = First;
while (p != NULL) {
if (p->m_Number == number) {
isfind = true;
cout << " -----------------------------------------------------------------------------------------" << endl;
cout << " 姓名:" << p->m_Name << "\t学号:" << p->m_Number << "\t性别:";
if (p->m_Sex == 0) cout << "男";
if (p->m_Sex == 1) cout << "女";
cout << "\t年龄:" << p->m_Age << "\t班级:" << p->m_Class ;
}
p = p->lnext;
}
}
//按学生学号进行查询 不用这个 直接调用下面的Searchstunum()函数
void SearchByStuNum1(string number, bool& isfind) {
Student* p = First;
while (p != NULL) {
if (p->m_Number == number) {
isfind = true;
cout << " ----------------------------------------------------------------------" << endl;
cout << " 姓名:" << p->m_Name << "\t学号:" << p->m_Number << "\t年龄:" << p->m_Age << "\t性别:";
if (p->m_Sex == 0) { cout << "男" << endl; }
else cout << "女" << endl;
cout << " ----------------------------------------------------------------------" << endl;
}
p = p->lnext;
}
}
//通过学生学号查找
void SearchStuNum1(string num) {
bool isfind = false;
cout << " ------------------------------------------------" << endl;
cout << " | 查询结果 |" << endl;
cout << " ------------------------------------------------" << endl;
SearchByStuNum1(num, isfind);
if (!isfind) cout << "未找到此人";
}
//判断学号唯一性
bool judgenumber(string num)
{
bool exsit = false;
Student* r = First;
while (r != NULL)
{
if (r->m_Number == num)
{
return exsit = true;
}
r = r->lnext;
}
return exsit;
}
//添加学生函数
void AddStudent(string name, string stunumber, int sex, int age, string clss) {
Student* p = new Student(name, stunumber, sex, age, clss);
if (First != NULL) {
Student* q = First;
while (q->lnext != NULL)
{
q = q->lnext;
}
q->lnext = p;
}
if (First == NULL) {
First = p;
}
}
//添加学生菜单
void addperson()
{
system("cls");
bool isfind = false;
cout << endl << endl;
cout << " -------------------------------" << endl;
cout << " | 请输入添加信息 |" << endl;
cout << " -------------------------------" << endl;
cout << " 请输入学生姓名:";
string name;
cin >> name;
cout << " 学生学号:";
string number;
cin >> number;
cout << " 学生性别(0是男,1是女):";
int sex;
cin >> sex;
cout << " 学生年龄:";
int age;
cin >> age;
cout << " 学生班级:";
string clss;
cin >> clss;
if (judgenumber(number))
{
cout << " 该学号已存在!请重新输入!" << endl;
return;
}
else
{
AddStudent(name, number, sex, age, clss);
cout << " 添加成功!" << endl;
}
}
//显示所有记录
void ShowAll() {
Student* p = First;
cout << endl;
cout << " *****************************************************************************************" << endl;
cout << " * 所有学生信息 *" << endl;
cout << " *****************************************************************************************" << endl;
while (p != NULL) {
cout << " -----------------------------------------------------------------------------------------" << endl;
cout << " 姓名:" << p->m_Name << "\t学号:" << p->m_Number << "\t性别:";
if (p->m_Sex == 0) cout << "男";
if (p->m_Sex == 1) cout << "女";
cout << "\t年龄:" << p->m_Age << "\t班级:" << p->m_Class ;
cout << endl;
p = p->lnext;
}
cout << " -----------------------------------------------------------------------------------------" << endl;
}
//定位函数,找他前一位
void location(string num, Student*& p)
{
Student* r = First;
while (r != NULL)
{
if (r->lnext->m_Number == num)
{
p = r;
return;
}
r = r->lnext;
}
}
//删除学生记录
void deletes()
{
system("cls");
cout << endl << endl;
cout << " -------------------------------" << endl;
cout << " | 请输入删除学生信息 |" << endl;
cout << " -------------------------------" << endl;
cout << " 请输入学生学号: ";
string id;
cin >> id;
bool isfind = false;
SearchByStuNum(id, isfind);
cout << endl;
if (isfind)
{
cout << " 请问是否删除?(yes or no)";
string ok;
cin >> ok;
if (ok == "yes")
{
Student* q = NULL;
location(id, q);
Student* p = q->lnext;
q->lnext = p->lnext;
cout << " 删除成功!" << endl;
}
else
{
return;
}
}
else
{
cout << " 记录为空!请重新输入!";
return;
}
}
//菜单界面
void menu()
{
cout << endl << endl;
cout << " **************************************************************" << endl;
cout << " * ----------------------------------------- *" << endl;
cout << " * | 欢迎使用学生考勤系统! | *" << endl;
cout << " * ------------------------------------------ *" << endl;
cout << " * | 1、添加学生记录 | *" << endl;
cout << " * | 2、查询信息 | *" << endl;
cout << " * | 3、显示所有记录 | *" << endl;
cout << " * | 4、删除记录 | *" << endl;
cout << " * ------------------------------------------ *" << endl;
cout << " **************************************************************" << endl;
cout << " 请选择:";
}
int main()
{
while (1)
{
system("cls");
menu();
int option;
cin >> option;
switch (option)
{
case 1://添加学生
addperson();
system("pause");
break;
case 2://查询信息
{
system("cls");
cout << " ---------------------------------------" << endl;
cout << " ||请输入被查询学生的学号:";
string num2;
cin >> num2;
cout << " ---------------------------------------" << endl;
SearchStuNum1(num2);
system("pause");
break;
}
case 3://显示所有记录
system("cls");
ShowAll();
system("pause");
break;
case 4://删除学生记录
deletes();
break;
default:
cout << "输入错误请重新输入!" << endl;
system("pause");//为了显示输出,停顿一下
cin.clear();//一个对异常输入判断的东西
cin.ignore(1024, '\n');
break;
}
}
}
this指针怎么改