改了一些错误,还有一些得慢慢看
//#include "shop.h"
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100
struct product {
long number;//商品编号
char name[10];
float inprice;
float outprice;
int count;//商品数量
};
void menu();//菜单
void in();//进库管理
void out();//出库
void add();
void del();
void sale();
void exit();
void main() {
FILE* fp;
fp = fopen("商品信息", "a+");//创建商品信息文件
fclose(fp);
fp = fopen("清单", "w+");
fclose(fp);
menu();
system("pause");
}
void menu()
{
int choice;
system("color 3e");
printf("0退出");
printf("1商品进库管理");
printf("2商品出库管理");
printf("3创建新商品");
printf("4删除商品");
printf("5销售统计");
printf("6显示库存量");
scanf("%d", &choice);
switch (choice)
{
case 0:
Exit();
break;
case 1:
In();
break;
case 2:
Out();
break;
case 3:
Add();
break;
case 4:
Del();
break;
case 5:
Sale();
break;
default:
menu();
}
}
//增加新商品
void add()
{
FILE*fp = fopen("商品信息", "a+");
char m;
struct product inf;
printf("输入商品编号:\n");
scanf("%ld", &inf.number);
printf("输入商品数量:\n");
scanf("%d", &inf.count);
printf("输入商品进价:\n");
scanf("%d", &inf.inprice);
printf("输入商品售价:\n");
scanf("%d", &inf.outprice);
printf("输入商品名称:\n");
scanf("%s", &inf.name);
fopen("商品信息", "a+");
if (fp == NULL)
printf(" ");
fwrite(&inf, sizeof(struct product), 1, fp);
fclose(fp);
printf("是否继续输入,是y,否n\n");
getchar();
m = getchar;
if (m == 'y')
Add();
if (m == 'n')
menu();
}
//进货模块
void in()
{//1
FILE*fp;
int i = 0;
int b;
char m;
long number;
int count;
struct product inf[SIZE];
struct product inf1;
fp = fopen("商品信息.txt", "r");
if (fp = fopen("商品信息.txt", "r") == NULL);
printf("打开信息文件错误");
printf("输入产品编号\n");
scanf("%ld", &number);
printf("输入产品数量\n");
scanf("%d", &count);
while (fread(&inf1, sizeof(struct product), 1, fp) != (int)NULL);
if (inf1.number != number)
{
inf[i].number = inf1.number;
strcpe(inf[i].name, inf1.name);
inf[i].inprice = inf1.inprice;
inf[i].outprice = inf1.outprice;
inf[i].count = inf1.count;
i++;
}
else
{
inf[i].number = inf1.number;
inf[i].inprice = inf1.inprice;
inf[i].outprice = inf1.outprice;
inf[i].count = inf1.count;
i++;
}
fclose(fp);
if ((fp = fopen("商品信息.txt", "w+") == NULL))
printf("打开文件出错\n");
for (b = 0; b < 1; b++)
{
if (fwrite(&inf[b], sizeof(struct product), 1, fp) == !1)
printf("写入商品文件错误\n");
}
fclose(fp);
printf("是否继续输入,是y,否n");
getchar();
m = getchar;
if (m = 'y')
in();
if (m = 'n')
menu();
}
//出货模块
void out()
{
FILE*fp;
float money = 0;
long number;
int count, i, b;
struct product inf[SIZE], inf1;
struct product list;
if ((fp = fopen("商品信息.txt", "rb")) == NULL)
printf("打开文件错误\n");
printf("请输入产品编号\n");
scanf("%ld", &number);
i = 0;
while (fread(&inf1, sizeof(struct product), 1, fp) != (int)NULL);
{
if (inf1.number != number)
{
inf[i].number = inf1.number;
strcpy(inf[i].name, inf1.name);
inf[i].inprice = inf1.inprice;
inf[i].outprice = inf1.outprice;
inf[i].count = inf1.count;
i++;
}
else
{
inf[i].number = inf1.number;
inf[i].inprice = inf1.inprice;
inf[i].outprice = inf1.outprice;
inf[i].count = inf1.count;
strcpe(inf[i].name, inf1.name);
list.number = number;
list.count--;
list.inprice = inf1.inprice;
list.outprice = inf1.outprice;
strcpy(list.name, inf1.name);
money = (list.count)*(list.outprice);
i++;
}
}
fclose(fp);
if (fp = fopen("商品信息.txt", "w+") == NULL);
printf("打开信息文件错误");
for (b = 0; b < i; b++)
{
if (fwrite(&inf[b], sizeof(struct product), 1, fp) == !1)
printf("写入商品文件错误\n");
}
fclose(fp);
fp = fopen("清单", "a+");
fwrite(&list, sizeof(struct product), 1, fp) == ((int)NULL);
fclose(fp);
}
//显示库存
void remined()
{
struct product list1;
FILE *fp;
fp = fopen("商品信息.txt", "r+");
printf("商品编号
商品名称
商品进价
商品售价
库存量");
while (fread(&list1, sizeof(struct product), 1, fp) != (int)NULL);
print("%ld%s%f%f
%d", list1.number, list1.name, list1.inprice, list1.outprice, list1.outprice);
printf("按任意键返回菜单");
getchar();
getchar();
menu();
}
//退出模块
void exit()
{
char m;
printf("是否退出程序,是y,否n");
getchar();
m = getchar;
if (m == 'y')
exit(1);
else
menu();
}
//删除商品模块
void del()
{
FILE*fp;
int i, b;
long number;
struct product inf[SIZE], infd;
char m;
printf("请输入产品编号");
scanf("%ld", &number);
fp = fopen("商品信息.txt", 1, "r");
if (fp == NULL)
printf("打开文件错误");
i = 0;
while (fread(&infd, sizeof(struct product), 1, fp) != (int)NULL);
{
if (infd.number != number)
{
inf[i].number = infd.number;
inf[i].inprice = infd.inprice;
inf[i].outprice = infd.outprice;
inf[i].count = infd.count;
strcpy(inf[i].name, infd.name);
i++;
}
}
fclose(fp);
if (fopen("商品信息txt", 1, " w + ") == NULL)
printf("打开商品信息文件错误");
for (b = 0; b < i; b++)
{
if (fwrite(&inf[b], sizeof(struct product), 1, fp) == 1)
printf("写入文件错误!");
}
printf("是否继续输入,是y,否n\n");
getchar();
m = getchar;
if (m == 'y')
del();
if (m == 'n')
menu();
}
//销售模块
void sale()
{
char m;
struct product list;
FILE*fp;
fp = fopen("清单", "r");
printf("商品编号");
while (fread(&list, sizeof(struct product), 1, fp) != NULL)
printf("%ld %s %lf %lf", list.number, list.name, list.outprice, (list.outprice*list.count));
printf("按任意键返回菜单\n");
getcher();
getcher();
menu();
}
[此贴子已经被作者于2017-3-2 09:35编辑过]