<span style="font-family:SimSun;font-size:18px;"><span style="font-family:SimSun;font-size:18px;"> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> //定义一个结构体 Car struct Car { int Numb; //汽车编号 char Name[20];//汽车名字 double Price; //汽车价钱 double Volume;//汽车排量 double Speed; //汽车速度 double FuelTankCapacity;//汽车油箱容量 }; //typedef struct Car Car; //定义一个结构体 链表 struct Node { Car data; //链表的数据域 struct Node * Next; //链表的指针域 }; //定义一个 全局变量 struct Node * Head = NULL; //链表的头插法 void AddHead(Car d) { struct Node * New = (struct Node *)malloc(sizeof(struct Node)); New -> data = d; New ->Next = Head; Head = New; } //链表的尾插法 void AddTail(Car d) { struct Node * New = (struct Node *)malloc(sizeof(struct Node)); struct Node * Tail = Head; New ->data = d; New ->Next = NULL; if(!Tail) { Head = New; return ; } while(Tail->Next) { Tail = Tail ->Next; } Tail ->Next = New; } //汽车信息输出 void Print() { struct Node * p = Head; printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); while(p) { printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); p = p ->Next; } } //输入学生信息 void Input(int i) { char c = '0'; do { Car d; printf("请输入:\n"); printf("汽车编号, 汽车名, 汽车价钱, 汽车排量, 汽车速度, 汽车油箱容量: \n"); scanf("%d%s%lf%lf%lf%lf",&d.Numb,d.Name,&d.Price,&d.Volume,&d.Speed,&d.FuelTankCapacity); switch(i) { case 1: AddHead(d); break; case 2: AddTail(d); break; } Print(); printf("是否要继续添加 [y/n]: \n"); c=getch(); putch(c); putch('\n'); }while(c != 'N' && c != 'n'); } //选择输入汽车信息的方法 void InputMenu() { system("cls"); printf("****** ------ *********\n"); printf(" 1.头插法: \n"); printf(" 2.尾插法: \n"); printf(" 0.返回主菜单: \n"); printf("-----------------------\n"); int i = 0; scanf("%d",&i); if(1 == i || 2 == i) { Input(i); } } //链表的删除 int Remove(int Numb) { struct Node * p =Head , *p1; if(p->data.Numb == Numb) { Head = p->Next; free(p); return 1; } while(p) { if(p->data.Numb == Numb) { p1->Next = p->Next; free(p); return 1; } p1 = p; p = p->Next; } return 0; } //删除汽车信息 void Delete() { char c = '0'; do { int Numb; printf("请输入你要删除的编号\n"); scanf("%d",&Numb); if(Remove(Numb)) { printf("删除成功\n"); } else { printf("删除失败\n"); } Print(); printf("是否要继续删除 [y/n]: \n"); c =getch(); putch(c); putch('\n'); }while(c!='N' && c!='n'); } //链表的修改 int ModifyData(int Numb) { struct Node *p = Head; while(p) { if(p->data.Numb == Numb) { printf("请输入新的汽车名字,汽车价格,汽车速度,汽车排量,汽车油箱容量\n"); char Name[20]; double Price; double Volume; double Speed; double FuelTankCapacity; scanf("%s%lf%lf%lf%lf",Name,&Price,&Volume,&Speed,&FuelTankCapacity); strcpy(p->data.Name,Name); p->data.Price = Price; p->data.Volume = Volume; p->data.Speed = Speed; p->data.FuelTankCapacity = FuelTankCapacity; return 1; } p = p ->Next; } return 0; } //修改汽车信息 void Modify() { char c ='0'; do { int Numb; printf("请输入你要修改的汽车编号\n"); scanf("%d",&Numb); if(ModifyData(Numb)) { printf("修改成功\n"); } else { printf("修改失败\n"); } Print(); printf("是否要继续修改 [y/n]: \n"); c = getch(); putch(c); putch('\n'); }while(c!='N' && c!='n'); } //按汽车编号进行查找 void FindNumb() { struct Node * p =Head; int Numb = 0; printf("请输入你要查找的编号: \n"); scanf("%d",&Numb); while(p) { if(p->data.Numb == Numb) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); break; } else { printf("没有你要查找的汽车编号 \n"); break; } p = p->Next; } } //按汽车名进行查找 void FindName() { struct Node * p = Head; char Name[20]; printf("请输入你要查找的汽车名: \n"); scanf("%s",Name); while(p) { if(strcmp(p->data.Name,Name)==0) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); } else { printf("你要查找的汽车名不存在\n"); break; } p = p ->Next; } } // 按汽车价格区间查找 void FindPrice() { struct Node * p =Head; int Price1,Price2; printf("请输入你要查找的汽车的价格区间\n"); scanf("%d%d",&Price1,&Price2); while(p) { if(p->data.Price>= Price1 && p->data.Price <=Price2) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); } else { printf("你所查找的汽车的价格区间不存在\n"); break; } p = p ->Next; } } //按汽车排量区间查找 void FindVolume() { struct Node * p =Head; int Volume1,Volume2; printf("请输入你要查找的汽车的排量区间\n"); scanf("%d%d",&Volume1,&Volume2); while(p) { if(p->data.Volume>= Volume1 && p->data.Volume<=Volume2) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); } else { printf("你输入的汽车的排量区间不存在\n"); break; } p = p ->Next; } } //按汽车速度区间查找 void FindSpeed() { struct Node * p =Head; int Speed1,Speed2; printf("请输入你要查找的汽车的速度区间\n"); scanf("%d%d",&Speed1,&Speed2); while(p) { if(p->data.Speed>= Speed1 && p->data.Speed<Speed2) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); } else { printf("你输入的汽车的速度区间不存在\n"); break; } p = p->Next; } } //按汽车油箱容量空间查找 void FindFuelTankCapacity() { struct Node *p =Head; int FuelTankCapacity1,FuelTankCapacity2; printf("请输入你要查找的汽车的油箱容量区间\n"); scanf("%d%d",&FuelTankCapacity1,&FuelTankCapacity2); while(p) { if(p->data.FuelTankCapacity>= FuelTankCapacity1 && p->data.FuelTankCapacity<= FuelTankCapacity2) { printf("汽车编号,汽车名,汽车价钱,汽车排量,汽车速度,汽车油箱容量 \n"); printf("%d\t %s\t %.2lf\t %.2lf\t%.2lf\t %.2lf\n",p->data.Numb,p->data.Name,p->data.Price,p->data.Volume,p->data.Speed,p->data.FuelTankCapacity); } else { printf("你输入的汽车的油箱容量区间不存在\n"); break; } } p =p ->Next; } //查找汽车信息 int Find() { system("cls"); printf("**********---------------------**********\n"); printf(" 1.按汽车编号查找 \n"); printf(" 2.按汽车名查找 \n"); printf(" 3.按汽车价格区间查找 \n"); printf(" 4.按汽车排量区间查找 \n"); printf(" 5.按汽车速度区间查找 \n"); printf(" 6.按汽车油箱容量区间查找 \n"); printf(" 0.返回主菜单 \n"); printf("-----------------------------------------\n"); int i=0; scanf("%d",&i); switch(i) { case 1: FindNumb(); break; case 2: FindName(); break; case 3: FindPrice(); break; case 4: FindVolume(); break; case 5: FindSpeed(); break; case 6: FindFuelTankCapacity(); break; default : return i; } system("pause"); return i; } //按汽车编号进行排序 void SortByNumb() { struct Node *p = Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { q = p->Next; m = p; while(q) { if(q->data.Numb < m->data.Numb) { m = q; } q = q ->Next; } if(p != m) { Car t = p->data; p->data = m ->data; m->data = t; } p = p ->Next; } } //按汽车名进行排序 void SortByName() { struct Node * p = Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { q = p->Next; m = p; while(q) { if(strcmp(q->data.Name , m->data.Name) < 0) { m = q; } q = q ->Next; } if(p != m) { Car t = p->data; p->data = m->data; m->data = t; } p = p ->Next; } } //按汽车价格进行排序 void SortByPrice() { struct Node * p =Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { m = p; q = p->Next; while(q) { if(q->data.Price < m->data.Price) { m = q; } q = q ->Next; } if(p!=m) { Car t = p ->data; p->data = m ->data; m->data = t; } p = p->Next; } } //按汽车排量进行排序 void SortByVolume() { struct Node * p= Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { m = p; q = p ->Next; while(q) { if(q->data.Volume<m->data.Volume) { m = q; } q = q ->Next; } if(p != m) { Car t =p->data; p->data = m->data; m->data=t; } p = p -> Next; } } //按汽车速度进行排序 void SortBySpeed() { struct Node * p= Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { m = p; q = p ->Next; while(q) { if(q->data.Speed<m->data.Speed) { m = q; } q = q ->Next; } if(p != m) { Car t =p->data; p->data = m->data; m->data=t; } p = p -> Next; } } //按汽车油箱容量进行排序 void SortByFuelTankCapacity() { struct Node * p= Head , *q , *m; if(!p) { return ; } if(!p->Next) { return ; } while(p) { m = p; q = p ->Next; while(q) { if(q->data.FuelTankCapacity<m->data.FuelTankCapacity) { m = q; } q = q ->Next; } if(p != m) { Car t =p->data; p->data = m->data; m->data=t; } p = p -> Next; } } //对汽车信息进行排序 int Browse() { printf("***********-----------***********\n"); printf(" 1.按编号进行排序 \n"); printf(" 2.按汽车名进行排序 \n"); printf(" 3.按价钱进行排序 \n"); printf(" 4.按排量进行排序 \n"); printf(" 5.按速度进行排序 \n"); printf(" 6.按油箱容量进行排序 \n"); printf(" 0.退回主菜单 \n"); printf("---------------------------------\n"); int i = 0; scanf("%d",&i); switch(i) { case 1: SortByNumb(); break; case 2: SortByName(); break; case 3: SortByPrice(); break; case 4: SortByVolume(); break; case 5: SortBySpeed(); break; case 6: SortByFuelTankCapacity(); break; default : return i; } Print(); system("pause"); return i; } //Welcome 主界面 int Welcome() { system("cls"); system("color 2e"); printf("\n\n\n"); printf("\t************-------------------------**************\n"); printf("\t* 欢迎使用汽车汇管理系统 *\n"); printf("\t---------------------------------------------------\n"); printf("\t* 1.增加汽车信息 *\n"); printf("\t* 2.删除汽车信息 *\n"); printf("\t* 3.修改汽车信息 *\n"); printf("\t* 4.查询汽车信息 *\n"); printf("\t* 5.遍历汽车信息 *\n"); printf("\t* 0.退出管理系统 *\n"); printf("\t---------------------------------------------------\n"); printf("请输入你所选的功能: \n"); int i = 0; scanf("%d",&i); switch(i) { case 1: InputMenu(); //输入信息 system("pause"); break; case 2: Delete(); //删除信息 system("pause"); break; case 3: Modify(); //修改信息 system("pause"); break; case 4: while(Find()) //查找信息 { ; } break; case 5: while(Browse()) //排序 输出 { ; } break; } return i; } //把信息写入到文件 void Save() { FILE * fp = fopen("e:\\2.txt","a+"); Node * p = Head; while(p) { fwrite(p,1,sizeof(Car),fp); p = p ->Next; } fclose(fp); } //把信息从文件中读出 void Load() { FILE * fp =fopen("e:\\2.txt","a+"); Car data; while(1) { int n = fread(&data,1,sizeof(data),fp); if(n<=0) { break; } AddTail(data); } fclose(fp); } // 堆内存 全部回收 void RemoveAll() { struct Node * p = Head , *p1; while(p) { p1 = p; p = p -> Next; free(p1); } } int main(int argc , char * argv[]) { Load(); while(Welcome()) { ; } Save(); RemoveAll(); return 0; }</span></span>