【c语言课程设计 设备信息管理系统 文件 链表】

c语言课程设计 设备信息管理系统 文件 链表

一、 课程设计简洁

大一C语言课程,程序设计中,利用C语言实现一个小型的设备信息管理系统。课程中就是灵活运用C语言中的知识,掌握对于结构体,链表,指针,文件等变量的用法,以及掌握对于函数的嵌套调用,在本次课程中掌握对于编写程序的格式以及模块化,能够使得整体看上去更加简洁。

二、 程序功能介绍

以磁盘文件(device.txt)录入保存某单位里的计算机类设备信息,设备信息包括:设备编号、设备名称、类别,价格、状态(说明:"“类别"可有"台式机”"笔记本”“打印机”“扫描仪”“投影仪”等,"状态"前分为“在用“维修”“报废”三种) ,请编写一个小型设备管理程序,实现设备信息的添加查询和报修登记等。.
功能说明
(1) 可添加任意台新设备
(2)显示输出所有设备信息
(3)可分别按编号、类别、状态,查询出设备信息
(4) 进行某设备的报修登记,即修改该设备的状态信息("维修"或"报废的
(5)可删除“报废"的设备
(6) 可按类别统计该类设备的总价格,可统计所有设备的总价格
要求,以菜单的方式实现本管理程序,各功能可反复执行,数据更新后,退出程序前应保存入原device.txt文件

三、 数据

文件名 device.txt
文件目录

00001 小明的笔记本 笔记本 5000 在用
00002 小明的台式机 台式机 6000 在用
00003 小明的打印机 打印机 500 维修
00004 小明的扫描仪 扫描仪 500 维修
00005 小明的投影仪 投影仪 2000 报废

四、 源代码

建议dev c++运行,
给你带来帮助的话给个赞吧🐽

/*2,设备管理系统
问题描述:以磁盘文件(device.txt)录入保存某单位里的计算机类设备信息,设备信息包括:设备编号、设备名称、类别,价格、状态(说明:"“类别"可有"台式机”"笔记本”“打印机”“扫描仪”“投影仪”等,"状态"前分为“在用“维修”“报废”三种) ,请编写一个小型设备管理程序,实现设备信息的添加查询和报修登记等。.
功能说明
(1) 可添加任意台新设备
(2)显示输出所有设备信息
(3)可分别按编号、类别、状态,查询出设备信息
(4) 进行某设备的报修登记,即修改该设备的状态信息("维修"或"报废的
(5)可删除“报废"的设备
(6) 可按类别统计该类设备的总价格,可统计所有设备的总价格
要求,以菜单的方式实现本管理程序,各功能可反复执行,数据更新后,退出程序前应保存入原device.txt文件*/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NO 6		//设备编号为5位
#define NA 21		//设备名称最多为10个汉字
#define CATE 6		//设备类型为3个汉字
#define STA 5		//设备状态为2个汉字

typedef struct devices {
	char dno[NO];
	char dname[NA];
	char dcategory[CATE];
	int dprice;
	char dstatus[STA];
	struct devices* next;
}device;

device* load();
device* add(device*);
void show_all(device*);
void search(device*);
device* change_status(device*);
device* delete_bad_device(device*);
void money(device*);
void end(device*);
void menu();
void test_empty(device*);

int main()
{
	int op; 
	device *head=NULL;
	head = load();
	while (1) {
		menu();
		printf("请输入选择编号:\n");
		scanf("%d", &op);
		getchar();
		switch (op) {
		case 1: {
			head=add(head);
			break;
		}
		case 2:{
			show_all(head);
			break;
		}
		case 3:{
			search(head);
			break;
		}
		case 4:{
			head=change_status(head);
			break;
		}
		case 5: {
			head=delete_bad_device(head);
			break;
		}
		case 6:{
			money(head);
			break;
		}
		case 0: {
			end(head);
			return 0;
		}
		default: {
			printf("输入有误,按回车键重新选择!\n");
			getchar();
		}
		}
	}
}


void menu() {
	printf("	设备管理系统\n");
	int i;
	for (i = 0; i < 40; i++)printf("*");
	printf("\n");
	printf("	(1)添加新设备\n");
	printf("	(2)显示输出所有设备信息\n");
	printf("	(3)查询出设备信息\n");
	printf("	(4)进行某设备的报修登记\n");
	printf("	(5)删除报废的设备\n");
	printf("	(6)统计设备的总价格\n");
	printf("	(0)保存数据结束程序\n");
	for (i = 0; i < 40; i++)printf("*");
	printf("\n");
}

device* load()
{
	char no[NO];
	char name[NA];
	char category[CATE];
	int price;
	char status[STA];


	FILE* fp;
	device *head, *ptr;
	
	head = (device*)malloc(sizeof(device));
	ptr = head;
	ptr->next=NULL;
	
	if (NULL==(fp = fopen("device.txt", "r"))) 
	{
		printf("ERROR!\n"); 
			exit(0);
	}
	while (!feof(fp))
	{

		fscanf(fp,"%s %s %s %d %s\n", &no, &name, &category, &price, &status);
			
		ptr->next = (device*)malloc(sizeof(device));
		ptr = ptr->next;
		ptr->next = NULL;

		
		strcpy((ptr->dno),no);
		strcpy((ptr->dname), name);
		strcpy((ptr->dcategory), category);
		ptr->dprice = price;
		strcpy((ptr->dstatus),status);
		
		
	}
	
	fclose(fp);

	return head; 
} 

device* add(device* x){
	char no[NO];
	char name[NA];
	char category[CATE];
	int price;
	char status[STA];
	
	device *head;
	device *p;
	head=x;
	p=head;
	while(p->next){
		p=p->next;
	}
	p->next=(device*)malloc(sizeof(device));
	p=p->next;
	p->next=NULL;
	
	printf("\n请依次输入一个新设备的信息设备编号、设备名称、类别,价格、状态。\n(类别有台式机、笔记本、打印机、扫描仪、投影仪等,状态分为在用、维修、报废、三种)\n");
	scanf("%s %s %s %d %s", &no, &name, &category, &price, &status);
		getchar();
		strcpy((p->dno),no);
		strcpy((p->dname), name);
		strcpy((p->dcategory), category);
		p->dprice = price;
		strcpy((p->dstatus),status);
		
	return head;
}

void show_all(device *head) {
	head=head->next;
	printf("设备的信息设备编号、设备名称、类别,价格、状态信息如下:\n");
	while(head!=NULL){
		printf("%s %s %s %d %s\n", head->dno, head->dname, head->dcategory, head->dprice, head->dstatus);
		head = head->next;
	}
}

void search(device *head) {
	device *p,*q;
	q=head;
	p=q->next;
	
	int number;
	char no[NO];
	char category[CATE];
	char status[STA];
	
	
	loop:printf("请输入要查询的设备选项:\n(0)编号\n(1)类型\n(2)状态\n");
	scanf("%d", &number);
	getchar();
	switch (number){
	case 0:
		printf("请输入(0)编号\n"); 
		scanf("%s", &no);
		getchar();
		while (p != NULL) {
			if (strcmp((p->dno),no)==0) {
				printf("%s %s %s %d %s\n", p->dno, p->dname, p->dcategory, p->dprice, p->dstatus);
				break;
			}
			p = p->next;
		}break;
	case 1:
		printf("请输入(1)类型\n"); 
		scanf("%s", &category);
		getchar();
		while (p != NULL) {
			if (strcmp((p->dcategory),category)==0){
				printf("%s %s %s %d %s\n", p->dno, p->dname, p->dcategory, p->dprice, p->dstatus);
			}
			p= p->next;
		}break;
	case 2:
		printf("请输入(2)状态\n"); 
		scanf("%s", &status);
		getchar();
		while (p != NULL) {
			if (strcmp((p->dstatus),status)==0) {
				printf("%s %s %s %d %s\n", p->dno, p->dname, p->dcategory, p->dprice, p->dstatus);
			}
			p = p->next;
		}break;
	default: 
		printf("输入错误,请重新输入...");
		goto loop; 
	}
}

device* change_status(device* head) {
	
	char no[NO]; 
	char status[STA];
	
	printf("请输入修改设备的编号:");
	scanf("%s", &no);
	getchar();
	device *p,*q;
	p=head;
	q=p->next;
	while (q != NULL) {
		if (strcmp((q->dno),no)==0)
		{
			printf("设备当前状态为:%s",q->dstatus);
			printf("请输入修改后的状态:");
			scanf("%s", &status);
			strcpy((q->dstatus),status);
		}
		q = q->next;
	}
	return head;
}

device* delete_bad_device(device *head) {
	
	char st[]="报废";
	printf("%s",st); 
	device *q,*d;
	device *x;
	x=head->next;
	q=x;
	d=x->next;
	while(d!=NULL)
{
	if(strcmp(d->dstatus,st)==0)
	{
		q->next=d->next;
		free(d);
		break;
	}
	else
	{
		q=d;
		d=d->next;
	}
}
	test_empty(head);
	return q;
}


void test_empty(device *head){
	char st[]="报废";
	head=head->next;
			while (head!= NULL) {
		if (strcmp((head->dstatus),st)!=0) {
			printf("删除成功!\n");
		}
		head = head->next;
		}
}

void money(device *x) {
	device* y,*head;
	y=x;
	head=y->next;
	
	int sum = 0;
	int number;
	char no;
	char category;
	char status;
	char d1[]="台式机";
	char d2[]="笔记本";
	char d3[]="打印机";
	char d4[]="扫描仪";
	char d5[]="投影仪";
	printf("请输入要查询的设备类型:\n(0)全部\n(1)台式机\n(2)笔记本\n(3)打印机\n(4)扫描仪\n(5)投影仪\n");
	scanf("%d", &number);
	getchar();
	switch (number) {
	case 0:
		while (head != NULL) {
			if (head->dcategory) {
				sum+=(int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	case 1:
		while (head != NULL) {
			if (strcmp((head->dcategory),d1)==0) {
				sum+=(int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	case 2:
		while (head != NULL) {
			if (strcmp((head->dcategory),d2)==0) {
				sum += (int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	case 3:
		while (head != NULL) {
			if (strcmp((head->dcategory),d3)==0) {
				sum += (int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	case 4:
		while (head != NULL) {
			if (strcmp((head->dcategory),d4)==0) {
				sum += (int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	case 5:
		while (head != NULL) {
			if (strcmp((head->dcategory),d5)==0) {
				sum += (int)(head->dprice);
			}
			head = head->next;
		}
		printf("%d\n",sum);
		break;
	default:
		printf("指令错误!...");
		void money(device * head);
		break;
}

}

void end(device *head)
{

	device *p,*q;
	FILE* fp;
	p=head;
	q = p->next;
	
	if ((fp = fopen("device.txt", "w"))==NULL) 			
	{
		printf("error!");
		exit(0);
	}
	else{
		printf("打开文件成功!\n");
	}
	
	while (q != NULL)						
	{
		fprintf(fp,"%s %s %s %d %s\n", q->dno, q->dname, q->dcategory,q->dprice, q->dstatus);
		q = q->next;
	}
	printf("保存文件成功!\n");
	fclose(fp);	
}






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值