|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#endif[/code]
- #include "List.h"
- #include <iostream>
- using namespace std;
- Node::Node(char* _name,int _id,float _score)
- {
- name = _name;
- id = _id;
- score = _score;
- next = NULL;
- }
- Node::~Node()
- {
- cout << "使用~Node析构函数" << endl;
- }
- void Node::setScore(float _score)
- {
- score = _score;
- }
- char* Node::getName()
- {
- return name;
- }
- int Node::getId()
- {
- return id;
- }
- float Node::getScore()
- {
- return score;
- }
- Node* Node::getNext()
- {
- return next;
- }
- void Node::setNext(Node *_node)
- {
- next = _node; //将一个新的节点插入到链表尾,配合LinkList::addData()使用
- }
- LinkList::LinkList()
- {
- head = NULL;
- }
- LinkList::LinkList(char* _name,int _id,float _score)
- {
- head =new Node(_name,_id,_score); //设置新节点的值,并且将头指针head指向新添加的节点
- }
- void LinkList::addDate(char* _name,int _id,float _score)
- {
- Node* p = head;//初始化一个Node型指针指向头指针head
- if(head==NULL)//判断head是否为空,即链表是否为空链表
- {
- head = new Node(_name,_id,_score);//若链表为空,创建新节点,并将头指针head指向新的节点
- }
- else//链表不为空
- {
- while(p->getNext()!=NULL) //遍历链表直至尾节点
- {
- p = p->getNext();
- }
- p->setNext(new Node(_name,_id,_score));//在尾节点后添加新的节点
- }
- }
- LinkList::~LinkList()
- {
- cout << "使用~LinkList析构函数" << endl;
- }
- void LinkList::display()//显示函数
- {
- ofstream out;
- out.open("Student.txt");
- if(!out)
- {
- cout << "文件打开失败" << endl;
- }
- else
- {
- Node* p = head;
- if(p==NULL)//链表为空的情况(我发现的一个大大的BUG,嘻嘻!)
- {
- out << "空链表!" << endl;
- }
- else
- {
- { //输出头节点
- out << "学号 姓名 成绩" << endl;
- out << p->getId() << p->getName() << p->getScore() << endl;
- }
- if(p->getNext()!=NULL)//判断下一节点是否为空,并依次输出
- {
- p = p->getNext();//节点后移
- out << p->getId() << p->getName() << p->getScore() << endl;
-
- }
- }
- }
-
- }
- void LinkList::serch(int n)//查找函数,通过学号查找
- {
- Node* p = head;//head;获取头指针就会内存报错
- if(p == NULL)
- {
- cout << "数据为空,查询失败!" << endl;
- }
- else
- {
- if(p->getId()==n)
- {
- cout << p->getId() << p->getName() <<p->getScore() << endl;
- }
- else
- {
- if(p->getNext()==NULL)
- {
- cout << "查询失败,不存在该学生!" << endl;
- }
- else
- {
- if(p->getId()==n)
- {
- cout << p->getId() << p->getName() <<p->getScore() << endl;
- }
- p = p->getNext();
-
- }
- }
- }
- }
- void LinkList::serch(char* n)//查找函数,通过姓名查找
- {
- Node* p = head;//获取头指针就会内存报错
- if(p == NULL)
- {
- cout << "数据为空,查询失败!" << endl;
- }
- else
- {
- if(p->getName()==n)
- {
- cout << p->getId() << p->getName() <<p->getScore() << endl;
- }
- else
- {
- if(p->getNext()==NULL)
- {
- cout << "查询失败,不存在该学生!" << endl;
- }
- else
- {
- if(p->getName()==n)
- {
- cout << p->getId() << p->getName() <<p->getScore() << endl;
- }
- p = p->getNext();
-
- }
- }
- }
- }
- void LinkList::changScore()//分数修改函数
- {
- Node* p = head;//获取头指针就会内存报错
- char admin[6] ="admin";
- int password = 123456;
- char admin1[6] = {0};
- int password1 = 0;
- cout << "温馨提示,你正在修改数据,请谨慎操作!" << endl;
- cout << "本系统具有超强防护功能,需要验明真身,以供下步操作!" << endl;
- cout << "请按程序提示操作!";
- cout << "请输入用户名" << endl;
- cin >> admin1;
- if(admin==admin1)
- {
- cout << "请输入密码" << endl;
- cin >> password1;
- if(password == password1)
- {
- cout << "欢迎使用成绩修改系统" << endl;
- cout << "请输入需要修改的学生学号" << endl;
- cin >> password1;
- if(p == NULL)
- {
- cout << "非常抱歉,当前数据为空,无法提供更改,请确保数据正常!" << endl;
- }
- else
- {
- if(p->getId()==password1)
- {
- p->setScore(password1);
- }
- else
- {
- while(p->getNext()!=NULL)
- {
- p = p->getNext();
- if(p->getId()==password1)
- {
- p->setScore(password1);
- }
-
- }
- }
- }
- }
- }
- else
- {
- cout << "用户名输入错误" << endl;
- cout << "本系统具有超强防护功能,为防止暴力破解!请重启后重试!" << endl;
- }
- }
- void LinkList::passAndGood()
- {
- Node* q = head;//获取头指针就会内存报错
- if(head==NULL)
- {
- cout << "数据为空,无法提供统计功能!" << endl;
- }
- else
- cout << "及格人数如下:" << endl;
- {
- if(q->getScore()>=60)
- {
- cout << q->getId() << q->getName() << q->getScore();
- if(q->getScore()>=90)
- {
- cout << "优秀" << endl;
- }
- }
- while(q->getNext()!=NULL)
- {
- q = q->getNext();
- if(q->getScore()>=60)
- {
- cout << q->getId() << q->getName() << q->getScore() << endl;
- if(q->getScore()>=90)
- {
- cout << "优秀" << endl;
- }
- }
- }
- }
- }
- void LinkList::redData()
- {
- char name[10];
- float score;
- ifstream in;
- in.open("Data.txt");
- if(!in)
- {
- cout << "文件打开失败" << endl;
- }
- else
- {
- for (int i = 0;i < 5;i++)
- {
- in >> name >> score;
- head = new Node(name,i+1,score);
- }
- }
- in.close();
- }
复制代码
运行错误,求帮助! |
|