C++信息管理系统(链表、文件、模块化程序设计)

1 实验的目的

通过用C++编写一个信息管理系统,强化模块化程序设计思想,能够将C++程序设计中的结构体、链表、数组、函数及函数重载、文件等各种概念,灵活的运用到实际的程序设计中去。

2 实验要求

具体要求如下:

  1. 数据组织方面尽量使用:数组、结构体、链表、文件;
  2. 程序结构方面做到模块化。
  3. 程序须有一定的健壮性和必要的提示信息,考虑问题的多种可能和边界数据。

    综合实验具体完成以下功能:

  1. 从文件中读取的信息存储于链表中;
  2. 设计查找函数:可按照指定字段查找志愿者信息;

3)设计增加函数:实现增加信息功能;

4)设计删除函数:实现删除某条信息功能;

5)设计输出函数能够按照要求将信息写入输出文件中。

6)菜单选项。

3 实验原理

实验使用模块化的程序设计思想,使用数据、结构体、链表、文件进行数据组织,分模块完成各功能。

4 实验说明

志愿者信息可用结构体存储:

struct person{

int ID;

char Name[20];

int  Age;

char Sex;

char Lanuage[10];

double score;

person *next;

};

函数说明:

person * input(const char *fileName);//读取input文件并并创建链表

void input(const char *fileName,person *q);//读取input1文件

void input(const char *fileName,int &num);//读取menu文件或find文件

void output(person *head,const char *fileName);//将链表内容写入文件

person * find(person *head,int no);//查找Id为no的人员,并返回改结点前一结点便于删除函数调用

person* del(person *head,int no);//从链表中删除Id为no的人员,返回链首

person *append(person *head ,person *s);//插入一名人员至链尾,返回链首

主函数中设置菜单选项,1  查找   2  插入    3 删除

不同选项调用各功能函数,所有功能测试结果均需写入文件。

可根据实际情况自定义其他函数。

注意事项:

1 输入文件包括四个:input.txt -----存放所有人员信息

menu.txt------存放菜单选项,数字1--3选一  

find.txt------ 查找人员ID

input1.txt-----插入人员信息

2输出文件一个:out.txt-----调用各函数后的结果均写入该文件

所有文件的内容如需分隔,用单个空格分隔。

读取写入文件需包含

#include <fstream>

using namespace std;

//创建文件流对象

ifstream file;

ofstream file1

file.open("input.txt",ios::in);//读方式打开文件inout.txt

file1.open("out.txt",ios::out);//写方式打开文件out.txt

person *q = new person;

//读取文件内容至q指向的人员结点

while(!file.eof())

{

    file>>q->ID>>q->Name>>q->Age>>q->Sex>>q->Lanuage>>q->score;

   }

//将q指向的结点信息写入file1文件

file1<<q->ID<<" ";

       file1<<q->Name<<" ";

       file1<<q->Age<<" ";

       file1<<q->Sex<<" ";

       file1<<q->Lanuage<<" ";

       file1<<q->score<<endl;

废话不多说,下面直接上代码:

project.h(头文件):

#pragma once
#include<iostream>
#include<fstream>
using namespace std;
struct person {
	int ID;
	char Name[20];
	int Age;
	char Sex;
	char Language[10];
	double score;
	person* next;
};

person* input(const char* fileName);
void input(const char* fileName, person* q);
void input(const char* fileName, int& num);
void output(person* head, const char* fileName);
person* find(person* head, int no);
person* del(person* head, int no);
person* append(person* head, person* s);

main.cpp(主函数):

#include"project.h"
using namespace std;

void input(const char* fileName, person* per)
{
	ifstream file;
	file.open(fileName, ios::in);
	file >> per->ID >> per -> Name >> per->Age >> per->Sex >> per->Language >> per->score;
}

void input(const char* fileName, int& num)
{
	ifstream file;
	file.open(fileName, ios::in);
	file >> num;
}

void output(person* head, const char* fileName)
{
	ofstream file1;
	file1.open(fileName, ios::out);
	person* per = head;
	while (per->next != NULL) {
		per = per->next;
		file1 << per->ID << " " << per->Name << " " << per->Age << " " << per->Sex << " " << per->Language << " " << per->score << endl;

	}
}

int main()
{
	person* head = new person;
	head = input("input.txt");

	int num = 0;
	input("menu.txt", num);

	switch (num) {

	case 1 : {
			int ID = 0;
			input("find.txt", ID);
			head = find(head, ID);
			output(head, "out.txt");
			break;
		}

	case 2 : {
			person* per = new person;
			input("input1.txt", per);
			head = append(head, per);
			output(head, "out.txt");
			break;
		}

	case 3 : {
			int ID = 0;
			input("find.txt", ID);
			head = del(head, ID);
			output(head, "out.txt");
			break;
		}

	

	}
	
	return 0;
}

input.cpp(创建链表):

#include"project.h"
using namespace std;

person* input(const char* fileName)
{
	ifstream file;
	file.open(fileName, ios::in);
	person* head = new person;
	person* per = new person;
	head->next = per;
	file >> per->ID >> per->Name >> per->Age >> per->Sex >> per->Language >> per->score;
	while (!file.eof()) {
		person* q = new person;
		file >> q->ID >> q->Name >> q->Age >> q->Sex >> q->Language >> q->score;
		per->next = q;
		per = per->next;
	}
	per->next = NULL;
	return head;
}

find.cpp(实现查找功能):

#include"project.h"
using namespace std;

person* find(person* head, int no)
{
	person* per = head->next;
	while (per->next != NULL) {
		if (per->ID == no) {
			head->next = per;
			per->next = NULL;
			return head;
		}
		per = per->next;
	}
}

append.cpp(实现插入功能):

#include"project.h"
using namespace std;
person* append(person* head, person* s)
{
	person* per = head->next;
	while (per->next != NULL) {
		per = per->next;
	}
	per->next = s;
	per = per->next;
	per->next = NULL;
	return head;
}

del.cpp(实现删除功能):

#include"project.h"
using namespace std;
person* del(person* head, int no)
{
	person* per = head->next;
	person* stu = new person;
	stu->next = per;
	while (per->next != NULL) {
		if (per->ID == no) {
			stu->next = per->next;
			return head;
		}
		stu = stu->next;
		per = per->next;
	}
}

值得注意的是,本程序在实现插入功能时只能将数据插入到末尾,不能插入中间,感兴趣的同学可以尝试修改或增加新的功能。

另外还需建立五个文本文件:menu.txt(输入菜单选项)、input.txt(存放人员信息)、find.txt(输入要查找或删除的人员ID)、input1.txt(输入要插入的人员信息)、out.txt(输出结果)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玄殛~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值