#include<stdio.h>
#include<stdlib.h>
# include<windows.h>
# include<string.h>
#include<time.h>
#include<conio.h>
#define format1 "%d %s %s %d %lf %d %d %d %s %s\n"
#define format2 "%d%s%s%d%lf%d%d%d%s%s\n"
#define format3 "%-6d\t%-6s\t%-6s\t%-6d\t%-6.2lf\t%-6d%-2d %-2d\t%-6s\t%-6s\n"
#define data1 stock[i].number,stock[i].name,stock[i].cate,stock[i].amount,stock[i].price,stock[i].year,stock[i].month,stock[i].day, stock[i].guiji,stock[i].danwei
#define data2 &stock[i].number,&stock[i].name,&stock[i].cate,&stock[i].amount,&stock[i].price,&stock[i].year,&stock[i].month,&stock[i].day,&stock[i].guiji,&stock[i].danwei
struct sku
{
int number; //材料编码
char name[20]; //名称
char cate[20]; //规格
int amount; //库存
int iput; //入库量
int iout ; //出库量
double price; //单价
int year; //年
int month; //月
int day; //日
char guiji[20] ; //归集产品
char danwei[20] ; //用料单位
}stock[100],istock[1000],ostock[1000], swap; //储存食品信息(量) //储存入库信息(量)//储存出库信息(量) //用于排序和统计模块的交换信息
struct TJ //用于月报表
{
int ruhao[100]; //储存出入库信息的材料号
int chushu[100]; //储存出入库信息的材料数
};
struct TJ rukuTJ[12], chukuTJ[12]; //储存1—12月的入库信息 //储存1—12月的出库信息
void tianjia(int n);//添加材料信息
void rk();//添加材料库存
void qc();//减少材料库存
void xg();//修改
void findmenu();//查找信息
void tj();//统计
void sortmenu();//信息排序
void ybb();//月报表
void dc();//显示材料信息
void sc();//删除信息
void endmenu();//退出界面
void xianshi();
void chukudan();
void rukudan();
void menu()
{
printf("* 欢迎使用企业仓库库存管理系统 *\n");
printf("*====================================*\n");
printf("* 1.基本信息管理 *\n");
printf("*====================================*\n");
printf("* 2.原材料出入库管理 *\n");
printf("*====================================*\n");
printf("* 3.原材料领用统计 *\n");
printf("*====================================*\n");
printf("* 4.出入库单查询 *\n");
printf("*====================================*\n");
int x;
while (scanf("%d", &x) != EOF)
{
switch (x)
{
case 1:
printf("* 1.显示原材料编码 *\n");
printf("*====================================*\n");
printf("* 2.原材料输入与追加 *\n");
printf("*====================================*\n");
printf("* 3.删除原材料 *\n");
printf("*====================================*\n");
printf("* 4.修改原材料记录 *\n");
int z;scanf("%d",&z);
switch(z) {
case 1:xianshi();break;
case 2:
printf("请输入添加数量:\n");
int n;
scanf("%d", &n);
tianjia(n);//添加材料信息
break;
case 3:sc();break;
case 4:xg();break;
}
case 2:
printf("* 1.原材料入库 *\n");
printf("*====================================*\n");
printf("* 2.原材料出库 *\n");
printf("*====================================*\n");
int p;scanf("%d",&p);
switch(p){
case 1:
rk();//材料入库
break;
case 2:qc(); //材料出库
break;}
case 3: ybb();break; //统计和月报表
case 4:
printf("* 1.入库单查询 *\n");
printf("*====================================*\n");
printf("* 2.出库单查询 *\n");
printf("*====================================*\n");
printf("* 3.原材料库存查询 *\n");
printf("*====================================*\n");
int g;scanf("%d",&g);
switch(g){
case 1:
rukudan();break;
case 2:
chukudan();break;
case 3:
findmenu();
break;}
break;
case 0:
endmenu(); //退出系统界面
exit(0);
default:printf("没有此选项,请重新输入\n");
}
}
}
int main()
{
FILE* fp;
fp = fopen("ship1.txt", "a+");//先创建一个文件
if (fp == NULL)
{
printf("无法打开文件\n");
return -1;
}
fclose(fp);
menu();//菜单
return 0;
}
int COUNT()//用于统计文件中的数量 ,读取文件信息
{
int i = 0;
FILE* fp;
fp = fopen("ship1.txt", "r");
if (fp == NULL)
{
printf("无法打开文件\n");
return -1;
}
while (!feof(fp))
{
fscanf(fp, format2, data2); //读取文件信息
i++;
}
fclose(fp);
return i;//返回文件中食品信息数量
}
void rk()//添加材料库存
{
system("cls");
printf("===============================\n");
printf("*********入库食品信息**********\n");
printf("===============================\n");
FILE* fp,*fp1;
int imax = COUNT();//先读取文件中原有的材料信息
int i,number,iput;
char ch;
again://与下面goto again对应
printf("请输入要入库的材料号:\n");
scanf("%d", &number);
for (i = 0; i < imax; i++)
{
if (number == stock[i].number)
{
printf("---已找到该材料:\n");
printf("材料号\t名字\t规格\t库存\t价格\t入库时间\t归集产品\t用料单位\n");
printf(format3, data1);
printf("请输入要入库的材料量:\n");
scanf("%d", &iput);
stock[i].amount = stock[i].amount + iput;//原来的材料库存加上现在新加的库存量
istock[i].iput+=iput;//统计入库的食品数量(用于月报表)
printf("请输入入库时间:\n");
daTe:
scanf("%d %d %d", &stock[i].year, &stock[i].month, &stock[i].day);
if((stock[i].month==1||stock[i].month==3||stock[i].month==5||stock[i].month==7||stock[i].month==8||stock[i].month==10||stock[i].month==12)&&(stock[i].day>31||stock[i].day<0))
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==4||stock[i].month==6||stock[i].month==9||stock[i].month==11)&&stock[i].day>30||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==2)&&stock[i].day>29||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
//将材料号、入库量、时间写入入库量的文本文件中
fp1=fopen("rukudan.txt","a+");//用于统计入库数量的文本文件
fprintf(fp1,"%d %d %d %d %d\n",stock[i].number,istock[i].iput,stock[i].year,stock[i].month,stock[i].day);
fclose(fp1);
//将入库后的总信息写入文件中
if ((fp = fopen("ship1.txt", "w")) == NULL)//这里以“w”的方式打开是为了清除之前的材料信息,将现在的新材料信息写入
{
printf("无法打开文件\n");
return;
}
for (i = 0; i < imax; i++)
{
fprintf(fp, format1, data1);
}
fclose(fp);
printf("入库成功,是否(y/Y N/n)继续入库:\n");
getchar();
ch = getchar();
if (ch == 'y' || ch == 'Y')
goto again;
else
{
printf("按任意键返回\n");
_getch();
system("cls");
menu();
}
return;
}
}
printf("找不到该信息,按任意键返回\n");
_getch();
system("cls");
menu();
}
void qc()//食品出库
{
system("cls");
printf("=================================\n");
printf("********材料1出库*****************\n");
FILE* fp,*fp2;
int i = 0, imax = 0, iout = 0, number = 0;
char ch;
imax = COUNT();//食品信息量
if (imax <= 0)
{
printf("请先添加信息\n");
return;
}
again: //go to语句,下面从go to返回到此
printf("请输入出库的材料号:\n");
scanf("%d", &number);
for (i = 0; i < imax; i++)
{
if (number == stock[i].number)
{
printf("材料号\t名字\t类别\t库存\t价格\t入库时间\t归集产品\t单位\n");
printf(format3, data1);
printf("已找到该信息,输入Y/y进行出库,输入0退出,其他重新选择出库食品\n");
getchar();
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
printf("请输入出库数量:\n");
scanf("%d", &iout);
if(iout>stock[i].amount)
{//如果出库的数量大于库存,那么出库的数量就等于库存量
printf("出库失败,库存量不足__返回菜单\n");
_getch();
system("cls");
menu();
//iout=stock[i].amount;
//printf("库存少于出库量,现库存为0\n");
}
else
{
stock[i].amount = stock[i].amount - iout;
ostock[i].iout=0;
ostock[i].iout+=iout;
}
printf("请输出出库时间:\n");
daTe:
scanf("%d %d %d", &ostock[i].year, &ostock[i].month, &ostock[i].day);
if((ostock[i].month==1||ostock[i].month==3||ostock[i].month==5||ostock[i].month==7||ostock[i].month==8||ostock[i].month==10||ostock[i].month==12)&&(ostock[i].day>31||ostock[i].day<0))
{
printf("出库日期有误,重新输入日期:\n");
goto daTe;
}
if((ostock[i].month==4||ostock[i].month==6||ostock[i].month==9||ostock[i].month==11)&&ostock[i].day>30||ostock[i].day<0)
{
printf("出库日期有误,重新输入日期:\n");
goto daTe;
}
if((ostock[i].month==2)&&ostock[i].day>29||ostock[i].day<0)
{
printf("出库日期有误,重新输入日期:\n");
goto daTe;
}
//把出库量、时间、食品号写入文件中
fp2=fopen("chukudan.txt","a+");
fprintf(fp2,"%d %d %d %d %d\n",stock[i].number,ostock[i].iout,ostock[i].year,ostock[i].month,ostock[i].day);
fclose(fp2);
//把出库后的总信息写入文件中
if ((fp = fopen("ship1.txt", "w")) == NULL)
{
printf("无法打开文件\n");
return;
}
for (i = 0; i < imax; i++)
fprintf(fp, format1, data1);
fclose(fp);
printf("出库成功,按任意键返回\n");
_getch();
system("cls");
menu();
}
else if(ch=='0')
{
system("cls");
menu();
}
else
{
printf("重新选择:\n");
goto again;//返回至again再进行出库
}
}
}
printf("找不到该信息\n按任意键返回\n");
_getch();
system("cls");
menu();
}
void xg()//修改
{
system("cls");
printf("================================\n");
printf("********材料信息修改************\n");
FILE* fp= fopen("ship1.txt", "r");
if (!fp)
{
printf("打开失败!\n");
return ;
}
int i = 0, number,imax = 0,p=0;
imax = COUNT(); //读取食品信息的文件
printf("请输入你要修改食品的食品号:\n");
scanf("%d", &number);
for (i = 0; i < imax; i++)
{
if (number == stock[i].number)
{
printf("已找到该信息\n");
printf("材料号\t名字\t规格\t库存\t价格\t入库时间\t归集产品\t用料单位\n");
printf(format3, data1);
printf("请选择要修改的信息:\n");
printf("******** 1.库存 *******\n");
printf("******** 2.单价 *******\n");
printf("******* 3.入库时间 *******\n");
printf("******* 4.确认修改 *******\n");
printf("**************0.返回菜单***************\n");
while (scanf("%d", &p) != EOF)
{
switch (p)
{
case 1:printf("请输入新的库存量:\n");
scanf("%d", &stock[i].amount);
break;
case 2:
printf("请输入新的单价:\n");
scanf("%lf", &stock[i].price);
break;
case 3:
printf("请输入新的入库时间:\n");
daTe:
scanf("%d %d %d", &stock[i].year, &stock[i].month, &stock[i].day);
if((stock[i].month==1||stock[i].month==3||stock[i].month==5||stock[i].month==7||stock[i].month==8||stock[i].month==10||stock[i].month==12)&&(stock[i].day>31||stock[i].day<0))
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==4||stock[i].month==6||stock[i].month==9||stock[i].month==11)&&stock[i].day>30||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==2)&&stock[i].day>29||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
break;
case 4:
if ((fp = fopen("ship1.txt", "w")) == NULL)
{
printf("无法打开文件\n");
return;
}
for (i = 0; i < imax; i++)
{
fprintf(fp, format1, data1);//打印在文件里面
}
fclose(fp);
printf("修改成功\n");
break;
case 0:system("cls");
menu();
break;
default:printf("输入格式错误,请重新选择\n");
}
printf("请选择修改项目:\n");
}
return;
}
}
printf("找不到该信息---按任意键继续\n");
_getch();
system("cls");
menu();
}
void findmenu()//查找信息
{
system("cls");//清屏
printf("================================\n");
printf("* 1.材料号查询 *\n");
int i = 0,n,imax=0,number,flag=0;
imax =COUNT();
if (imax <= 0)
{
printf("请先添加信息\n");
return;
}
printf("-------请输入材料号:\n");
scanf("%d", &number);
for (i = 0; i < imax; i++)
if (number == stock[i].number)
{
flag = 1;
printf("材料号\t名称\t规格 库存量 单价\t入库时间 归集产品 单位\n");
printf(format3, data1);
printf("按任意键继续\n");
_getch();
system("cls");
menu();
}
if (flag == 0)
{ printf("* 没有该食品,重新选择查询方式 *\n");
system("cls"); menu();}
}
void tj()//统计
{
system("cls");
printf("================================\n");
printf("*********材料信息统计***********\n");
printf("================================\n");
printf("\n");
int imax = 0, i = 0, count = 0;
imax = COUNT();
for (i = 0; i < imax; i++)
{
count += stock[i].amount;
}
int average_count = count / imax;
printf("* 总材料平均库存量:%d\n",average_count);
printf("**********************\n");
int j = 0,x;
printf("* 大于平均库存的材料降序排序: *\n");
printf("=====================================\n");
for (i = 0; i < imax; i++)
{
if (stock[i].amount > average_count)
{//把大于平均库存的食品排于前面
swap = stock[j];
stock[j] = stock[i];
stock[i] = swap;
j++;
}
}
for (i = 0; i < j - 1; i++)
for (x = i + 1; x< j; x++)
{
if (stock[i].amount < stock[x].amount)
{//把大于平均库存的食品按升序排序
swap = stock[i];
stock[i] = stock[j];
stock[j] = swap;
}
}
printf("材料号\t名称\t规格 库存量 单价\t入库时间 归集产品 单位 \n");
for (i = 0; i < j; i++)
printf(format3, data1);
printf("\n* 库存量最少的食品(最多10件): *\n");
printf("=======================================\n");
for (i = 0; i < imax - 1; i++)
for (j = i + 1; j < imax; j++)
{
if (stock[i].amount > stock[j].amount)
{//罗列出库存量最少的10种商品,且按升序排序
swap = stock[i];
stock[i] = stock[j];
stock[j] = swap;
}
}
printf("材料号\t名称\t规格 库存量 单价\t入库时间 归集产品 单位\n");
if (imax > 10)//食品量大于10,就只打印10个信息
{
for (i = 0; i < 10; i++)
printf(format3, data1);
}
else//不大于10就全部打印
{
for (i = 0; i < imax; i++)
printf(format3, data1);
}
printf("****按任意键返回****\n");
_getch();
system("cls");
menu();
}
void ybb()//月报表
{
system("cls");
printf("================================\n");
printf("*********月报表******************\n");
FILE *fp3,*fp4;
int imax = COUNT();
//读取出库信息量
int k=0,outcount=0;
int i=0 , j=0,flag ;
fp4=fopen("chukudan.txt","r");//读取出库单文件信息
while(fscanf(fp4,"%d %d %d %d %d",&ostock[i].number,&ostock[i].iout,&ostock[i].year,&ostock[i].month,&ostock[i].day)!=EOF)
{
i++;
outcount++;//统计出库的信息量
}
//将出库月、量读入到chukuTJ结构体中
for(i=0;i<12;i++)
{
k=0;
for(j=0;j<outcount;j++)//
if(ostock[j].month-1==i)
{
chukuTJ[i].ruhao[k]=ostock[j].number;
chukuTJ[i].chushu[k]=ostock[j].iout;
k++;
}
}
fclose(fp4);
int chukucount=0;//计数每月同材料出库量
//打印出库信息
printf("出库月报表:\n");
for(i=0;i<12;i++)//1~12月
{
printf("----------%d月出库月报表:----------\n",i+1);
for(j=1;j<=100;j++)//材料号为1~100
{
flag=0,chukucount=0;
for(k=0;k<100;k++)//每个月中储存的材料信息量(最多100)
if(chukuTJ[i].ruhao[k]==j)
{
chukucount+=chukuTJ[i].chushu[k];
flag=1;
}
if(flag==1)
printf("* 材料号为%d的出库量为;%d *\n",j,chukucount);
}
}
//入库月报表
int incount=0;//入库信息量
i=0;
fp3=fopen("rukudan.txt","r");
while((fscanf(fp3,"%d %d %d %d %d",&istock[i].number,&istock[i].iput,&stock[i].year,&istock[i].month,&istock[i].day))!=EOF)
{
incount++;
i++;
}
//将入库月、量转移到rukuTJ结构体中
for(i=0;i<12;i++)
{
k=0;
for(j=0;j<incount;j++)
if(istock[j].month-1==i)
{
rukuTJ[i].ruhao[k]=istock[j].number;
rukuTJ[i].chushu[k]=istock[j].iput;
k++;
}
}
int rukucount=0;//入库量
fclose(fp4);
//打印入库信息
printf("\n入库月报表:\n");
for(i=0;i<12;i++)
{
printf("----------%d月入库月报表:----------\n",i+1);
for(j=1;j<=100;j++)//食品号为1~100
{
flag=0,rukucount=0;
for(k=0;k<100;k++)
if(rukuTJ[i].ruhao[k]==j)
{
rukucount+=rukuTJ[i].chushu[k];
flag=1;
}
if(flag==1)
printf("* 材料号为%d的出库量为;%d *\n",j,rukucount);
}
}
fclose(fp3);
printf("按任意键继续_ _ _\n");
_getch();
system("cls");
menu();
}
void sc()
{
system("cls");
printf("*******************************\n");
printf("**********删除材料信息*********\n");
printf("*******************************\n");
FILE* fp;
int i = 0,number,index = 0;
int imax = COUNT();
char ch;
again:
printf("* 请输入要删除材料的材料号 *\n");
scanf("%d", &number);
for (i = 0; i < imax; i++)
{
if (number == stock[i].number)
{
index = i;
printf("材料号\t名字\t类别\t库存\t价格\t入库时间 归集产品 单位\n");
printf(format3,data1);
printf("找到了所要删除的信息\n你是否(y/Y N/n)要删除,输入0返回主菜单\n");
getchar();
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
for (i = 0; i < imax; i++)
{
if (i < index)
stock[i].number = stock[i].number;
else
stock[i] = stock[i + 1];
}
fp = fopen("ship1.txt", "w");
for (i = 0; i < imax - 1; i++)
{
fprintf(fp, format1, data1);
}
fclose(fp);
printf("删除成功- - -按任意键返回\n");
_getch();
system("cls");
menu();
}
else if (ch == '0')
{
printf("按任意键返回_ _ _\n");
_getch();
system("cls");
menu();
}
else
{
printf("* 请重新输入删除的信息 *\n");
goto again;
}
return;
break;
}
}
printf("没有此信息- - -按任意键返回上一级\n");
_getch();
system("cls");
menu();
}
void tianjia(int n)
{
system("cls");
printf("=================================\n");
printf("*********添加材料信息************\n");
printf("请写入信息\n");
FILE* fp;
fp = fopen("ship1.txt", "a+");
int i;
for (i = 0; i < n; i++)
{
printf("请输入材料号:\n");
scanf("%d", &stock[i].number);
printf("请输入名称:\n");
scanf("%s", &stock[i].name);
printf("请输入材料规格:\n");
scanf("%s", &stock[i].cate);
printf("请输入库存:\n");
scanf("%d", &stock[i].amount);
printf("请输入材料单价:\n");
scanf("%lf", &stock[i].price);
printf("请输入日期 年/月/日:\n");
daTe:
scanf("%d %d %d", &stock[i].year, &stock[i].month, &stock[i].day);
if((stock[i].month==1||stock[i].month==3||stock[i].month==5||stock[i].month==7||stock[i].month==8||stock[i].month==10||stock[i].month==12)&&(stock[i].day>=31||stock[i].day<0))
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==4||stock[i].month==6||stock[i].month==9||stock[i].month==11)&&stock[i].day>=30||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
if((stock[i].month==2)&&stock[i].day>=29||stock[i].day<0)
{
printf("入库日期有误,重新输入日期:\n");
goto daTe;
}
printf("请输入归集产品:\n");
scanf("%s", &stock[i].guiji);
printf("请输入用料单位:\n");
scanf("%s", &stock[i].danwei);
fprintf(fp,format1,data1);
}
fclose(fp);
printf("添加成功\n按任意键继续\n");
_getch();
system("cls");
menu();
}
void xianshi(){
int imax = COUNT();//食品信息量
if(imax==0){
printf("没有任何材料哦宝宝");
system("cls"); menu();
}
else{
for(int i=0;i<imax;i++){
printf("%s\t",stock[i].name);
printf("%d\n",stock[i].number);
}
}
}
void rukudan(){
printf("请问你要查询的是什么材料入库信息");
int bianma;
scanf("%d",&bianma) ;
FILE *fp3;
int incount=0;//入库信息量
int i=0;
fp3=fopen("rukudan.txt","r");
while((fscanf(fp3,"%d %d %d %d %d",&istock[i].number,&istock[i].iput,&stock[i].year,&istock[i].month,&istock[i].day))!=EOF)
{
if(istock[i].number==bianma){
printf("%d %d %d %d\n",istock[i].iput,stock[i].year,istock[i].month,istock[i].day);
}
i++;
}
printf("按任意键继续\n");
_getch();
system("cls");
menu();
}
void chukudan(){
printf("请问你要查询的是什么材料出库信息");
int bianma;
- scanf("%d",&bianma) ;
FILE *fp4;
int incount=0;//入库信息量
int i=0;
fp4=fopen("rukudan.txt","r");
while((fscanf(fp4,"%d %d %d %d %d",&ostock[i].number,&ostock[i].iout,&ostock[i].year,&ostock[i].month,&ostock[i].day))!=EOF)
{
if(ostock[i].number==bianma){
printf("%d %d %d %d",ostock[i].iput,ostock[i].year,ostock[i].month,ostock[i].day);
}
i++;
}
printf("按任意键继续\n");
_getch();
system("cls");
menu();
}
void endmenu()
{
system("cls");
printf("================================\n");
printf("*********感 谢 使 用*********\n");
printf("================================\n");
}