学生系统文件C++

二进制文件读写,链表操作

student.h

#pragma once
#include <iostream>
#include<cstring>

using namespace std;
class person
{
protected:
	char name[20];
	int age;
public:
	person();
	person(char *pname, int page);
	char *get_name() { return name;}
	int  get_age()   { return age; }
	char *set_name(char *pname){ strcpy(name, pname); return name; }
	int set_age(int page){ age = page; return age; }
};
class student :public person
{
private:
	char snum[12];
	double sgpa;
public:
	student *next;
	student();
	student(char*pname, int page, char *num, double gpa);
	void input();
	void show();
	char  *get_snum() { return snum; }
	double get_sgpa() { return sgpa; }
	char  *set_snum(char *pnum)  { strcpy(snum, pnum); return snum; }//设置
	double set_sgpa(double pgpa){ sgpa = pgpa; return sgpa; }
};

mylist.h

#pragma once
#include"student.h"
class mylist
{
private:
	student *head;
public:
	mylist();
	mylist(int n);                //构造链表(键盘输入)
	mylist(student s[],int n);    //构造链表(传对象数组)
	void show(int f);             //输出链表
	~mylist();
	void savetofile();            //保存
	void readfromfile();          //读取
	int delnodeforsnum(const char inputsnum[]);//根据名字删除
	void delnode(student*node);
	void find(const char inputsnum[]);//查找并修改
	void insert();                    //手动添加学生
	void sort();                      //按绩点排序
	void swap(student*p,student*q);   //交换节点的内容(成员变量)
	int change(student *node);        //修改学生信息 	
	struct student*gethead(){ return head; }
};

stub.h

#include"mylist.h"
#include<stdlib.h>
#include<fstream>
`person::person()
{
	age = 0;
}
person::person(char*pname, int page)
{
	strcpy(name, pname);
	age = page;
}

student::student()
{
	input();
}
student::student(char*pname, int page, char* num, double gpa) :person(pname, page),
sgpa(gpa)
{
	strcpy(snum, num);
	//printf("||对象创建成功||\n");
}
void student::show()``
{
	cout << "姓名: " << name << "\t"
		<< "年龄: " << age << "\t"
		<< "学号: " << snum << "\t"
		<< "绩点: " << sgpa << "\t"
		<< endl;
}
void student::input()
{
	cout << "输入姓名:";
	cin >> name;
	cout << "输入年龄:";
	cin >> age;
	cout << "输入学号:";
	cin >> snum;
	cout << "输入绩点:";
	cin >> sgpa;
}

mylist.h

#include "mylist.h"
#include <iostream>
#include <fstream>

mylist::mylist()
{
	head = new student("无", 1, "00000000000", 0.0);
	head->next = NULL;
}
mylist::mylist(int n)
{
	head = new student("无",1,"00000000000",0.0);
	head->next = NULL;
	int i;
	for (i = 0; i < n; i++)
	{
		student *p = new student();
		p->next = head->next;
		head->next = p;
	}
}
mylist::mylist(student s[], int n)
{
	/*head = new student("无", 1, "00000000000", 0.0);
	head->next = NULL;*/
	int i;
	for (i = 0; i < n; i++)
	{
		student *p = new student(s[i].get_name(),s[i].get_age(),s[i].get_snum(),s[i].get_sgpa());
		p->next = head->next;
		head->next = p;
	}
}
//打印 0/1:横/竖打印
void mylist::show(int f)  
{
	student *p = head->next;
	if (f == 0)
	{
		while (p)
		{
			cout << "姓名: " << p->get_name() << "\t"
				 << "年龄: " << p->get_age()  << "\t"
				 << "学号: " << p->get_snum() << "\t"
				 << "绩点: " << p->get_sgpa() << "\t"
				 << endl;
			p = p->next;
		}
	}
	if (f == 1)
	{
		cout << "姓名" << "\t" << "年龄" << "\t" << "学号" << "\t\t" << "绩点" << endl;
		while (p)
		{
			cout << p->get_name() << "\t"
				 << p->get_age()  << "\t"
				 << p->get_snum() << "\t"
				 << p->get_sgpa() << "\t"
				 << endl;
			p = p->next;
		}
	}
}
void mylist::savetofile()
{
	ofstream outFile("student.dat", ios::out | ios::binary);//写入
	student*p = head->next;
	while (p)
	{
		outFile.write((char*)p, sizeof(*p));               //写入
		p = p->next;
	}
	outFile.close();
}
void mylist::readfromfile()
{
	ifstream inFile("student.dat", ios::in | ios::binary); //二进制读方式打开
	head = new student("无", 1, "00000000000", 0.0);
	head->next = NULL;
	student p("",0,"",0.0);
	while (inFile.read((char *)&p, sizeof(student)))//一直读到文件结束 
	{ 
		//printf("%d ",inFile.gcount());
		//打印   //inFile.gcount()返回值就是最近一次 read() 方法成功读取的字节数
		student *newnode = new student(p.get_name(), p.get_age(), p.get_snum(), p.get_sgpa());
		newnode->next = head->next;
		head->next = newnode;
	}
	inFile.close();
}
void mylist::insert()//插入信息
{
	student *p = new student();//手动输入
	p->next = head->next;
	head->next = p;
}
void mylist::find(const char inputsnum[])//返回找到指定名字的学生人数
{
	student *p = head->next;
	int sum = 0;//人数
	while (p)
	{
		if (strcmp(p->get_snum(), inputsnum) == 0)
		{
			sum++;
			printf("找到学生:%s\n",p->get_snum());
			change(p);
		}
		p = p->next;
	}
	if (sum == 0)
		printf("没有找到此学生:%s\n",inputsnum);
}
void mylist::delnode(student*node) //删除一个节点
{
	student*p = head, *q = head->next;
	while (q!=node)
	{
		p = p->next;
		q = q->next;
	}
	p->next = q->next;
	delete q;
}
int mylist::delnodeforsnum(const char inputsnum[])  //依据姓名删除
{
	student *p = head->next;
	printf("正在删除:%s\n",inputsnum);
	while (p)
	{
		if (strcmp(p->get_name(), inputsnum) == 0)
		{
			delnode(p);  //删除节点
			printf("删除成功!\n");
			return 0;
		}
		p = p->next;
	}
	printf("未找到学生!\n");
	return -1;
}
void mylist::sort()    //冒泡排序
{
	student*p = head->next,*q=p->next;
	student*last = NULL;
	int temp1;
	while (p != last)
	{
		while (q != last)
		{
			if (p->get_sgpa() > q->get_sgpa())
			{
				swap(p,q);// 交换两个节点内容(成员变量)
			}
			p = p->next;
			q = q->next;
		}
		last = p;
		//重置p,q
		p = head->next;
		q = p->next;
	}
}
void mylist::swap(student*p, student*q)//交换两个节点内容
{
	int temp1;
	char temp2[40];
	double temp3;

	strcpy(temp2,p->get_name());
	p->set_name(q->get_name());
	q->set_name(temp2);

	temp1 = p->get_age();
	p->set_age(q->get_age());
	q->set_age(temp1);

	strcpy(temp2, p->get_snum());
	p->set_snum(q->get_snum());
	q->set_snum(temp2);

	temp3 = p->get_sgpa();
	p->set_sgpa(q->get_sgpa());
	q->set_sgpa(temp3);
}
int mylist::change(student *node) //修改
{
	student*p = node;
	char pname[20], psnum[12];
	int page;
	double pgpa;
	printf("修改信息:\n");
	p->show();
	int n;
	printf("修改选择:(0:全部修改)(1:单项修改)(其它按键:不做修改)\n");
	cout << "请输入修改方式( 0 或 1 ):"; cin >> n;
	if (n == 0)
	{
		//输入信息
		cout << "输入新名字:"; cin >> pname;
		cout << endl;
		cout << "输入新年龄:"; cin >> page;
		cout << endl;
		cout << "输入新学号:"; cin >> psnum;
		cout << endl;
		cout << "输入新绩点:"; cin >> pgpa;
		cout << endl;
//更改信息
		p->set_name(pname);
		p->set_age(page);
		p->set_snum(psnum);
		p->set_sgpa(pgpa);
	}
else if (n == 1)
	{
		int m = 0;
		cout << "输入0:修改姓名" << endl;
		cout << "输入1:修改年龄" << endl;
		cout << "输入2:修改学号" << endl;
		cout << "输入3:修改绩点" << endl;
		cin >> m;
		switch (m)
		{
		case 0:
			cout << "输入新名字:"; cin >> pname;
			p->set_name(pname);
			break;
case 1:
			cout << "输入新年龄:"; cin >> page;
			p->set_age(page);
			break;
		case 2:
			cout << "输入新学号:"; cin >> psnum;
			p->set_snum(psnum);
			break;
		case 3:
			cout << "输入新绩点:"; cin >> pgpa;
			p->set_sgpa(pgpa);
			break;
		default:
			cout << "输入不规范" << endl;
			return 0;
		}
	}
	else
	{
		cout << "没有修改" << endl;
		return 0;
	}
	return 1;
}

mylist::~mylist()
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值