struct Product /定义水产品结构体/
{
int iId; /水产品编号/
char acName[15]; /水产品名称/
char acProducer[15]; /水产品生产商/
char acDate[15]; /水产品进货时间/
double dPrice; /水产品价格/
int iAmount; /水产品数量/
};
struct Product astPro[100]; /定义结构体数组/
/*****************************************************************/
void ShowMenu(); /显示主菜单/
void InputProduct(); /水产品入冷库/
void OutputProduct(); /水产品出冷库/
void DeleteProduct(); /删除水产品/
void ModifyProduct(); /修改水产品/
void SearchProduct(); /查询水产品/
int ShowProduct(); /显示水产品/
/*******************************************************************************/
void main()
{
int iItem;
ShowMenu();
scanf("%d", &iItem); /输入菜单项/
while (iItem)
{
switch (iItem)
{
case 1:InputProduct(); break;
case 2:OutputProduct(); break;
case 3:DeleteProduct(); break;
case 4:ModifyProduct(); break;
case 5:SearchProduct(); break;
case 6:ShowProduct(); break;
default:printf("input wrong number"); /错误输入/
}
getch();
ShowMenu(); /执行完功能再次显示菜单功能/
scanf("%d", &iItem);
}
}
//
void ShowMenu()
{
system("cls");
printf("\n\n\n\n\n");
printf(" 欢迎来到永平水产厂!\n");
printf("\t\t|水产品|\n");
printf("\t\t|\t 1. 输入记录 |\n");
printf("\t\t|\t 2. 输出记录 |\n");
printf("\t\t|\t 3. 删除记录 |\n");
printf("\t\t|\t 4. 修改记录 |\n");
printf("\t\t|\t 5. 查询记录 |\n");
printf("\t\t|\t 6. 显示记录 |\n");
printf("\t\t|\t 0. 返回 |\n");
printf("\t\t|***************************|\n\n");
printf("\t\t\t输入0-6:");
}
//
/水产品入冷库/
void InputProduct()
{
int i, iMax = 0; /iMax记录文件中的水产品记录条数/
char cDecide;
char p;
p=&cDecide; /存储用户输入的是否入冷库的判断字符/
FILE fp; /定义文件指针/
iMax = ShowProduct();
if ((fp = fopen("product.txt", "ab")) == NULL) /追加方式打开一个二进制文件/
{
printf("can not open file\n");
return;
}
printf("press y/Y to input:");
getchar(); /把选择1之后输入的回车符取走/
cDecide = getchar();
while (p == 'y' || p== 'Y') /判断是否要录入新信息/
{
printf("Id:"); /输入水产品编号/
scanf("%d", &astPro[iMax].iId);
for (i = 0; i<iMax; i++)
if (astPro[i].iId == astPro[iMax].iId) /若该水产品已存在/
{
printf("the id is existing,press any key to continue!");
getch();
fclose(fp); /关闭文件,结束input操作/
return;
}
printf("Name:"); /输入水产品名称/
scanf("%s", &astPro[iMax].acName);
printf("Producer:"); /输入水产品生产商/
scanf("%s", &astPro[iMax].acProducer);
printf("Date(Example 15-5-1)😊; /输入水产品进货时间/
scanf("%s", &astPro[iMax].acDate);
printf("Price:"); /输入水产品价格/
scanf("%lf", &astPro[iMax].dPrice);
printf("Amount:"); /输入水产品数量/
scanf("%d", &astPro[iMax].iAmount);
if (fwrite(&astPro[iMax], PRODUCT_LEN, 1, fp) != 1) /在文件末尾添加该水产品记录/
{
printf("can not save!\n");
getch();
}
else
{
printf("product Id %d is saved!\n", astPro[iMax].iId);/成功入冷库提示/
iMax++;
}
printf("press y/Y to continue input:");
getchar();
cDecide = getchar(); /判断是否为y/Y,继续循环/
}
fclose(fp); /不再继续录入,关闭文件/
printf("Input is over!\n");
}
/水产品出冷库/
void OutputProduct()
{
FILE fp;
int iId, i, iMax = 0, iOut = 0; /iId表示水产品编号,iOut表示要出冷库的水产品数量/
char cDecide;
charp;
p=&cDecide;
iMax = ShowProduct();
if (iMax <= -1) /若文件不存在,或者没有记录,不能进行出冷库操作/
{
printf("please input first!");
return;
}
printf("please input the id:");
scanf("%d", &iId);
for (i = 0; i < iMax; i++)
{
if (iId == astPro[i].iId)
{
printf("find the product,press y/Y to output:");
getchar();
cDecide = getchar();
if (p == 'y' ||p == 'Y') /判断是否要进行出冷库/
{
printf("input the amount to output:");
scanf("%d", &iOut);
astPro[i].iAmount = astPro[i].iAmount - iOut;
if (astPro[i].iAmount < 0) /要出冷库的数量比实际库存量还小/
{
printf("the amount is less than your input and the amount is 0 now!\n");
astPro[i].iAmount = 0; /出冷库后的库存量置为0/
}
if ((fp = fopen("product.txt", "rb+")) == NULL)
{
printf("can not open file\n");
return;
}
fseek(fp, iPRODUCT_LEN, 0); /文件指针移动到要出冷库的水产品记录位置/
if (fwrite(&astPro[i], PRODUCT_LEN, 1, fp) != 1) /写入该水产品出冷库后的信息/
{
printf("can not save file!\n");
getch();
}
fclose(fp);
printf("output successfully!\n");
ShowProduct(); /显示出冷库后的所有水产品信息/
}
return;
}
}
printf("can not find the product!\n"); /如果没有找到该水产品,提示用户/
}
/删除水产品/
void DeleteProduct()
{
FILE fp;
int i, j, iMax = 0, iId;
int p;
p=&iId;
iMax = ShowProduct();
if (iMax <= -1)
{
printf("please input first!");
return;
}
printf("please input the id to delete:");
scanf("%d", &iId);
for (i = 0; i<iMax; i++)
{
if (p == astPro[i].iId)
{
for (j = i; j < iMax; j++)
astPro[j] = astPro[j + 1];
iMax--;
if ((fp = fopen("product.txt", "wb")) == NULL) /只写方式打开文件,文件存在则先删除并创建一个新文件/
{
printf("can not open file\n");
return;
}
for (j = 0; j<iMax; j++) /将新修改的信息写入指定的磁盘文件中/
if (fwrite(&astPro[j], PRODUCT_LEN, 1, fp) != 1)
{
printf("can not save!");
getch();
}
fclose(fp);
printf("delete successfully!\n");
ShowProduct(); /显示删除后的所有水产品信息/
return;
}
}
printf("can not find the product!\n");
}
void ModifyProduct() /修改水产品函数*/
{
FILE *fp;
int i, iMax = 0, iId;
iMax = ShowProduct();
if (iMax <= -1) /*若文件不存在,或者没有记录,不能进行出库操作*/
{
printf("please input first!");
return;
}
printf("please input the id to modify:");
scanf("%d", &iId);
for (i = 0; i<iMax; i++)
{
if (iId == astPro[i].iId) /*检索记录中是否有要修改的水产品*/
{
printf("find the product, you can modify!\n");
printf("id:");
scanf("%d", &astPro[i].iId);
printf("Name:");
scanf("%s", &astPro[i].acName);
printf("Producer:");
scanf("%s", &astPro[i].acProducer);
printf("Date:");
scanf("%s", &astPro[i].acDate);
printf("Price:");
scanf("%lf", &astPro[i].dPrice);
printf("Amount:");
scanf("%d", &astPro[i].iAmount);
if ((fp = fopen("product.txt", "rb+")) == NULL)
{
printf("can not open\n");
return;
}
fseek(fp, i*PRODUCT_LEN, 0); /*将新修改的信息写入指定的磁盘文件中*/
if (fwrite(&astPro[i], PRODUCT_LEN, 1, fp) != 1)
{
printf("can not save!");
getch();
}
fclose(fp);
printf("modify successful!\n");
ShowProduct(); /*显示修改后的所有水产品信息*/
return;
}
}
printf("can not find information!\n");
}
void SearchProduct() /查找水产品函数/
{
//FILE *fp;
int iId, i, iMax = 0;
char cDecide;
iMax = ShowProduct();
if (iMax <= -1) /*若文件不存在,或者没有记录,不能进行出库操作*/
{
printf("please input first!");
return;
}
printf("please input the id:");
scanf("%d", &iId);
for (i = 0; i<iMax; i++)
if (iId == astPro[i].iId) /*查找输入的编号是否在记录中*/
{
printf("find the product,press y/Y to show:");
getchar();
cDecide = getchar();
if (cDecide == 'y' || cDecide == 'Y')
{
printf("id name producer date price amount\n");
printf(FORMAT, DATA); /*将查找出的结果按指定格式输出*/
return;
}
}
printf("can not find the product"); /*未找到要查找的信息*/
}
int ShowProduct() /显示所有水产品信息/
{
int i, iMax = 0;
FILE fp;
if ((fp = fopen("product.txt", "rb")) == NULL) /只读方式打开一个二进制文件/
{
printf("can not open file\n"); /提示无法打开文件/
return -1;
}
while (!feof(fp))
if (fread(&astPro[iMax], PRODUCT_LEN, 1, fp) == 1)
iMax++;
fclose(fp);
if (iMax == 0) /文件中没有记录时提示用户/
printf("No record in file!\n");
else
{
printf("id name producer date price amount\n");
for (i = 0; i < iMax; i++)
{
printf(FORMAT, DATA); /将信息按指定格式打印*/
}
}
return iMax;
}
卧槽,还可以这样,哈哈哈
库存管理要啥收入支出没有订单吧
– naerg 4年前