这个程序我用的方法是动态链表。我打了那个count函数,以学号查找对应学生信息,然后求出总分和平均分,查找的方法是和上一个del函数相同的,指针遍历。但是在运行过程中del函数的使用是正常的,count函数一输入学号敲了回车就引发了异常,异常是读取访问权限冲突。p1 是 nullptr。我不懂是什么意思,也不知道怎么解决,上csdn搜索了一些解决方法都不适用,希望有朋友可以为我解惑,感激不尽!
#include <iostream>
#include <iomanip>
using namespace std;
#define NULL 0
int n;
struct Student
{
int number;
char name[20];
char sex;
int age;
int lesson;
int escore;
int mscore;
int hscore;
Student* Next;
};
void printmenu()
{
cout << setw(77) << "|*********学生成绩管理系统*********|" << endl;
cout << setw(77) << "|----------------------------------|" << endl;
cout << setw(77) << "| 主菜单项 |" << endl;
cout << setw(77) << "|----------------------------------|" << endl;
cout << setw(77) << "| 1-- 录入学生信息 |" << endl;
cout << setw(77) << "| 2-- 编辑学生信息 |" << endl;
cout << setw(77) << "| 3-- 查询学生信息 |" << endl;
cout << setw(77) << "| 4-- 统计学生信息 |" << endl;
cout << setw(77) << "| 5-- 显示学生信息 |" << endl;
cout << setw(77) << "| 6-- 删除学生信息 |" << endl;
cout << setw(77) << "| 0-- 退出学生信息 |" << endl;
cout << setw(53) << "请输入选项(0-6):";
}
Student *input(void)
{
Student* p1, *p2, *head;
p1 = p2 = new Student;
n = 0;
cout << "请输入学生学号:" << endl;
cin >> p1->number;
cout << "请输入学生姓名:" << endl;
cin >> p1->name;
cout << "请输入学生性别:" << endl;
cin >> p1->sex;
cout << "请输入学生年龄:" << endl;
cin >> p1->age;
cout << "请输入学生班级:" << endl;
cin >> p1->lesson;
cout << "请输入学生英语成绩:" << endl;
cin >> p1->escore;
cout << "请输入学生数学成绩:" << endl;
cin >> p1->mscore;
cout << "请输入学生高数成绩:" << endl;
cin >> p1->hscore;
head = NULL;
while (p1->number != 0)
{
cout << "添加成功!" << endl;
cout << endl;
n = n + 1;
if (n == 1)head = p1;
else p2->Next = p1;
p2 = p1;
p1 = new Student;
cout << "请输入学生学号:" << endl;
cin >> p1->number;
if (p1->number == 0)
{
cout << "录入结束!" << endl;
break;
}
cout << "请输入学生姓名:" << endl;
cin >> p1->name;
cout << "请输入学生性别:" << endl;
cin >> p1->sex;
cout << "请输入学生年龄:" << endl;
cin >> p1->age;
cout << "请输入学生班级:" << endl;
cin >> p1->lesson;
cout << "请输入学生英语成绩:" << endl;
cin >> p1->escore;
cout << "请输入学生数学成绩:" << endl;
cin >> p1->mscore;
cout << "请输入学生高数成绩:" << endl;
cin >> p1->hscore;
}
p2->Next = NULL;
return(head);
}
void print(Student *head)
{
Student* p;
p = head;
if (head != NULL)
{
cout << "学号" << ' ' << "姓名" << ' ' << "性别" << ' ' << "年龄" << ' ' << "班级" << ' ' << "英语成绩" << ' ' << "数学成绩" << ' ' << "高数成绩" << endl;
while (p != NULL)
{
cout << p->number << ' ' << p->name << ' ' << p->sex << ' ' << p->age << ' ' << p->lesson << ' ' << p->escore << ' ' << p->mscore << ' ' << p->hscore << endl;
p = p->Next;
}
}
else cout << "无记录!" << endl;
}
Student* del(Student* head, int num)
{
Student* p1, *p2=0;
if (head == NULL)
{
cout << "无记录!" << endl;
return(head);
}
p1 = head;
while (num != p1->number && p1->Next != NULL)
{
p2 = p1;
p1 = p1->Next;
}
if (num == p1->number)
{
if (p1 == head)
{
head = p1->Next;
}
else p2->Next = p1->Next;
cout << "删除成功!" << endl;
}
else cout << "找不到该学号!" << endl;
return(head);
}
void count(Student *head,int num)
{
Student* p1=head, * p2=0;
if (head == NULL)
{
cout << "无记录!" << endl;
}
while (p1->Next!=NULL&&num != p1->number)
{
p2 = p1;
p1 = p1->Next;
}
if (p1->number == num)
{
cout << "该学生总分为:" << p1->escore + p1->mscore + p1->hscore << endl;
cout << "该学生平均分为:" << (p1->escore + p1->mscore + p1->hscore) / 3 << endl;
}
else cout << "查无此人!" << endl;
}
int main()
{
int a,b,num;
Student* head=0,*newhead=NULL;
while (1)
{
printmenu();
cin >> a;
switch (a)
{
case 0:
{
system("cls");
cout << "感谢使用本程序!" << endl;
return 0;
}break;
case 1:
{
system("cls");
cout << "请录入学生信息!(若录入完成请在输入学号时输入0)" << endl;
head=input();
system("pause");
system("cls");
}break;
case 5:
{
system("cls");
if (newhead != head)
{
print(newhead);
}
else print(head);
system("pause");
system("cls");
}break;
case 6:
{
system("cls");
cout << "请输入需要删除学生信息的学号" << endl;
cin >> num;
if (newhead != head)
{
newhead= del(newhead, num);
}
else del(head, num);
system("pause");
system("cls");
}break;
case 4:
{
system("cls");
cout << "请输入要统计的学生的学号" << endl;
cin >> num;
if (newhead != head)
{
count(newhead, num);
}
else count(head, num);
system("pause");
system("cls");
}
}
}
}
```