1. 写在前面
大部分的函数其实都是差不多的,题目和大一C语言大作业差不多,代码不是源代码。
2. 题目要求
作业:链表的维护与文件形式的保存
要求
1 )用链表结构的有序表表示某商场家电的库存模型 ;
2 )当有提货或进货时需要对该链表进行维护 ;
3 )每个工作日结束之后,将该链表中的数据以文件形式保存,每日开始营业之前,需将以文件形式保存的数据恢复成链表结构的有序表。
4 )链表结点的数据域包括家电名称、品牌、单价和数量,以单价的升序体现链表的有序性。
5 )程序功能包括:创建表、营业开始(读入文件恢复链表数据)、进货(插入)、提货(更新或删除)、查询信息、更新信息、营业结束(链表数据存入文件)等。
3. 代码
3.1 结构体和函数
typedef struct apply
{
char name[10];
char brand[10];
int cost;
int number;
} APP;
typedef struct link
{
APP info;
struct link *next;
} Table;
int Menu(); //菜单
Table *Read(Table *head); //读取文件
Table *Create(Table *head); //新建家电类型
void Display(Table *head); //显示全部
Table *Add(Table *head); //进货
Table *Minus(Table *head); //出货
void *Inquire(Table *head); //查询
void *Save(Table *head); //结束
void DeleteMemory(Table *head); //释放内存
3.2 主函数
void main()
{
Table *head=NULL;
head=Read(head);
while(1)
{
ch=Menu();
switch(ch)
{
case 1:
head=Create(head);
break;
case 2:
head=Add(head);
break;
case 3:
head=Minus(head);
break;
case 4:
Inquire(head);
break;
case 5:
Display(head);
break;
case 6:
Save(head);
break;
default:
break;
}
if(ch==6)
break;
}
DeleteMemory(head);
}
3.3 菜单Menu
int Menu()
{
printf("1.新增家电产品\n");
printf("2.进货\n");
printf("3.提货\n");
printf("4.查询\n");
printf("5.显示全部\n");
printf("6.营业结束\n");
scanf("%d", &ch);
getchar();
return ch;
}
3.4 读取文件
Table *Read(Table *head)
{
FILE *fp;
Table temp;
fp=fopen("文件路径", "r");
Table *pr=head;
Table *p=NULL;
while(fread(&temp, sizeof(Table), 1, fp)!=0)
{
pr=head;
p=(Table *)malloc(sizeof(Table));
if(head==NULL)
head=p;
else
{
while(pr->next!=NULL)
pr=pr->next;
pr->next=p;
}
strcpy(p->info.name, temp.info.name);
strcpy(p->info.brand, temp.info.brand);
p->info.cost=temp.info.cost;
p->info.number=temp.info.number;
p->next=NULL;
}
fclose(fp);
return head;
}
有关文件读写的部分在大一没做重点,写作业的时候复习一下,以后还有。
3.5 Create()
Table *Create(Table *head)
{
Table *p=NULL;
Table *pr=head;
Table *pt=NULL;
Table temp;
p=(Table *)malloc(sizeof(Table));
p->next=NULL;
gets(temp.info.name);
gets(temp.info.brand);
scanf(temp.info.cost);
scanf(temp.info.number);
strcpy(p->info.name, temp.info.name);
strcpy(p->info.brand, temp.info.brand);
p->info.cost=temp.info.cost;
p->info.number=temp.info.number;
if(head==NULL)
head=p;
else
{
while(temp.info.cost>pr->info.cost && pr->next!=NULL)
{
pt=pr;
pr=pr->next;
}
if(pr->info.cost>=temp.info.cost)
{
if(pr==head)
{
p->next=head;
head=p;
}
else
{
pr=pt;
p->next=pr->next;
pr->next=p;
}
}
else
pr->next=p;
}
return head;
}
3.6 Add()
Table *Add(Table *head)
{
Table *p=head;
Table *pr=head;
if(head==NULL)
return head;
gets(进货家电名称);
gets(进货家电品牌);
while((strcmp(家电名称, p->info.name)!=0 || strcmp(家电品牌, p->info.brand)!=0) && p->next!=NULL)
{
pr=p;
p=p->next;
}
if(strcmp(家电名称, p->info.name)==0 && strcmp(家电品牌, p->info.brand)==0)
{
scanf(进货数量);
getchar();
p->info.number=p->info.number+进货数量;
}
else
printf("没有此种家电!\n");
return head;
}
3.7 Minus()
Table *Minus(Table *head)
{
Table *p=head;
Table *pr=head;
if(head==NULL)
return head;
gets(提货家电名称);
gets(提货家电品牌);
while((strcmp(家电名称, p->info.name)!=0 || strcmp(家电品牌, p->info.brand)!=0) && p->next!=NULL)
{
pr=p;
p=p->next;
}
if(strcmp(家电名称, p->info.name)==0 && strcmp(家电品牌, p->info.brand)==0)
{
scanf(提货数量);
getchar();
if(p->info.number<dec)
return head;
p->info.number=p->info.number-dec;
if(p->info.number==0)
{
if(p==head)
head=p->next;
else
pr->next=p->next;
free(p);
}
}
else
printf("没有此种家电!\n");
return head;
}
3.8 查询函数Inquire(),打印函数Display()和释放内存函数DeleteMemory()
略
3.9 写入文件
void *Save(Table *head)
{
Table *p=head;
FILE *fp;
Table temp;
fp=fopen("文件路径", "w");
while(p!=NULL)
{
strcpy(temp.info.name, p->info.name);
strcpy(temp.info.brand, p->info.brand);
temp.info.cost=p->info.cost;
temp.info.number=p->info.number;
fwrite(&temp, sizeof(Table), 1, fp);
p=p->next;
}
fclose(fp);
}