以前看到有人问过,找出来了,不明白的别问我,问这里的人http://topic.csdn.net/u/20090624/15/28e09190-58a6-4469-b6e1-b0f1eba07a09.html
楼主,你再加工一下吧,那些指定执行哪个函数用switch,还有链表未建立之前,其它函数要设置无法调用.
C/C++ code
代码: 全选
#include
#include
#include
#define LEN sizeof(struct car)
#define NULL 0
struct car
{
int num; //编号
unsigned int vin;//发动机号
unsigned int old;//使用年限
long double price;//原价
long double att;//磨损
long double realprice;//实际车价
unsigned int car_age;//使用车龄
struct car *next;
};
struct car *creat(void)
{ //建立汽车数据
struct car *p1,*p2,*head;
p1=p2=(struct car *)malloc(LEN);
head=NULL;
for(int i=0;i<3;i++)
{//建立一个有10辆汽车信息的链表
printf("pleae input the information of the car(%d):\n",i);
printf("order:\tnum\tvin\told\tprice\tcar_age:\n");
scanf("%d%ld%ld%Lf%ld",&p1->num,&p1->vin,&p1->old,&p1->price,&p1->car_age);
p1->att=p1->old*4;//磨损=使用年限(old)*4
p1->realprice=p1->price-p1->att;//实际车价=原价(price)-磨损(att)
if(i==0)
head=p1;
p2=p1;
p1=(struct car*)malloc(LEN);
p2->next=p1;
}
p2->next=NULL;
return head;
}
void print(struct car *head)
{ //打印函数.
printf("\tnum\tvin\told\tprice\tatt\trealprice\tcar_age\n");
for(struct car *p=head;p!=NULL;p=p->next)
printf("\t%d\t%ld\t%ld\t%.2f\t%.2f\t%.2f\t\t%d\n",p->num,p->vin,p->old,p->price,p->att,p->realprice,p->car_age);
}
void order(struct car *head)
{ //按编号从小至大排序
int num;
unsigned int vin,car_age,old;
long double price,att,realprice;
struct car *p1,*p2;
for(p1=head;p1->next!=NULL;p1=p1->next)
for(p2=p1->next;p2!=NULL;p2=p2->next)
if(p1->num>p2->num)
{
num=p1->num;
p1->num=p2->num;
p2->num=num;
vin=p1->vin;
p1->vin=p2->vin;
p2->vin=vin;
old=p1->old;
p1->old=p2->old;
p2->old=old;
price=p1->price;
p1->price=p2->price;
p2->price=price;
att=p1->att;
p1->att=p2->att;
p2->att=att;
realprice=p1->realprice;
p1->realprice=p2->realprice;
p2->realprice=realprice;
car_age=p1->car_age;
p1->car_age=p2->car_age;
p2->car_age=car_age;
}
}
struct car *insert(struct car *head)
{ //插入一辆汽车的信息
struct car *p;
p=(struct car *)malloc(LEN);
printf("insert a car information:\n");
printf("order:\tnum\tvin\told\tprice\tcar_age:\n");
scanf("%d%ld%ld%Lf%ld",&p->num,&p->vin,&p->old,&p->price,&p->car_age);
p->att=p->old*4;//磨损=使用年限(old)*4
p->realprice=p->price-p->att;//实际车价=原价(price)-磨损(att)
p->next=head;//插入信息结点接到首结点之前
head=p;
order(head);//调用排序
return head;
}
void check(struct car *head)
{ //查找
printf("please input the num of the car:\n");
int num,flag=0;
scanf("%d",&num);
for(struct car *p=head;p!=NULL;p=p->next)
if(p->num==num)
{
printf("found:\n");
printf("\tnum\tvin\told\tprice\tatt\trealprice\tcar_age\n");
printf("\t%d\t%ld\t%ld\t%.2f\t%.2f\t%.2f\t\t%d\n",p->num,p->vin,p->old,p->price,p->att,p->realprice,p->car_age);
flag=1;
}
if(!flag)
printf("Not found!\n");
}
struct car *delete_vin(struct car *head)
{ //删除指定编号
struct car *p1,*p2;
printf("please input the vin of the car(delete):\n");
unsigned int vin;
scanf("%ld",&vin);
for(p2=p1=head;p2!=NULL;p1=p2,p2=p2->next)
if(p2->vin==vin)
if(p2==head)
head=head->next;
else
if(p2->next==NULL)
p1->next=NULL;
else
p1->next=p2->next;
return head;
}
void car_age_first(struct car *head)
{ //显示使用年龄最高的某部车信息.
struct car *p=head,*ptemp=head;;
unsigned int age_max=p->car_age;
for(p=p->next;p!=NULL;p=p->next)
if(p->car_age>age_max)
{
age_max=p->car_age;
ptemp=p;
}
printf("the age of the car is:\n");
printf("\tnum\tvin\told\tprice\tatt\trealprice\tcar_age\n");
printf("\t%d\t%ld\t%ld\t%.2f\t%.2f\t%.2f\t\t%d\n",ptemp->num,ptemp->vin,ptemp->old,ptemp->price,ptemp->att,ptemp->realprice,ptemp->car_age);
}
main()
{
struct car *head;
head=creat();
print(head);
order(head);//调用排序函数
print(head);
car_age_first(head);//使用年龄max
// head=insert(head);//插入一个结点
// print(head);
// check(head);查找某编号
// head=delete_vin(head); //删除特定编号
// print(head);
}