1. 程式人生 > >貨物管理(有序插入)

貨物管理(有序插入)

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXSIZE 100

typedef struct
{
char name[20];
int num;
float price;
int storage;
}cargo;

typedef struct node
{
cargo c;
struct node *next;

}Lnode;

void menu()
{
printf(“1:get some stuff \n”);
printf(“2:print the information\n”);
printf(“3:search by number\n”);
printf(“4:input cargo\n”);
printf(“5:output cargo\n”);
}

void getstuff(Lnode *head)
{
int x,i;
printf(“how many stuff you want to store\n”);
scanf("%d",&x);
for(i=0;i<x;i++)
{
Lnode p;
p=(Lnode
)malloc(sizeof(Lnode));
printf(“the information(name,number,price,storage) \n”);
scanf("%s %d %f %d",&p->c.name,&p->c.num,&p->c.price,&p->c.storage);
p->next=head->next;
head->next=p;
}

}

void output_information(Lnode *head)
{
head=head->next;
while(head!=NULL)
{
printf("%s %d %.1f %d\n",head->c.name,head->c.num,head->c.price,head->c.storage);
head=head->next;
}

}

Lnode* search_bynum(int n,Lnode*head)
{
Lnode *p=head->next;
while(p!=NULL&&p->c.num!=n)
p=p->next;
return p;

}

void input_(Lnode *head)
{
int n;
int storage;
printf(“print the number\n”);
scanf("%d",&n);
if(search_bynum(n,head)==NULL)
{
Lnode *p=head,s;
char name[20];
float price;
printf(“print name,price,storage\n”);
scanf("%s %f %d",&name,&price,&storage);
s=(Lnode
)malloc(sizeof(Lnode));
strcpy(s->c.name,name);
s->c.num=n;
s->c.price=price;
s->c.storage=storage;
while(p->next!=NULL&&p->next->c.num<n)
p=p->next;
s->next=p->next;
p->next=s;

}
else
{

    Lnode *q;
    q=search_bynum(n,head);
    printf("print the storage\n");
    scanf("%d",&storage);
    q->c.storage+=storage;

}

}

void output_(Lnode *head)
{
int storage,n;
printf(“the number you wan to output\n”);
scanf("%d",&n);
Lnode *p;
if(search_bynum(n,head)==NULL)
printf(“sorry\n”);
else
{
printf(“how many do you want \n”);
scanf("%d",&storage);
p=search_bynum(n,head);

    if(storage<p->c.storage)
        p->c.storage-=storage;
    else
        printf("sorry,we don't have enough\n");
}

}

int main()
{
Lnode *head;
head=(Lnode *)malloc(sizeof(Lnode));
head->next=NULL;
int f=1;
while(f)
{
menu();
int x;
scanf("%d",&x);
switch(x)
{
case 1:getstuff(head);break;
case 2:output_information(head);break;
case 3:
printf(“print the number of the cargo\n”);
scanf("%d",&x);
search_bynum(x,head);
break;
case 4:input_(head);break;
case 5:output_(head);break;
default: f=0;

    }



}



return 0;

}