资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

1 实验的目的

创新互联公司企业建站,10余年网站建设经验,专注于网站建设技术,精于网页设计,有多年建站和网站代运营经验,设计师为客户打造网络企业风格,提供周到的建站售前咨询和贴心的售后服务。对于成都做网站、网站设计中不同领域进行深入了解和探索,创新互联在网站建设中充分了解客户行业的需求,以灵动的思维在网页中充分展现,通过对客户行业精准市场调研,为客户提供的解决方案。

通过用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

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<ID<<" ";

file1<Name<<" ";

file1<Age<<" ";

file1<Sex<<" ";

file1<Lanuage<<" ";

file1<score<

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

project.h(头文件):

#pragma once
#include#includeusing 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(输出结果)。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


名称栏目:C++信息管理系统(链表、文件、模块化程序设计)-创新互联
分享链接:http://cdkjz.cn/article/igjhe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220