发呆的感觉 发布留言 2008-7-5 14:43 请帮我看看这个程序:销售管理系统。不能正确显示输入数据的问题!我用C做的销售管理系统。还有问题。 请帮我看看这个程序:销售管理系统。不能正确显示输入数据的问题! 题目: 销售管理系统 某公司有四个销售员(编号:1-4),负责销售五种产品(编号:1-5)。每个销售员都将当天出售的每种产品各写一张便条交上来。每张便条包含内容: 1)销售员的代号 2)产品的代号 3)这种产品的当天的销售额 每位销售员每天可能上缴0-5张便条。假设,收集到了上个月的所有便条,编写一个处理系统,读取上个月的销售情况(自己设定),进行如下处理。 1)计算上个月每个人每种产品的销售额。 2)按销售额对销售员进行排序,输出排序结果(销售员代号) 3)统计每种产品的总销售额,对这些产品按从高到底的顺序,输出排序结果(需输出产品的代号和销售额) 4)输出统计报表 销售统计报表 产品代号 销售之和 销售员代号 1 2 3 4 5 1 2 3 4 每种产品之和 总和 5)要有删除,查询,插入操作。 6)要求用到链表; 7)要求有保存和载入功能; 发呆的感觉 发布留言 2008-7-5 14:43 程序如下: #include #include #include /*其它说明*/ #include /*字符串函数*/ #include /*内存操作函数*/ #include /*动态地址分配函数*/
#define LEN sizeof(struct sailer)
struct sailer { int p_num; int num[6]; int sum; struct sailer *next; }; /*函数原型*/ int menu_select(); /*菜单函数*/ struct sailer *creat(); /*创建链表*/ void print(struct sailer *head); /* 显示全部记录*/ void search(struct sailer *head); /*查找记录*/ struct sailer *delect(struct sailer *head); /*删除记录*/ struct sailer *sort1(struct sailer *head); /*排序*/ struct sailer *sort2(struct sailer *head); struct sailer *insert(struct sailer *head); /*插入记录*/ void save(struct sailer *head); /*保存文件*/ struct sailer *load(); /*读文件*/ /*主函数*/ void main() { struct sailer *head=NULL; for(;;) { switch(menu_select()) { case 1:head=creat();break; case 2:print(head);break; case 3:search(head);break; case 4:head=delect(head);break; case 5:head=sort1(head);break; case 6:head=sort2(head);break; case 7:head=insert(head);break; case 8:save(head);break; case 9:head=load(); break; case 10:exit(0); /*如菜单返回值为10则程序结束*/ } } } /*创建链表函数*/ struct sailer *creat() { int i=0,j,k=1; struct sailer *head=NULL,*p1,*p2; p1=p2=head=(struct sailer *)malloc(LEN); for(i=0;i <=4;i++) { p1->p_num=i; for(j=0;j <=5;j++) p1->num[j]=0; p1->sum=0; p1=(struct sailer *)malloc(LEN); p2->next=p1; p2=p1; } p1=head; for(;k==1;) { 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=p1->p_num;/*销售员代号附值给变量i*/ for(j=0;j p1=p1->next; for(i=1;i <6;i++)/*向模块中添加数据*/ p1->num+p2->num); 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=p1->num; 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->p_num;/*将所要删除的销售员号附值给变量i*/ for(j=0;j p1=p1->next; printf("delecting the record.....\n"); for(i=1;i <=5;i++)/*将该输入纸条中的数从相应的链表模块中减去*/ p1->num-p2->num;/*此数组用来存放四个销售员的销售额*/ struct sailer *p1; int i,j,temp; p1=head->next;/*p1指向链表第一个模块*/ for(i=1;i <5;i++)/*通过循环将四个销售员的销售额放入相对应的数组元素中*/ { sort1>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); } } /*比较函数,按照产品销售额进行比较*/
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;/*此循环使链表最后一个模块中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); } } /*保存数据到文件函数*/ 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); }k7785524 发布留言 2008-7-5 15:03 看不懂
 神墓
页: [1] 特别说明:如网页特效代码中有引用图片文件等,请自己下载到本地调试! |