struct chexinxi *chazhao(struct chexinxi *head) { char chexinghao[N]; char chepaihao[N]; char chexinghao1[N]; char chepaihao1[N]; int n,m; struct chexinxi *p; p=head; printf("\t\t\t\t请选择查找方式(1.车型号,2.车牌号):"); scanf("%d",&n); if(n==1) { printf("\t\t\t\t请输入车型号:"); scanf("%s",chexinghao); while(p!=NULL) { if(strcmp(chexinghao,p->chexinghao)==0) { printf("\t\t\t\t车型号:%s 车牌号:%s\n",p->chexinghao,p->chepaihao); printf("\t\t\t\t是否进行修改(1.是,2.否):"); scanf("%d",&m); if(m==1) { printf("\t\t\t\t车型号:"); scanf("%s",&chexinghao1); strcpy(p->chexinghao,chexinghao1); printf("\t\t\t\t车牌号:"); scanf("%s",&chepaihao1); strcpy(p->chepaihao,chepaihao1); } break; } p=p->next; } if(p==NULL) { printf("\t\t\t\t未找到该车型号的车辆信息!\n"); } } else if(n==2) { printf("\t\t\t\t请输入车编号:"); scanf("%s",chepaihao); while(p!=NULL) { if(strcmp(chepaihao,p->chepaihao)==0) { printf("\t\t\t\t车型号:%s 车牌号:%s\n",p->chexinghao,p->chepaihao); break; } } if(p==NULL) printf("\t\t\t\t未找到该车牌号的车辆信息!\n"); } return head; }优化从文件中查找
时间: 2023-10-30 19:08:10 浏览: 79
C语言变长数组 struct中char data[0]的用法详解
5星 · 资源好评率100%
可以将车辆信息保存到文件中,然后在程序运行时从文件中读取车辆信息,进行查找和修改操作。以下是一个示例代码,用于从文件中读取车辆信息,并实现车辆信息的查找和修改功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
struct chexinxi {
char chexinghao[N];
char chepaihao[N];
struct chexinxi *next;
};
void save(struct chexinxi *head) {
FILE *fp;
struct chexinxi *p;
p = head;
fp = fopen("chexinxi.txt", "w");
if (fp == NULL) {
printf("\t\t\t\t文件打开失败!\n");
exit(1);
}
while (p != NULL) {
fprintf(fp, "%s %s\n", p->chexinghao, p->chepaihao);
p = p->next;
}
fclose(fp);
}
struct chexinxi *load() {
FILE *fp;
struct chexinxi *head, *p, *q;
head = NULL;
fp = fopen("chexinxi.txt", "r");
if (fp == NULL) {
printf("\t\t\t\t文件打开失败!\n");
exit(1);
}
while (!feof(fp)) {
p = (struct chexinxi *)malloc(sizeof(struct chexinxi));
fscanf(fp, "%s %s", p->chexinghao, p->chepaihao);
p->next = NULL;
if (head == NULL) {
head = p;
} else {
q->next = p;
}
q = p;
}
fclose(fp);
return head;
}
struct chexinxi *chazhao(struct chexinxi *head) {
char chexinghao[N];
char chepaihao[N];
char chexinghao1[N];
char chepaihao1[N];
int n, m;
struct chexinxi *p;
p = head;
printf("\t\t\t\t请选择查找方式(1.车型号,2.车牌号):");
scanf("%d", &n);
if (n == 1) {
printf("\t\t\t\t请输入车型号:");
scanf("%s", chexinghao);
while (p != NULL) {
if (strcmp(chexinghao, p->chexinghao) == 0) {
printf("\t\t\t\t车型号:%s 车牌号:%s\n", p->chexinghao, p->chepaihao);
printf("\t\t\t\t是否进行修改(1.是,2.否):");
scanf("%d", &m);
if (m == 1) {
printf("\t\t\t\t车型号:");
scanf("%s", &chexinghao1);
strcpy(p->chexinghao, chexinghao1);
printf("\t\t\t\t车牌号:");
scanf("%s", &chepaihao1);
strcpy(p->chepaihao, chepaihao1);
save(head); // 保存修改后的车辆信息到文件中
}
break;
}
p = p->next;
}
if (p == NULL) {
printf("\t\t\t\t未找到该车型号的车辆信息!\n");
}
} else if (n == 2) {
printf("\t\t\t\t请输入车牌号:");
scanf("%s", chepaihao);
while (p != NULL) {
if (strcmp(chepaihao, p->chepaihao) == 0) {
printf("\t\t\t\t车型号:%s 车牌号:%s\n", p->chexinghao, p->chepaihao);
break;
}
p = p->next;
}
if (p == NULL)
printf("\t\t\t\t未找到该车牌号的车辆信息!\n");
}
return head;
}
int main() {
struct chexinxi *head;
int n;
head = load(); // 从文件中读取车辆信息
while (1) {
printf("\n\t\t\t\t请输入操作代码(1.查找车辆信息,2.退出程序):");
scanf("%d", &n);
if (n == 1) {
head = chazhao(head); // 查找车辆信息
} else if (n == 2) {
save(head); // 退出程序前,保存车辆信息到文件中
break;
}
}
return 0;
}
```
在这个示例代码中,我们通过 `load()` 函数从文件中读取车辆信息,并将其保存到一个链表中。在程序运行时,通过 `chazhao()` 函数实现车辆信息的查找和修改功能。当用户选择修改车辆信息时,我们将修改后的车辆信息保存到文件中,以便下次程序运行时能够读取到最新的车辆信息。在程序退出前,我们通过 `save()` 函数保存车辆信息到文件中。
阅读全文