声明
此代码使用VS2019编译器进行编译
使用 vc 和 dev-c 的有可能会出现编译警告,需要自己去网上查找相关的编译环境的问题
其次使用vs编译器也可能会报 C4996 错误,请自行搜索 C4996 错误
最后也是最容易忽略的错误,C/C++编译器常使用ASCII码
CSDN上直接复制的代码是UTF-8格式的需要自己手动调(自行搜索)要注意文件读写问题需要跟编译器环境有关系当时编写没考虑其他编译环境
需要自己去搜读取和写入的函数可以自己进行修改本代码已经尝试很多次已确定代码格式无误.报错问题绝大部分与编译器的调制有关
此外
由于这是作者大一时候编写的,不一定有时间及时回答真想问加Q:1403145273并写明
一起学习
由于很多人说我的程序执行失败,我在此解释一下
我的编译环境是VS2019 使用dev和VC的在scanf_s会报错,因为这个函数是VS才有的好像
总之不能使用的时候建议百度
其次 由于不同的编译环境对程序的要求不同,有些需要调整编译器的警告等级才能使用
在执行程序前,需要在当前cpp文件夹内自己手动建立car.txt文本才能运行成功
请看仔细点
初始化信息是在car.txt文件内的,我没有写在程序内所有在运行时会显示没有内容
要写初始化信息自己先运行程序中输入新的信息录入才会更新初始化内容
文件尾的问题也是CSDN在复制长代码的时候会出现的问题,需要分开复制
IntelliSense:此声明没有存储类型或类型说明符
同理
大部分都是出现在IO流的问题,请自行根据自己的编译器进行调整
#include<stdio.h>
#include
#include
typedef struct car;
typedef struct list_car;
typedef struct list_car_sequence;
list_car* rear=NULL;
/* 数据模型 */
struct car {
char type_of_name[10];//车型
char manufacturer[10];//厂商
char model_class[10];//车型级别
char seating_number[10];//座位数
char output_volume[10];//排量
char transmission_case[10];//变速箱
char color[10];//颜色
char prize[10];//价格
};
/* 链表 */
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 ((fscanf(fp, "%s", create->imformation->type_of_name)) != EOF) {
fscanf(fp, "%s""%s""%s""%s""%s""%s""%s", create->imformation->manufacturer, create->imformation->model_class
, create->imformation->seating_number, create->imformation->output_volume, create->imformation->transmission_case
, create->imformation->color, create->imformation->prize);
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));
}
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("打开文件失败");
}
list_car* p = head;
for(;p!=NULL;p=p->next)
fprintf(fp,"%s ""%s ""%s ""%s ""%s ""%s ""%s ""%s", p->imformation->type_of_name, p->imformation->manufacturer, p->imformation->model_class, p->imformation->seating_number, p->imformation->output_volume, p->imformation->transmission_case, p->imformation->color, p->imformation->prize);
printf("写入文件成功!\n");
fclose(fp);
}
/******************************************************//* 修改 *//*************************************************************/
/* 录入车辆信息进 [结构体内] */
void list_car_write(list_car*rear) {
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(p->imformation->type_of_name)
, p->imformation->manufacturer, sizeof(p->imformation->manufacturer), p->imformation->model_class
, sizeof(p->imformation->model_class), p->imformation->seating_number, sizeof(p->imformation->seating_number)
, p->imformation->output_volume, sizeof(p->imformation->output_volume), p->imformation->transmission_case
, sizeof(p->imformation->transmission_case), p->imformation->color, sizeof(p->imformation->color), p->imformation->prize, sizeof(p->imformation->prize));
getchar();
if(rear!=NULL)
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");
char imformation;
imformation = getchar();
printf("请输入修改后内容\n");
switch (imformation)
{
case '1':scanf_s("%s", p->imformation->type_of_name, sizeof(p->imformation->type_of_name));break;
case '2':scanf_s("%s", p->imformation->manufacturer, sizeof(p->imformation->manufacturer));break;
case '3':scanf_s("%s", p->imformation->model_class, sizeof(p->imformation->model_class));break;
case '4':scanf_s("%s", p->imformation->seating_number, sizeof(p->imformation->seating_number));break;
case '5':scanf_s("%s", p->imformation->output_volume, sizeof(p->imformation->output_volume));break;
case '6':scanf_s("%s", p->imformation->transmission_case, sizeof(p->imformation->transmission_case));break;
case '7':scanf_s("%s", p->imformation->color, sizeof(p->imformation->color));break;
case '8':scanf_s("%s", p->imformation->prize, sizeof(p->imformation->prize));break;
}
getchar();
printf("修改成功!\n");
}
/* 删除 [结构体内] 车辆信息 */
void list_car_delete(list_car* head) {
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;
loop:scanf_s("%c", &information, sizeof(information));
getchar();
switch (information) {//结构体里的字符型数组是不能被赋值的,我也不知道为什么.好像是被当成了常量
case '0':t->next = p->next;printf("删除成功!\n");break;
case '1':strcpy(p->imformation->manufacturer, "无信息");break;
case '2':strcpy(p->imformation->model_class, "无信息");break;
case '3':strcpy(p->imformation->seating_number, "无信息");break;
case '4':strcpy(p->imformation->output_volume, "无信息");break;
case '5':strcpy(p->imformation->transmission_case, "无信息");break;
case '6':strcpy(p->imformation->color, "无信息");break;
case '7':strcpy(p->imformation->prize, "无信息");break;
default:printf("无法识别该序号,请重新输入序号");goto loop;
}
}
/******************************************************//* 修改 *//*************************************************************/
/******************************************************//* 输出 *//*************************************************************/
/* 查询 [结构体内] 车辆具体信息 */
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;
list_car_sequence* list_prize_sequence(list_car_sequence * sequence_head, list_car * p);
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->prize) >= prize_down && atof(p->imformation->prize) <= prize_high)
sequence_head = list_prize_sequence(sequence_head, p);
if (sequence_head == NULL) {
printf("该价格范围内无车辆型号:\n");
return;
}
for (;sequence_head != NULL;sequence_head = sequence_head->next,printf("\n"))
printf("%s\t%s\n", sequence_head->name,sequence_head->imformation);
}
void search_other(list_car* head) {
list_car_sequence* list_dissequence(list_car_sequence * dissequence, list_car * p);
list_car* p = head;
list_car_sequence* dissequence_head=NULL;
void list_prize_sequence(list_car_sequence * sequence_head, list_car*p);
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_prize_sequence(list_car_sequence* sequence_head, list_car* p) {
if (sequence_head == NULL) {
sequence_head = (list_car_sequence*)malloc(sizeof(list_car_sequence));
strcpy(sequence_head->imformation, p->imformation->prize);
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->prize)) {
if (t == sequence_head) {
next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
next->next = sequence_head;
strcpy(next->imformation, p->imformation->prize);
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->prize);
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->prize);
strcpy(next->name, p->imformation->type_of_name);
t->next = next;
next->next = NULL;
return x;
}
}
}
list_car_sequence* list_seating_sequence(list_car_sequence* sequence_head, list_car* p) {
if (sequence_head == NULL) {
sequence_head = (list_car_sequence*)malloc(sizeof(list_car_sequence));
strcpy(sequence_head->imformation, p->imformation->seating_number);
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->seating_number)) {
if (t == sequence_head) {
next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
next->next = sequence_head;
strcpy(next->imformation, p->imformation->seating_number);
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->seating_number);
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->seating_number);
strcpy(next->name, p->imformation->type_of_name);
t->next = next;
next->next = NULL;
return x;
}
}
}
list_car_sequence* list_output_sequence(list_car_sequence* sequence_head, list_car* p) {
if (sequence_head == NULL) {
sequence_head = (list_car_sequence*)malloc(sizeof(list_car_sequence));
strcpy(sequence_head->imformation, p->imformation->output_volume);
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->output_volume)) {
if (t == sequence_head) {
next = (list_car_sequence*)malloc(sizeof(list_car_sequence));
next->next = sequence_head;
strcpy(next->imformation, p->imformation->output_volume);
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->output_volume);
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->output_volume);
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_sequence_head=NULL, * seating_sequence_head=NULL, * output_sequence_head=NULL;
if (head == NULL){
printf("头指针为空!\n");
return;
}
else {
printf("车型\t厂商\t\t车型级别\t座位数\t排量\t变速箱\t车身颜色\t价格\n");
printf("_______________________________________________________________________________________\n");
for (; head != NULL; head = head->next) {
printf("%s\t""%s\t""%s\t""%s座\t""%sT\t""%s\t""%s\t\t""%s万\n", head->imformation->type_of_name, head->imformation->manufacturer, head->imformation->model_class, head->imformation->seating_number, head->imformation->output_volume, head->imformation->transmission_case, head->imformation->color, head->imformation->prize);
printf("\n");
prize_sequence_head = list_prize_sequence(prize_sequence_head, head);
seating_sequence_head = list_seating_sequence(seating_sequence_head, head);
output_sequence_head = list_output_sequence(output_sequence_head, head);
}
}
printf("从小到大类型:");
getchar();
char c;
list_car_sequence* p;
printf("1价格 2座位数 3排量 CTRL+Z退出 ");
while (~(c = getchar())) {
switch (c) {
case '1':p = prize_sequence_head;break;
case '2':p = seating_sequence_head;break;
case '3':p = output_sequence_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("%s\t%s\n", p->name, p->imformation);
getchar();
loop: printf("1价格 2座位数 3排量 CTRL+Z退出 ");
}
}
/******************************************************//* 输出 *//*************************************************************/
int main() {
list_car* head = read_list_car();
char x;
printf("1为浏览所有信息 2为查询车辆具体信息 3录入新的车辆 4修改车辆信息 5为删除车辆信息 CTRL+Z为退出\n"
"请选择你想进行的功能:");
while (~(x = getchar())) {
system("cls");
switch (x) {
case '1':export_list(head);break;
case '2':search(head);break;
case '3':list_car_write(rear);break;
case '4':list_car_alter(head);break;
case '5':list_car_delete(head);break;
default:printf("输入有误,请重新输入:\n");getchar();break;
}
system("cls");
printf("1为浏览所有信息 2为查询车辆具体信息 3录入新的车辆 4修改车辆信息 5为删除车辆信息 CTRL+Z为退出\n"
"请选择你想进行的功能:");
}
write_list_car(head);
return 0;
}