当前位置:文档之家› 湖北汽车工业学院C语言课程设计——商品销售信息管理系统源代码

湖北汽车工业学院C语言课程设计——商品销售信息管理系统源代码

C语言课程设计商品销售信息管理系统#include<stdio.h>#include<stdlib.h>#include<string.h>#include<malloc.h>struct product //定义商品数据结构{int num; //商品编号char productname[20]; //商品名称float price; //商品售价char place[30]; //商品产地int storage; //商品库存struct product *next;}*head,*p1,*p2; //定义主结构体数组和备用结构体数组及指针struct time //定义时间结构体{int year;int month;int day;};struct sell //定义销售数据结构{int ID; //顾客代码char customername[20]; //顾客姓名int num; //商品编号int count; //销售数量char date[10]; //日期struct sell * next1;}*head1; //定义头指针head,缓冲指针p1,以及尾指针p2/*************************函数申明****************************/ void Enter_productmessage(); //导入商品信息void Creat(); //输入销售记录void Search_num(); //按编号查询销售历史void Search_customername(); //按顾客姓名查询购买历史void Printf_customername(); //显示顾客信息void Search_date(); //计算时间段内销售总额void Save_product(); //保存销售信息void Save_customername(); //保存顾客信息void Exit(); //退出程序/***************************主菜单******************************/ int select(){int c;printf("\t\t************欢迎使用商品销售管理程序**************\n");printf("\t\t T1123-5班胡军一分耕耘一分收获\n");printf("\n--------------------------------------------------------------------------------\n");printf("\n");printf("\t\t********************************************\n");printf("\t\t**********欢迎使用商品销售管理系统**********\n");printf("\t\t** **\n");printf("\t\t** 1:导入商品信息**\n");printf("\t\t** 2:输入销售记录**\n");printf("\t\t** 3:按编号查询销售历史**\n");printf("\t\t** 4: 按顾客姓名查询购买历史**\n");printf("\t\t** 5:计算时段内各销售总额**\n");printf("\t\t** 6:显示顾客信息**\n");printf("\t\t** 7:保存销售信息**\n");printf("\t\t** 8:保存顾客信息**\n");printf("\t\t** 0:退出**\n");printf("\t\t********************************************\n");printf("\n--------------------------------------------------------------------------------\n");printf("\t\t请输入您的选择(0~8的整数): ");scanf("%d",&c);return c;}/********************************主函数***************************/int main(){for(;;){switch(select()){case 1:system("cls");Enter_productmessage();break;case 2:system("cls");Creat();break;case 3:system("cls");Search_num();break;case 4:system("cls");Search_customername();break;case 5:system("cls");Search_date();break;case 6:system("cls");Printf_customername();break;case 7:system("cls");Save_product();break;case 8:system("cls");Save_customername();break;case 0:system("cls");Exit();break;default :{ system("cls");printf("\n\n\n\n\n\n");printf("\t\t*******************************************************\n");printf("\t\t** 输入错误!!!请重新输入您的选择(0~8的整数)**\n");printf("\t\t** 请按Enter键返回!!! **\n");printf("\t\t*******************************************************\n");getchar();getchar();system("cls");}}}return 0;}/****************************************************************/int judgech(char *a) //判断是否为汉字{int i,flag;for(i=0;a[i]!='\0';i++)if(a[i]>=0){flag=0;break;}elseflag=1;return flag;}int judgedate(char date[10]) //判断日期是否合法{int i,year,month,day,s1=0; //日期格式输入正确则s1=1,否则s1=0for(i=0;i<10;i++)if(date[i]=='\0'){s1=0;break;}if(date[4]=='-'&&date[7]=='-'){if(strlen(date)==10){year=1000*(date[0]-48)+100*(date[1]-48)+10*(date[2]-48)+(date[3]-48);//年月日month=10*(date[5]-48)+(date[6]-48);day=10*(date[8]-48)+(date[9]-48);}}if(i==10){if(year%4==0&&month==2&&day>0&&day<=29) s1=1;else if(year%4!=0&&month==2&&day>0&&day<=28) s1=1;elseif((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&d ay>0&&day<=31) s1=1;else if((month==4||month==6||month==9||month==11)&&day>0&&day<=30) s1=1;}return s1;}void file() //读入文件操作{int i=0,m=0,k=1,j=1,c=1,o=1,v=1;FILE *fp;char filename[30];printf("请输入读入文件路径:");rewind(stdin);gets(filename);rewind(stdin);p1=p2=head=(struct product*)malloc(sizeof(struct product));if((fp=fopen(filename,"r+"))==NULL){printf("\t\t********************************************\n");printf("\t\t** 导入信息文件错误!请检查文件路径!!! **\n");printf("\t\t********************************************\n");getchar();system("cls");Enter_productmessage();}printf("********************************商品信息***********************************\n");printf("编号(1-10的整数) 名称(汉字) 价格(数字) 产地(汉字) 库存数量\n");printf("************************************************************************* **\n");while(!feof(fp)){p1=(struct product*)malloc(sizeof(struct product));p2->next=p1;p1->next=NULL;p2=p1;fscanf(fp,"%d%s%f%s%d",&p2->num,p2->productname,&p2->price,p2->place,&p2->storage );while(k==1){if(p2->num>10||p2->num<=0){printf("\t*******************************************************\n");printf("\t\t第%d个商品编码!请重新输入(1-10):",i+1);rewind(stdin);scanf("%d",&p2->num);k=1;}else k=0;}while(j==1){if(judgech(p2->productname)==0){printf("\t\t第%d个商品名称错误!请重新输入(汉字):",i+1);rewind(stdin);scanf("%s",p2->productname);j=1;}else j=0;}while(o==1){if(p2->price<=0){printf("\t\t第%d个商品价格错误!请重新输入价格(>0):",i+1);rewind(stdin);scanf("%f",&p2->price);o=1;}else o=0;}while(c==1){if(judgech(p2->place)==0){printf("\t\t第%d个商品产地格式错误!请重新输入(汉字):",i+1);rewind(stdin);scanf("%s",p2->place);c=0;}else c=0;}while(v==1){if(p2->storage!=1000){printf("\t*******************************************************\n");printf("\t\t第%d个商品数量错误!请输入1000:",i+1);rewind(stdin);scanf("%d",&p2->storage);v=1;}else v=0;}printf("\t%-12d%-15s %-15.2f%-15s%-12d\n",p2->num,p2->productname,p2->price,p2->pla ce,p2->storage);}fclose(fp);}void comp(){FILE *fp;int i=0,n,k=1,m=1,c=1,l=1,d=1;char filename[20],a;printf("\t请输入商品信息保存路径:");rewind(stdin);gets(filename);p1=p2=head=(struct product*)malloc(sizeof(struct product));if((fp=fopen(filename,"a+"))==NULL){printf("\t\t********************************************\n");printf("\t\t** 不能打开文件!请检查文件路径!!! **\n");printf("\t\t********************************************\n");getchar();system("cls");Enter_productmessage();}printf("\n\n\n");printf("\t\t********************************************\n");printf("\t\t** 若输入路径与从文件导入商品信息路径相同!**\n");printf("\t\t** 并已导入十个商品信息,请不要再导入!!! **\n");printf("\t\t** 注意商品编号不要和已导入的编码重复!!! **\n");printf("\t\t********************************************\n");printf("\n\n\n");getchar();system("cls");printf("\t已从文件导入的个数和要输入的个数和不超过十!!!\n\n");printf("\t请输入要导入的销售记录个数:");scanf("%d",&n);if(n>0){do{p1=(struct product*)malloc(sizeof(struct product));p2->next=p1;p1->next=NULL;p2=p1;printf("***********************************商品信息*****************************\n");printf(" 编号(整数) 名称(汉字) 价格(数字) 产地(汉字) 库存数量\n");scanf("%d%s%f%s%d",&p2->num,p2->productname,&p2->price,p2->place,&p2->storage);while(k==1){if(p2->num>10||p2->num<=0){printf("\t*******************************************************\n");printf("\t\t 商品编码!请重新输入(1-10):");rewind(stdin);scanf("%d",&p2->num);k=1;}else k=0;}while(m==1){if(judgech(p2->productname)==0){printf("\t\t商品名称错误!请重新输入(汉字):");rewind(stdin);scanf("%s",p2->productname);m=1;}else m=0;}while(l==1){if(p2->price<=0){printf("\t\t商品价格错误!请重新输入价格(>0):");rewind(stdin);scanf("%f",&p2->price);l=1;}else l=0;}while(c==1){if(judgech(p2->place)==0){printf("\t\t商品产地格式错误!请重新输入(汉字):");rewind(stdin);scanf("%s",p2->place);c=1;}else c=0;}while(d==1){if(p2->storage!=1000){printf("\t*******************************************************\n");printf("\t\t 商品数量错误!请输入1000:");rewind(stdin);scanf("%d",&p2->storage);d=1;}else d=0;}p2=head->next;fprintf(fp,"\n%-6d%-6s%-6.2f%-6s%-6d\n",p2->num,p2->productname,p2->price,p2->place, p2->storage);printf("\n%-6d%-6s%-6.2f%-6s%-6d\n",p2->num,p2->productname,p2->price,p2->place,p2->storage);printf("\t\t是否继续输入信息(Y/N)?\n");getchar();a=getchar();}while(a=='Y'||a=='y');}fclose(fp); //关闭文件夹if(n==0){printf("\t\t不从键盘输入入信息!!!");getchar();getchar();system("cls");}fclose(fp); //关闭文件夹}void Enter_productmessage() //导入销售信息{int a;printf("\t************************************************************\n");printf("\t** 请输入10种产品的信息--编号、名称、价格、产地、数量: **\n");printf("\t** 1:通过文件导入(推荐)! **\n");printf("\t** 2: 通过键盘输入! **\n");printf("\t************************************************************\n");printf("\t 请输入您的选择(1或2): ");scanf("%d",&a);getchar();system("cls");if(a!=1&&a!=2){printf("\t\t输入错误!!!请重新输入您的选择(1或2):");scanf("%d",&a);}switch(a){case 1:system("cls"); file();break;case 2:system("cls"); comp();break;}printf("************************************************************************* \n");printf("\t*******************************************************\n");printf("\t** 数据类信息操作成功!请按Enter键继续!!! **\n");printf("\t*******************************************************\n");getchar();getchar();system("cls");}void Creat() //输入销售信息函数{int i=0,k=1,code;char a;struct sell *p3,*p4;srand(0);code=1+rand()%90;p3=p4=head1=(struct sell *)malloc(sizeof(struct sell));p3=head1;printf("\t\t请输入销售记录:\n"); //提示输入记录printf("--------------------------------------------------------------------------------\n");do{p3=(struct sell *)malloc(sizeof(struct sell));p4->next1=p3;p3->next1=NULL;p4=p3;printf(" 顾客姓名(汉字) 商品编号(1-10的整数) 销售数量(数字) 销售日期(YYYY-MM-DD)\n");printf("--------------------------------------------------------------------------------\n");scanf("%s%d%d%s",p4->customername,&p4->num,&p4->count,p4->date); //输入记录do{if(judgech(p4->customername)==0){printf("\t*******************************************************\n");printf("\t\t顾客名字输入错!请重新输入顾客名字(汉字):");scanf("%s",p4->customername);rewind(stdin);k=1;}elsek=0;}while(k==1);do{if(p4->num>10||p4->num<=0){printf("\t*******************************************************\n");printf("\t\t 商品编码!请重新输入(1-10):");scanf("%d",&p4->num);rewind(stdin);k=1;}else k=0;}while(k==1);do{if(p4->count>1000||p4->count<0){printf("\t*******************************************************\n");printf("\t 销售商品数量错误!请输入(0-1000):");scanf("%d",&p4->count);rewind(stdin);}else k=0;}while(k==1);do{if(judgedate(p4->date)!=1){printf("\t*******************************************************\n");printf("\t 输入日期不合法!请重新输入(YYYY-MM-DD):");scanf("%s",p4->date);rewind(stdin);k=1;}else k=0;}while(k==1);p4->ID=code+i;i++;printf("\n\n");printf("是否继续输入销售记录(Y/N)?\n");getchar();a=getchar();}while(a=='Y'||a=='y');system("cls");printf("\n\n\n\n");printf("\t*******************************************************\n");printf("\t** 销售信息输入成功!按Enter键继续!!! **\n");printf("\t*******************************************************\n");getchar();getchar();system("cls");return;}void Search_num() //根据产品编号查询产品的销售历史{int num,flag=1,i=0;struct sell *p3;struct product *p;printf("\t\t请输入商品编号(1-10的整数):");scanf("%d",&num);getchar();system("cls");printf(" 顾客姓名商品编号销售数量销售总额销售日期\n");printf(" ------------------------------------------------------------------------------\n");p=head;while(p3!=NULL){if(p3->num==num){p=head;while(p!=NULL){if(p->num==num)break;elsep=p->next;}printf(" %-15s%-13d%-13d%-13.2f%-10s\n",p3->customername,p3->num,p3->count,p->pric e*p3->count,p3->date);flag=0;}p3=p3->next1;}if(flag==1){printf("\t\t*******************************************************\n");printf("\t\t** 无此编码商品销售记录!请查实后再输入!!! **\n");printf("\t\t** 请按Enter键返回!!! **\n");printf("\t\t*******************************************************\n");getchar();system("cls");return;}printf("-------------------------------------------------------------------------------\n");printf("\t\t********************************************\n");printf("\t\t** 销售记录查询完毕!请按Enter键返回!!! **\n");printf("\t\t********************************************\n");getchar();getchar();system("cls");return;}void Search_customername() //根据顾客姓名查询购买历史{int k=1,m=0;float a=0;char name[20],b;struct product *p;struct sell *p3;p3=head1;p=head;printf("\t\t********************************************\n");printf("\t\t\t请输入顾客姓名(汉字):");scanf("%s",name);getchar();system("cls");if(head1->next1==NULL){printf("\t\t*******************************************************\n");printf("\t\t** 销售记录为空!请按Enter键返回!!! **\n");printf("\t\t*******************************************************\n");getchar();getchar();system("cls");return ;}do{if(judgech(name)==0){printf("\t*******************************************************\n");printf("\t\t顾客名字输入错!请重新输入顾客名字(汉字):");rewind(stdin);scanf("%s",name);rewind(stdin);k=1;}elsek=0;}while(k==1);if(judgech(name)==1){printf("顾客姓名(汉字) 顾客代码商品编号购买数量购买总额购买日期(YYYY-MM-DD)\n");printf("-----------------------------------------------------------------------------\n");while(m!=1){if(strcmp(name,p3->customername)==0){m=1;while(p!=NULL){if(p3->num==p->num){printf("%5s%15d%10d%10d%15.2f%14s\n",p3->customername,p3->ID,p3->num,p3->count, p3->count*p->price,p3->date);break;}else p=p->next;}}else p3=p3->next1;if(p3==NULL)break;}}if(m==1){printf("------------------------------------------------------------------------------\n");printf("\t*******************************************************\n");printf("\t** 查询完毕!请按Enter键返回!!! **\n");printf("\t*******************************************************\n");getchar();system("cls");return;}if(m==0){printf("\t\t*******************************************************\n");printf("\t\t** 不存在此顾客!!!请按Enter键继续!!! **\n");printf("\t\t*******************************************************\n");printf("\t\t是否记录该顾客销售记录!!\n");printf("\t\t请输入选择(Y/N):");rewind(stdin);b=getchar();if(b=='n'||b=='N'){getchar();system("cls");return;}if(b=='y'||b=='Y')getchar();Creat();}}void Search_date() //计算时间段内各个商品的销售总额{int n=0;float temp=0;char a[10],b[10],c[10];struct sell *p3;struct product *p;p3=head1->next1;p=head->next;getchar();system("cls");if(head1->next1==NULL){printf("\t\t*******************************************************\n");printf("\t\t** 无销售记录!请按Enter键返回!!! **\n");printf("\t\t*******************************************************\n");getchar();system("cls");return ;}printf("\t\t请输入开始日期(YYYY-MM-DD):");gets(a);do{if(judgedate(a)!=1){printf("\t\t********************************************\n");printf("\t\t 输入日期不合法!请重新输入(YYYY-MM-DD):");gets(a);system("cls");n=1;}elsen=0;}while(n==1);printf("\t\t********************************************\n");printf("\t\t 请输入结束日期(YYYY-MM-DD):");gets(b);system("cls");do{if(judgedate(b)!=1){printf("\t\t********************************************\n");printf("\t\t 输入日期不合法,请重新输入(YYYY-MM-DD):");gets(b);system("cls");n=1;}elsen=0;}while(n==1);do{if((strcmp(a,p3->date)<=0)&&(strcmp(b,p3->date)>=0)){n=1;break;}else p3=p3->next1;if(p3==NULL)break;}while(n!=1);while(1){p3=head1->next1;if((strcmp(a,p3->date)>0&&strcmp(b,p3->date)>0)||(strcmp(a,p3->date)<0&&strcmp(b,p3-> date)<0)||(strcmp(a,b)>0)){printf("时间断不包括销售日期!!!");Search_date();getchar();return;}else p3=p3->next1;if(p3==NULL)break;}if(n==1){printf("\t\t销售记录如下:\n");printf("\t\t时间: %s至%s\n",a,b);printf("\t商品编号(1-10的整数) 商品名(汉字) 销售总额\n");printf("\n--------------------------------------------------------------------------------\n");}while(p!=NULL){p3=head1->next1;if(p3->num==p->num){temp+=(int)p3->count*(float)p->price;strcpy(c,p->productname);printf("\t\t%-20d%-20s%.2f\n",p3->num,c,temp);}p=p->next;}printf("\n--------------------------------------------------------------------------------\n");printf("\t\t**********************************************\n");printf("\t\t** 查询完毕!请按Enter键继续!! **\n");printf("\t\t**********************************************\n");getchar();getchar();system("cls");return;}void Printf_customername() //显示所有顾客的姓名{int n=1;struct sell *p3;p3=head1->next1;if(p3!=NULL){printf("\t\t顾客姓名如下:\n"); //用来显示所有顾客的姓名printf("\t\t*******************************************************\n");printf("\t\t\t\t\t姓名:\n");do{printf("\t\t\t\t\t %s\n",p3->customername);p3=p3->next1;}while(p3!=NULL);printf("\t\t*******************************************************\n");printf("\t\t*******************************************************\n");printf("\t\t**顾客姓名查询完毕!请按Enter键继续!!!**\n");printf("\t\t*******************************************************\n");getchar();getchar();system("cls");return;}else{printf("\t\t*******************************************************\n");printf("\t\t**顾客链表为空!!!没有顾客销售记录!!!**\n");printf("\t\t*******************************************************\n");getchar();system("cls");return;}}void Save_product() //保存销售信息记录{FILE *fp;//定义文件指针char filename[30];struct product *p;struct sell *p3;p3=head1->next1;p=head->next;printf("请输入保存路径:\n");rewind(stdin);gets(filename);rewind(stdin);if((fp=fopen(filename,"a+"))==NULL)//打开文件{printf("\t\t*******************************************************\n");printf("\t\t** 保存文件失败!按Enter键返回!!! **\n");printf("\t\t*******************************************************\n");getchar();getchar();system("cls");Save_product();}printf("\n--------------------------------------------------------------------------------\n");printf("\t商品编码商品品名字销售量剩余库存销售日期\n");while(p3!=NULL){if(p3->num==p->num){fprintf(fp,"%-5d%-20s%-8d%-8d%s\n",p3->num,p->productname,p3->count,p->storage-p3-> count,p3->date);printf("\t %-10d%-15s%-10d%-5d%15s\n",p3->num,p->productname,p3->count,p->storage-p3->c ount,p3->date);p3=p3->next1;}elsep=p->next;}printf("\n--------------------------------------------------------------------------------\n");printf("\t*******************************************************\n");printf("\t** 销售信息保存完毕!按Enter键返回!!! **\n");printf("\t*******************************************************\n");getchar();fclose(fp);getchar();system("cls");return;}void Save_customername(){FILE *fp;//定义文件指针char filename[30];struct sell *p3;p3=head1->next1;printf("请输入保存路径:\n");rewind(stdin);gets(filename);rewind(stdin);if((fp=fopen(filename,"a+"))==NULL)//打开文件{printf("\t\t*************************************************\n");printf("\t\t** 顾客信息保存失败!按Enter键返回!!! **\n");printf("\t\t*************************************************\n");getchar();getchar();system("cls");Save_customername();}else{while(p3!=NULL){fprintf(fp," %d %s \n",p3->ID,p3->customername);printf("\t\t*************************************************\n");printf("\t\t\t顾客编码:%d\t\t顾客名字:%s\n",p3->ID,p3->customername);printf("\t\t*************************************************\n");p3=p3->next1;}printf("\t\t*************************************************\n");printf("\t\t** 数据库中顾客信息保存完毕,按Enter键返回!!! **\n");printf("\t\t*************************************************\n");getchar();fclose(fp);getchar();system("cls");return;}}void Exit(){ printf("\n\n\n\n\n\n");printf("\t\t**********************************************\n");printf("\t\t**** 谢谢使用!!!请按Enter键退出!!! ****\n");printf("\t\t**********************************************\n");printf("\n\n");getchar();exit(0);}。

相关主题