int ListInsert( linklist &head)
{
system("cls");
char ch[2];
linklist *r,*s;
FILE *fp;
r=head;
s=LEN;
printf("是否添加学生成绩信息(Y/N):");
scanf("%s",ch);
printf("请输入商品的id号:");
scanf("%s",s->id);
while(strcmp(ch,"Y")==0 || strcmp(ch,"y")==0)
{
system("cls");
if((fp=fopen("data.txt","r"))==NULL)
{
printf("打开文件失败,可能是此文件不存在\n");
return false;
}
if(head->next==NULL)
{
linklist *p,*k;
k=p;
while(!feof(fp))
{
p=LEN;
fread(p,sizeof(linklist),1,fp); //将数据读入新链
r->next=p;
r=p;
p=p->next;
r->next=NULL;
}
}
while(r->next!=0)
{ r=r->next;
if(strcmp(r->id,s->id)==0)
{
printf("该商品已经存在\n");
return false;
}
}
system("cls");
printf("请输入商品条码号:");
scanf("%s",s->id);
s->id[4]='\0';
printf("请输入商品名称:");
scanf("%s",s->sname);
s->sname[14]='\0';
printf("请输入供应商:");
scanf("%s",s->sup);
s->sup[17]='\0';
printf("请输入进货价:");
scanf("%f",&s->price1);
printf("请输入销售价:");
scanf("%f",&s->price2);
printf("请输入进货数量:");
scanf("%",&s->num);
fwrite(s,1,sizeof(linklist),fp);
r->next=s;
r=s;
fclose(fp);
printf("保存成功\n");
system("cls");
printf("是否继续添加(Y/N):");
scanf("%s",ch);
}
r->next=NULL;
}
/---------------------显示信息----------------------*/
void view(int n)
{
FILE *fp;
int m=0;
linklist *head,*r,*p;
if(n==0)
return;
system("cls");
if((fp=fopen("data.txt","r"))==NULL)
{
printf("打开文件失败,可能是此文件不存在\n");
return;
}
head=LEN;
r=head;
while(!feof(fp))
{
p=LEN;
fread(p,sizeof(linklist),1,fp); //将数据读入新链
r->next=p;
r=p;
p=p->next;
r->next=NULL;
m++;
}
fclose(fp);
if(m==0)
{
printf("目前尚未添加记录\n");
return;
}
PRINTF;
p=head->next;
while(p)
{
printf("%s%s%s",p->id,p->sname,p->sup);
printf("%f%f%d",p->price1,p->price2,p->num);
printf("\n");
p=p->next;
}
}