⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新书进库.cpp

📁 由于当时只是学到C语言
💻 CPP
字号:
#include"1.h"

void in()//新书入库,改变入库表,写入文件in.
{buy_list1 *p,*head,*q,*c,*headq;
 int i,count=0,count_all=0,k;
 FILE *fp;
 char goon;
head=p=(buy_list)malloc(sizeof(buy_list1));
p->next=NULL;
while(1) 
{system("cls");printf("\n有新书入库是否继续(y/n)\n");
scanf("%c",&goon);getchar();
if(goon=='y')
{system("cls");
printf("\n请输入要入库书的书名\n\n");
scanf("%s",&p->name);getchar();
printf("\n请输入要入库书的作者\n\n");
scanf("%s",&p->author);getchar();
printf("\n请输入要入库书的出版社\n\n");
scanf("%s",&p->press);getchar();
printf("\n请输入要入库书的数量\n\n");
scanf("%d",&p->num);getchar();

strcpy(p->buyer,"空");
strcpy(p->cla,"空");

p->next=(buy_list)malloc(sizeof(buy_list1));
p=p->next;
count++;//节点数加1
}
else break;
}p->next=NULL;

p=head;

fp=fopen("in.txt","w");
	fwrite(&count,sizeof(int),1,fp);//写节点的数目
    for(i=0;i<count;i++){
		fwrite(p,sizeof(buy_list1),1,fp);
		p=p->next;
	}
fclose(fp);





fp=fopen("in_all.txt","r");
 fread(&count_all,sizeof(int),1,fp);
q=c=(buy_list1 *)malloc(sizeof(buy_list1));
headq=NULL;
for(i=0;i<count_all;i++)
{if(i==0) headq=q;
 else q->next=c;
   q=c;
   c=(buy_list1 *)malloc(sizeof(buy_list1));
   fread(c,sizeof(buy_list1),1,fp);
}
fclose(fp);
q->next=c;
c->next=NULL;
p=head;
while(p)
{
k=1;q=headq->next;
 while(q)
{if(!strcmp(p->name,q->name)&&!strcmp(p->author,q->author)&&!strcmp(p->press,q->press))
 {q->num=q->num+p->num;k=0;break;}
 else q=q->next;
 }//

 if(k)
 {q=headq;
 while(q->next){q=q->next;}
count_all++;	 
 q->next=(buy_list)malloc(sizeof(buy_list1));
 q=q->next;
  q->next=NULL;
  strcpy(q->name,p->name);
  strcpy(q->author,p->author);
  strcpy(q->press,p->press);
   strcpy(q->buyer,"空");
    strcpy(q->cla,"空");
  q->num=p->num;
 }

p=p->next;
}//没有的节点被新建,有的节点数目增加
count_all--; //不知道什么原因记录比真正的多1

/*q=headq->next;//p回到头指针
while(q->next)
{
printf("%s    %s   %s   %s   %s    %d\n",q->cla,q->buyer,q->name,q->author,q->press,q->num);
q=q->next;
}*/

q=headq->next;


//printf("\n\n\n%d",count_all);
fp=fopen("in_all.txt","w");
	fwrite(&count_all,sizeof(int),1,fp);//写节点的数目
    for(i=0;i<count_all;i++){
		fwrite(q,sizeof(buy_list1),1,fp);
		q=q->next;
	}
fclose(fp);

p=head;
q=headq;

//释放内存
while(p->next)
{c=p->next;free(p);p=c;
}

while(q->next)
{c=q->next;free(q);q=c;
}

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -