C语言大作业:车辆管理系统

C语言大作业:车辆管理系统

流程图

C语言大作业流程图
大概流程
具体流程
在这里插入图片描述

代码

​#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<conio.h>
typedef struct car;
typedef struct list_car;
typedef struct list_car_sequence;
list_car* rear;
list_car* head;

list_car* read_list_car();
void write_list_car(list_car* head);
void list_car_write();
void list_car_alter(list_car* head);
void list_car_delete();
void search_prize(list_car* head);
void search_other(list_car* head);
list_car_sequence* list_dissequence(list_car_sequence* dissequence, list_car* p);
list_car_sequence* list_sequence(list_car_sequence* sequence_head, list_car* p, int i);

/* 数据模型 */
struct car {
	char type_of_name[10];//车型
	char manufacturer[10];//厂商
	char model_class[10];//车型级别
	char transmission_case[10];//变速箱
	char color[10];//颜色
	char math[3][10];//0是价格	1是排量	2是座位数
};
/* 链表 */
struct list_car {
	car* imformation;
	list_car* next;
};
/* 按顺序排列链表 */
struct list_car_sequence {
	char name[10];
	char imformation[10];
	list_car_sequence* next;
};
/* [结构体] 从 [文本] 获得信息 */
list_car* read_list_car() {//-->改动后
	FILE* fp;
	list_car* create, * head = NULL;
	int num = 0;
	if ((fp = fopen("car.txt", "r")) == NULL) {
		printf("打开文件失败");
		exit(0);
	}
	create = rear = (list_car*)malloc(sizeof(list_car));
	create->imformation = (car*)malloc(sizeof(car));
	while ((fread(create->imformation, sizeof(car), 1, fp)) != 0) {
		num++;
		if (num == 1)
			head = rear;
		else
			rear->next = create;
		rear = create;
		create = (list_car*)malloc(sizeof(list_car));
		create->imformation = (car*)malloc(sizeof(car));
	}
	if (num != 0)
		rear->next = NULL;
	free(create->imformation);
	free(create);
	fclose(fp);
	printf("录入成功");
	return head;
}

/* [文本] 从 [结构体] 获得信息 */
void write_list_car(list_car* head) {//-->改动后
	FILE* fp;
	if ((fp = fopen("car.txt", "w")) == NULL) {
		printf("打开文件失败");
		exit(0);
	}
	list_car* p = head;
	for (;p != NULL;p = p->next)	fwrite(p->imformation, sizeof(car), 1, fp);
	printf("写入文件成功!\n");
	fclose(fp);
}
/******************************************************//*		修改	*//*************************************************************/
/* 录入车辆信息进 [结构体内]  */
void list_car_write() {
	list_car* p = (list_car*)malloc(sizeof(list_car));
	p->next = NULL;//容易漏
	p->imformation = (car*)malloc(sizeof(car));
	printf("请输入\n");
	scanf_s("%s %s %s %s %s %s %s %s", p->imformation->type_of_name, sizeof(char)*10
		, p->imformation->manufacturer, sizeof(char) * 10, p->imformation->model_class
		, sizeof(char) * 10, p->imformation->math[2], sizeof(char) * 10
		, p->imformation->math[1], sizeof(char) * 10, p->imformation->transmission_case
		, sizeof(char) * 10, p->imformation->color, sizeof(char) * 10, p->imformation->math[0], sizeof(char) * 10);
	getchar();
	printf("%-10s""%-10s""%-10s""%10s座""%10sT\t""%-10s""%-10s""%10s万\n", p->imformation->type_of_name, p->imformation->manufacturer, p->imformation->model_class, p->imformation->math[2], p->imformation->math[1], p->imformation->transmission_case, p->imformation->color, p->imformation->math[0]);
	getch();
	if (head == NULL)
		head = rear = p;
	else {
		rear->next = p;
		rear = p;
	}
}
/* 修改 [结构体内] 车辆具体信息 */
void list_car_alter(list_car* head) {

	if (head == NULL) {
		printf("头指针为空!\n");
		return;
	}

	list_car* p = head;
	char name[20];
	printf("请输入型号:\n");
	scanf_s("%s", name, sizeof(name));
	getchar();

	for (;p != NULL;p = p->next)
		if (strcmp(name, p->imformation->type_of_name) == 0)
			break;
	if (p == NULL) {
		printf("没有找到该类型!\n");
		return;
	}

	printf("请输入修改类型的序号:\n");
	printf("1车型\t2厂商\t3车型级别\t4座位数\t5排量\t6变速箱\t7颜色\t8价格\n");
	char imformation;
	imformation = getchar();getchar();

	printf("请输入修改后内容\n");
	switch (imformation)
	{
	case '1':scanf_s("%s", p->imformation->type_of_name, sizeof(char)*10);break;
	case '2':scanf_s("%s", p->imformation->manufacturer, sizeof(char) * 10);break;
	case '3':scanf_s("%s", p->imformation->model_class, sizeof(char) * 10);break;
	case '4':scanf_s("%s", p->imformation->math[2], sizeof(char) * 10);break;
	case '5':scanf_s("%s", p->imformation->math[1], sizeof(char) * 10);break;
	case '6':scanf_s("%s", p->imformation->transmission_case, sizeof(char) * 10);break;
	case '7':scanf_s("%s", p->imformation->color, sizeof(char) * 10);break;
	case '8':scanf_s("%s", p->imformation->math[0], sizeof(char) * 10);break;
	}
	getchar();
	printf("修改成功!\n");
	getchar();

}
/* 删除 [结构体内] 车辆信息 */
void list_car_delete() {
	if (head == NULL) {
		printf("头指针为空!\n");
		return;
	}
	printf("进入修改\n");
	list_car* p = head,*t=head;
	char name[20];
	printf("请输入\n");
	scanf_s("%s", name, sizeof(name));
	getchar();
	for (;p != NULL;p = p->next)
		if (strcmp(name, p->imformation->type_of_name) == 0) {
			printf("已找到\n");
			break;
		}
		else
			t = p;
	if (p == NULL) {
		printf("没有找到该类型!\n");
		return;
	}

	printf("请输入删掉的内容\n");
	char information;
	printf("0删除车辆全部信息\t1车型\t2厂商\t3车型级别\t4座位数\t5排量\t6变速箱\t7颜色\t8价格\n");	
loop:scanf_s("%c", &information, sizeof(information));
	getchar();
	switch (information) {//结构体里的字符型数组是不能被赋值的,我也不知道为什么.好像是被当成了常量
	case '0':
		if (p != head)
			t->next = p->next;
		else 
			head = head->next;
		break;
	case '1':strcpy(p->imformation->manufacturer, "无信息");break;
	case '2':strcpy(p->imformation->model_class, "无信息");break;
	case '3':strcpy(p->imformation->math[2], "无信息");break;
	case '4':strcpy(p->imformation->math[1], "无信息");break;
	case '5':strcpy(p->imformation->transmission_case, "无信息");break;
	case '6':strcpy(p->imformation->color, "无信息");break;
	case '7':strcpy(p->imformation->math[0], "无信息");break;
	default:printf("无法识别该序号,请重新输入序号");goto loop;
	}
	printf("删除成功!\n");
	getchar();
}
/******************************************************//*		修改	*//*************************************************************/
/******************************************************//*		输出	*//*************************************************************/
/* 查询 [结构体内] 车辆具体信息 */
void search(list_car* head) {
	if (head == NULL) {
		printf("头指针为空!\n");
		return;
	}
	void search_prize(list_car * head);
	void search_other(list_car * head);
	char find[10] = { 0 };//字符初始化
	
	printf("请输入你想查找的范围:例如 价格 、 厂商 、 车型级别 ,退出则输入 退出\n");
	scanf_s("%s", find, sizeof(find));
	while ((strcmp(find,"退出")!=0)) {
		getchar();
		if (strcmp(find, "价格") == 0)
			search_prize(head);
		else if (strcmp(find, "厂商") == 0 || strcmp(find, "车型级别") == 0)
			search_other(head);
		else
			printf("搜索没有此项,请重新输入\n");
		printf("请输入你想查找的范围:例如 价格 、 厂商 、 车型级别 ,退出则输入 退出\n");
		scanf_s("%s", find, sizeof(find));
	}
	
}
/*价格排序*/
void search_prize(list_car* head) {
	list_car* p = head;
	list_car_sequence* sequence_head = NULL;
	float prize_down, prize_high;
	printf("请依次输入最低价格和最高价格\n");
	scanf_s("%f%f", &prize_down, &prize_high);
	for (;p != NULL;p = p->next)
		if (atof(p->imformation->math[0]) >= prize_down && atof(p->imformation->math[0]) <= prize_high)
			sequence_head = list_sequence(sequence_head, p,0);
	if (sequence_head == NULL) {
		printf("该价格范围内无车辆型号:\n");
		return;
	}
	for (;sequence_head != NULL;sequence_head = sequence_head->next,printf("\n"))
		printf("%-10s\t%10s\n", sequence_head->name,sequence_head->imformation);
	
}
/* 查厂商或车型级别 */
void search_other(list_car* head) {
	list_car* p = head;
	list_car_sequence* dissequence_head=NULL;
	printf("请输入你想找的内容:\n");
	char name[20] = { 0 };
	scanf_s("%s", name, sizeof(name));
	getchar();
	for (;p != NULL;p = p->next)
		if (strcmp(name, p->imformation->manufacturer) == 0 || strcmp(name, p->imformation->model_class) == 0 ) {
			dissequence_head = list_dissequence(dissequence_head, p);
		}
	if (dissequence_head == NULL) {
		printf("没有找到相关内容!\n");
		return;
	}
	for (;dissequence_head != NULL;dissequence_head = dissequence_head->next)
		printf("%s\n", dissequence_head->name);
	
}
/* 价格排序 *//* 排量排序 *//* 座位数排序 */
list_car_sequence* list_sequence(list_car_sequence* sequence_head, list_car* p,int i) {

	if (sequence_head == NULL) {
		
		sequence_head = (list_car_sequence*)malloc(sizeof(list_car_sequence));
		strcpy(sequence_head->imformation, p->imformation->math[i]);
		strcpy(sequence_head->name, p->imformation->type_of_name);
		sequence_head->next = NULL;
		return sequence_head;
	}
	else {
		list_car_sequence* t = sequence_head, * next = NULL, * x = sequence_head;
		
		for (;sequence_head != NULL;sequence_head = sequence_head->next) {
			
			if (atof(sequence_head->imformation) >= atof(p->imformation->math[i])) {
				
				
				if (t == sequence_head) {
					
					next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
					next->next = sequence_head;
					strcpy(next->imformation, p->imformation->math[i]);
					strcpy(next->name, p->imformation->type_of_name);
					
					return next;
					
				}
				else {
					
					next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
					t->next = next;
					next->next = sequence_head;
					strcpy(next->imformation, p->imformation->math[i]);
					strcpy(next->name, p->imformation->type_of_name);
					return x;
				}
			}
			else
				t = sequence_head;
		}
		if (sequence_head == NULL) {
			next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
			strcpy(next->imformation, p->imformation->math[i]);
			strcpy(next->name, p->imformation->type_of_name);
			t->next = next;
			next->next = NULL;
			return x;
		}
	}
}
/*  无排序 */
list_car_sequence* list_dissequence(list_car_sequence* dissequence, list_car* p) {
	
	if (dissequence == NULL) {
		dissequence = (list_car_sequence*)malloc(sizeof(list_car_sequence));
		strcpy(dissequence->name, p->imformation->type_of_name);
		strcpy(dissequence->imformation,"/0");
		dissequence->next = NULL;
		return dissequence;
	}
	else {
		list_car_sequence* creat = (list_car_sequence*)malloc(sizeof(list_car_sequence));
		strcpy(creat->name, p->imformation->type_of_name);
		strcpy(creat->imformation, "/0");
		creat->next = dissequence;
		return creat;
	}
}
/* 浏览 [结构体内] 所有车辆信息 */
void export_list(list_car* head) {
	list_car_sequence* prize_head=NULL, * seating_head=NULL, * output_head=NULL;
	if (head == NULL){
		printf("头指针为空!\n");
		return;
	}
		
	else {
		printf("车型\t\t厂商\t\t车型级别\t座位数\t     排量\t变速箱\t\t车身颜色\t价格\n");
		printf("_______________________________________________________________________________________________________________\n");
		int num = 0;
		for (; head != NULL; head = head->next) {
			num++;
			printf("%-10s\t""%-10s\t""%-10s""%10s座""%10sT\t""%-10s\t""%-10s""%10s万\n", head->imformation->type_of_name, head->imformation->manufacturer, head->imformation->model_class, head->imformation->math[2], head->imformation->math[1], head->imformation->transmission_case, head->imformation->color, head->imformation->math[0]);
			printf("\n");
			prize_head = list_sequence(prize_head, head,0);
			seating_head = list_sequence(seating_head, head, 2);
			output_head = list_sequence(output_head, head, 1);
		}	
		printf("一共有%02d辆车\n", num);
	}
	printf("从小到大类型:");
	char c;
	list_car_sequence* p;
	printf("1价格 2座位数 3排量 CTRL+Z退出 ");
	while (~(c = getchar())) {

		switch (c) {
		case '1':p = prize_head;break;
		case '2':p = seating_head;break;
		case '3':p = output_head;break;
		default:system("cls");printf("请重新输入:");goto loop;
		}


		if (p == NULL) {
			printf("没有信息!\n");
			return;
		}
		printf("_____________\n");
		for (;p != NULL;p = p->next,printf("\n"))
			printf("%-10s\t\t%10s\n", p->name, p->imformation);
		
		getchar();
		loop:	printf("1价格 2座位数 3排量 CTRL+Z退出 ");
	}
}

/******************************************************//*		输出	*//*************************************************************/
int main() {
	head = read_list_car();
	char x;
	printf("1为浏览所有信息 2为查询车辆具体信息 3录入新的车辆 4修改车辆信息 5为删除车辆信息 0为退出\n"
		"请选择你想进行的功能:");
	
	while ((x = getch())!='0') {
		system("cls");
		switch (x) {
		case '1':export_list(head);break;
		case '2':search(head);break;
		case '3':list_car_write();break;
		case '4':list_car_alter(head);break;
		case '5':list_car_delete();break;
		default:printf("输入有误,请重新输入:\n");getch();break;
		}
		system("cls");
		printf("1为浏览所有信息 2为查询车辆具体信息 3录入新的车辆 4修改车辆信息 5为删除车辆信息 0为退出\n"
			"请选择你想进行的功能:");
	}
	write_list_car(head);

	return 0;
}
发布了12 篇原创文章 · 获赞 6 · 访问量 703

猜你喜欢

转载自blog.csdn.net/weixin_45696526/article/details/104831696