/*菜单选择函数*/
menu_select()
{
int n;
struct dates
{
int da_year;
int da_mon;
int da_day;
};
struct dates d;/* 定义时间结构体*/
getdate(&d);/*读取系统时间把它存放与结构体D中*/
printf("press any key to enter the menu......");/*按任意键进入主菜单*/
getch();
clrscr();/*清屏*/
printf("********************************************************************************\n");
printf("\t\t
welcome to \n");
printf("\n\t\t
the sale system\n");
printf("**********************************MENU******************************************\n");
printf("\t\t\t 1.Enter the record\n");
printf("\t\t\t 2.Print the record\n");
printf("\t\t\t 3.Search record by number\n");
printf("\t\t\t 4.Delect a record\n");
printf("\t\t\t 5.Sort to the record by sailer number\n");
printf("\t\t\t 6.Sort to the record by product number\n");
printf("\t\t\t 7.Insert record to list\n");
printf("\t\t\t 8.Save the file\n");
printf("\t\t\t 9.Load the file\n");
printf("\t\t\t 10.Quit\n");
printf("\n\t\t
made
by
du
Liming.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\%d\%d\n",d.da_year,d.da_mon,d.da_day);
do{
printf("\n\t\t\tEnter your choice(0~10):");
scanf("%d",&n);
}while(n <1 ¦ ¦n>10);
return(n);
}
/*查找记录函数*/
void search (struct sailer *head)
{
int i,j,k,n;
struct sailer *p1;
p1=head;
do
{
printf("please input the sailer num(1~4) and product num(1~5)\n");
do
{
scanf("%d,%d",&i,&j);
if(i>4 ¦ ¦i <1 ¦ ¦j>5 ¦ ¦j <1)
printf("error input ,please input again!");
}while(i>4 ¦ ¦i <1 ¦ ¦j>5 ¦ ¦j <1);
for(k=0;k <i;k++)
p1=p1->next;
printf("the search num is %d",p1->num[j]);
printf("Do you want to search again?(1-yes,0-No)\n");
scanf("%d",&n);
}while(n==1);
}
/*显示记录函数*/
void print(struct sailer *head)
{
struct sailer *p1;
int i,j;
p1=head->next;/*使p1指向链表第二个模块,即第一个销售员的模块*/
printf("\tsailer number\t\tproduct number\t\t");
printf("\t\t\t\t\t\tp1\tp2\tp3\tp4\tp5\tsum\n");
for(i=1;i <5;i++)/*输出销售员代号*/
{
printf("\t\t%d\t",p1->p_num);
for(j=1;j <6;j++)/*输出5种产品销售额*/
{
printf("%d\t",p1->num[j]);
}
printf("%d\n",p1->sum);/*输出每个销售员5种产品的销售额。即每个销售员所对应模块里的sum*/
}
}
/*插入一张纸条中数据的函数*/
struct sailer *insert(struct sailer *head)
{
struct sailer *p1,*p2;
int i,j;
p1=p2=head;/*p1,p2都指向链表头*/
printf("please input the record:\n");
printf("please input the sailer number:\n");
scanf("%d",p1->p_num);/*输入销售员代号*/
for(i=1;i <6;i++)/*用循环依次输入五中产品的销售额*/
{
printf("please input the %d product number:\n",i);
scanf("%d",p1->num[i]);
}
i=p1->p_num;/*销售员代号附值给变量i*/
for(j=0;j <i;j++)/*根据变量i找到所对应销售员的模块*/
p1=p1->next;
for(i=1;i <6;i++)/*向模块中添加数据*/
{
p1->num[i]=p1->num[i]+p2->num[i];
p1->sum=p1->sum+p2->sum;
}
p2->sum=0;
printf("insert successful!");
}
/*删除一张纸条中的数据*/
struct sailer *delect(struct sailer *head)
{
struct sailer *p1,*p2;
int i,j;
p1=p2=head;/*使p1,p2都指向链表中第一个模块*/
p1->sum=0;/*使链表第一个模块中sum为0*/
clrscr();
printf("****************************************************************\n");
printf("\t\t\tplease input the record you want to delect\n");
printf("input the sailer number(1~4)\n");
scanf("%d",p1->p_num);
for(i=1;i <=5;i++)/*循环输入5种产品的销售额,等全部输完后算出5种产品的销售额放在sum中*/
{
printf("please input the %d product number\n",i);
scanf("%d\n",p1->num[i]);
p1->sum=p1->sum+p1->num[i];
}
i=p1->p_num;/*将所要删除的销售员号附值给变量i*/
for(j=0;j <i;j++)/*通过i找到链表中对应该销售员的模块使p1指向它*/
p1=p1->next;
printf("delecting the record.....\n");
for(i=1;i <=5;i++)/*将该输入纸条中的数从相应的链表模块中减去*/
p1->num[i]=p1->num[i]-p2->num[i];
p1->sum=p1->sum-p2->sum;
printf("delect the record successful!\n");
}
/*按照每人的销售额进行比较*/
struct sailer *sort1(struct sailer *head)
{
int sort1[5];/*此数组用来存放四个销售员的销售额*/
struct sailer *p1;
int i,j,temp;
p1=head->next;/*p1指向链表第一个模块*/
for(i=1;i <5;i++)/*通过循环将四个销售员的销售额放入相对应的数组元素中*/
{
sort1[i]=p1->sum;
p1=p1->next;
}
p1=head->next;/*p1重新指向链表第二个模块*/
for(i=1;i <4;i++)/*冒泡法排序*/
for(j=1;j <4-i;j++)
if(sort1[j]>sort1[j+1])
{
temp=sort1[j];
sort1[j]=sort1[j+1];
sort1[j+1]=temp;
}
for(i=1;i <5;i++)/*循环4次,达到将四个数按排好的序列输出的结果*/
{
for(j=1;j <4;j++)/*循环比较出从小到大的销售额所对应的销售员并依次输出销售员代号*/
{
if((p1->sum-sort1[i])==0)
printf("%d\t",j);
}
printf("\n\t");
for(i=1;i <5;i++)/*输出从小到大的销售额*/
printf("%d\t",sort1[i]);
}
}
/*比较函数,按照产品销售额进行比较*/
struct sailer *sort2 (struct sailer *head)
{
int sort2[6];/*定义一个数组,用来存放5种产品销售总和*/
int i,j,temp;
struct sailer *p2;
p2=head;
for(i=1;i <6;i++)
p2=p2->next;/*此循环使P2指向链表最后一个模块*/
for(i=1;i <6;i++)
sort2[i]=p2->num[i];/*此循环使链表最后一个模块中5中产品销售额附值给数组*/
for(i=1;i <5;i++)
for(j=1;j <5-i;j++)
{
if(sort2[j]>sort2[j+1])
{
temp=sort2[j];
sort2[j]=sort2[j+1];
sort2[j+1]=temp;
}/*冒泡法比较大小*/
printf("\t\t\tthe sorted numbers:\n");
printf("\t\t\tnum1\tnum2\tnum3\tnum4\tnum5\t\n\t\t\t");
for(i=1;i <6;i++)
for(j=1;j <6;j++)
if((p2->num[j]-sort2[i])==0)/*通过循环比较最后一个链表和排好序数组中的数,若相等则输出该链表中的数*/
{
printf("%d\t",j);/*依次按照销售额从小到大输出销售员号*/
}
printf("\n\t\t\t");/*为了对齐*/
for(j=1;j <6;j++)
printf("%d\t",sort2[j]);
}
}
/*保存数据到文件函数*/
void save(struct sailer *head)
{
FILE *fp;
/*定义指向文件的指针*/
struct sailer *p;
/* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
/*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return;
/*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head;
/*移动指针从头指针开始*/
while(p)
/*如p不为空*/
{
fwrite(p,LEN,1,fp);
/*写入一条记录*/
p=p->next;
/*指针后移*/
}
fclose(fp);
/*关闭文件*/
printf("Save the file successfully!\n");
}
/* 从文件读数据函数*/
struct sailer *load()
{
struct sailer *p1,*p2,*head=NULL;
/*定义记录指针变量*/
FILE *fp;
/* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
/*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(struct sailer *)malloc(LEN);
/*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1;
/*申请到空间,将其作为头指针*/
while(!feof(fp))
/*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break;
/*如果没读到数据,跳出循环*/
p1->next=(struct sailer *)malloc(LEN);
/*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1;
/*使p2指向刚才p1指向的结点*/
p1=p1->next;
/*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL;
/*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}