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 14:08:10 浏览: 115
C语言变长数组 struct中char data[0]的用法详解
5星 · 资源好评率100%
可以对该函数进行优化,提高其执行效率和代码质量。具体优化方法如下:
1. 减少重复输入。可以在函数开始时,先让用户选择查找方式,并根据不同的查找方式提示用户输入相应的信息,以避免用户重复输入信息。
2. 使用指针传参。可以将链表的头指针作为参数传入函数中,以便函数能够修改链表节点的值。
3. 简化逻辑结构。可以将两种查找方式分别写成两个函数,以减少函数的复杂度。
4. 增加错误处理机制。可以在函数中加入错误处理机制,以便在出现错误时能够及时提示用户并退出函数。
优化后的代码如下:
```
struct chexinxi* chaxun_chexinghao(struct chexinxi* head) {
char chexinghao[N];
char chexinghao1[N];
char chepaihao[N];
char chepaihao1[N];
int m;
printf("\t\t\t\t请输入车型号:");
scanf("%s", chexinghao);
struct chexinxi* p = head;
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);
}
return head;
}
p = p->next;
}
printf("\t\t\t\t未找到该车型号的车辆信息!\n");
return head;
}
struct chexinxi* chaxun_chepaihao(struct chexinxi* head) {
char chepaihao[N];
printf("\t\t\t\t请输入车牌号:");
scanf("%s", chepaihao);
struct chexinxi* p = head;
while (p != NULL) {
if (strcmp(chepaihao, p->chepaihao) == 0) {
printf("\t\t\t\t车型号:%s 车牌号:%s\n", p->chexinghao, p->chepaihao);
return head;
}
p = p->next;
}
printf("\t\t\t\t未找到该车牌号的车辆信息!\n");
return head;
}
struct chexinxi* chazhao(struct chexinxi* head) {
int n;
printf("\t\t\t\t请选择查找方式(1.车型号,2.车牌号):");
scanf("%d", &n);
if (n == 1) {
head = chaxun_chexinghao(head);
}
else if (n == 2) {
head = chaxun_chepaihao(head);
}
else {
printf("\t\t\t\t输入错误!\n");
}
return head;
}
```
优化后的代码结构更加清晰,易于阅读和维护,并且能够提高程序的执行效率和代码质量。
阅读全文