C++链式学生管理系统(详细解析)

step1:创建节点

在头文件node.h中:

node.h

#pragma once
class Node
{
public:
	Node();
	Node(int data);
	Node(int data, Node* next);
	int& getdata();
	Node*& getNext();
protected:
	int data;
	Node* next;
};

然后就是一个快捷操作!!!

选中一个函数,右击“快速操作和重构”->再点击“创建定义与声明”就会有一个cpp产生,里面函数的定义框架就会自动搭好了。(很关键的一点,你复制粘贴板不能含有函数名相关的内容,否则会创建失败)

此后会有一个node.cpp文件(填充一下函数内容即可)

node.cpp

#include "node.h"

Node::Node()
{
	this->next = nullptr;
}

Node::Node(int data)
{
	this->data = data;
	this->next = nullptr;
}

Node::Node(int data, Node* next)
{
	this->data = data;
	this->next = next;
}

int& Node::getdata()
{
	return data;
}

Node*& Node::getNext()
{
	return next;
}

step2:创建链表

和上述一样的操作,在head文件中list.head写好函数原型,然后在.cpp中写好即可

list.cpp中

(注意:在新建一个头文件需要用到其他头文件的类class Node,尽量写声明操作

过程中出了:“不允许指针指向不完整的类类型”这样的问题,具体原因是个人在list.cpp中没有包含

“node.h”的头文件。(其余会引发的原因见收藏夹c++)

list.h

#pragma once
class Node;//声明操作!
//有头链表:头节点不需要存储数据
//无头链表:头节点存储数据(本处写的无头)

class list
{
public:
	list();
	void printlist();
	void insert(int data);
	void deleteData(int posdata);
	Node* search(int posdata);
protected:
	Node * headNode;
	int curSize;//当前
};

list.cpp 

#include "list2.h"
#include"node.h"
#include<iostream>
using namespace std;

list::list()
{
	this->headNode = nullptr;
	this->curSize = 0;//创建链表
}

void list::printlist()
{
	Node* pMove = headNode;
	for ( ; pMove != nullptr;)
	{
		cout << pMove->getdata() << " ";
		pMove = pMove->getNext();//++
	}
}

void list::insert(int data)
{
	headNode = new Node(data, headNode);//只需一句话即可创建并插入。
	this->curSize++;
}

void list::deleteData(int posdata)
{
	Node* preNode=headNode;
	Node* behNode = headNode;
	while (preNode && preNode->getdata() != posdata)
	{
		behNode = preNode;
		preNode = preNode->getNext();
	}
	if (!preNode)//如果没找到preNode就是空的
	{
		cout << "链表中不存在这样的data" << endl;
	}
	else
	{
		if (preNode == behNode)//说明需要删除的是头节点,需要单独去考虑
		{
			preNode = preNode->getNext();
			delete headNode;
			headNode = preNode;
			delete behNode;
			this->curSize--;
		}
		else//说明正常找到了 preNode指向的就是需要找的(非头结点)
		{
			behNode->getNext() = preNode->getNext();
			delete preNode;
			preNode = nullptr;//别忘记置为空。
			this->curSize--;
		}
	}
}

Node* list::search(int posdata)
{
	Node* pMove = new Node;
	for (pMove = headNode; pMove&&pMove->getdata() !=posdata ;)
	{
		pMove = pMove->getNext();
	}
	if (pMove != nullptr)
		return pMove;
	else
	{
		printf("未找到该节点");
		return headNode;
	}
}

解释一下:

        ①节点的插入(仅一句话):

        ②节点的删除: 

 step3:写好所管理数据的类型(此处以学生STU为例)

stuManagement.h

#pragma once
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
class STU
{
public:
	STU(string name, int age, string tel, string sex);
	void printSTU();
	void saveFile(fstream&file);
	void readFile(fstream&file);
protected:
	string name;
	int age;
	string tel;//电话
	string sex;//性别
};

stuManagement.cpp 

#include "Management.h"
#include "stuManagement.h"

STU::STU(string name, int age, string tel, string sex):name(name),age(age),tel(tel),sex(sex)
{
}

void STU::printSTU()
{
	cout << name << " " << age << " " << tel << " " << sex << endl;
}

void STU::saveFile(fstream&file)
{
	file<<name<<" "<< age << " " << tel << " " << sex << endl;
}

void STU::readFile(fstream&file)
{
	file >> name >> age >> tel >> sex;//注:在数据流进去的时候是无视空格的
}

step4:修改链表和Node中所有的int data类型(在学完模板后,就无须这一步操作,可以传任意类型的数据)

修改上述step1和step2的代码即可,一定要小心是否忘记包含了相关的头文件,也别重复包含。(#pragma once)

(过程中一直发现不了bug?解析出了问题,无法直接看提示信息去解决,没想到竟然是头文件list2.h写成了list.h 使得疯狂报错。。。)

 step5:封装system类

system.h

#pragma once
#include"list2.h"

class System
{
public:
	System();
	void MakeMeue();
	void KeyDown();
	void initSTU();
	list*& getlist();
protected:
	list* plist;
};

system.cpp

包含按键的封装,文件的保存操作

此前在stuMangement.cpp中增添文件保存的操作

void STU::saveFile(fstream&file)
{
	file<<name<<"\t"<< age << "\t" << tel << "\t" << sex << endl;
}

void STU::readFile(fstream&file)
{
	file >> name >> age >> tel >> sex;//注:在数据流进去的时候是无视空格的
}

system.cpp(确信 

#include "System.h"
#include"list2.h"
#include"node.h"
#include"stuManagement.h"
#include<iostream>
using namespace std;

System::System():plist(new list)//看好了,不传参,直接new出来。
{
    plist->ReadFileToList("1.txt");
}

void System::MakeMeue()
{
    cout << "-----------------【stu管理系统】-----------------" << endl;
    cout << "\t\t" << "0.退出系统" << endl;
    cout << "\t\t" << "1.插入信息" << endl;
    cout << "\t\t" << "2.删除信息" << endl;
    cout << "\t\t" << "3.查找信息" << endl;
    cout << "\t\t" << "4.修改信息" << endl;
    cout << "\t\t" << "5.显示信息" << endl;
}

void System::KeyDown()
{
    int userkey = 0;
    cin>>userkey;
    string name;
    Node* dest;
    switch (userkey)
    {
    case 0:
        cout << "退出成功" << endl;
        exit(0);
        break;
    case 1:
        this->initSTU();
        plist->SaveListToFile("1.txt");//同步到文件中
        break;
    case 2:
        cout << "请输入你需要删除的信息的名字" << endl;
        cin >> name;
        plist->deleteData(name);
        cout << "删除成功" << endl;
        plist->SaveListToFile("1.txt");
        break;
    case 3:
        cout << "请输入你需要stu的名字" << endl;
        cin >> name;
        dest = plist->search(name);
        if (dest==nullptr)
        {
            cout << "未查询到相关信息" << endl;
        }
        else
        {
            cout << "查询到相关信息如下" << endl;
            cout << "name" << " " << "age" << " " << "tel" << " " << "sex" << endl;
            dest->getdata().printSTU();
        }
        break;
    case 4:
        cout << "请输入你需要修改信息的stuの名字" << endl;
        cin >> name;
        dest = plist->search(name);
        if (!dest)
        {
            cout << "未查询到相关信息" << endl;
        }
        else
        {
            cout << "查找成功,请输入你需要修改成的name,age,tel,sex" << endl;
            cin >> dest->getdata().getName()
                >> dest->getdata().getAge()
                >> dest->getdata().getTel()
                >> dest->getdata().getSex();
            cout << "修改成功" << endl;
        }
        plist->SaveListToFile("1.txt");
        break;
    case 5:
        plist->printlist();
        break;
    }
}

void System::initSTU()
{
    string name; int age; string tel; string sex;
    cout << "input the stu's name,age,tel,sex" << endl;
    cin >> name >> age >> tel >> sex;
    plist->insert(STU(name, age, tel, sex));//插入链表。
}
                            
list*& System::getlist()
{
    return plist;
}

大功告成:

main:

#include"list2.h"
#include"stuManagement.h"
#include"System.h"
#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
	System * psystem = new System;
	while (1)
	{
		psystem->MakeMeue();
		psystem->KeyDown();
		system("pause");
		system("cls");
	}
	return 0;//19行实现管理系统(doge
}

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Ocean__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值