You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
first_versio/通信工程1802—李清宇.c

251 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct infor {
char ID[50];
char NAME[50];
float PRICE;
long NUMBER;
}g[1000],m[1000],x;
struct inf {
char id[50];
char name[50];
float price;
long number;
}c;
//struct infor g[1000];
FILE *p;
void menu_s();
void input();
void output();
void check();
void updata();
void add();
void dele();
void dele2();
int len();
int len2();
int main()
{
system("cls");//清屏
int n;
menu_s();
scanf("%d",&n);
while(n)
{
system("cls");
switch(n)
{
case 1:
input() ;break;
case 2:
output();break;
case 3:
updata();break;
case 4:
check();break;
case 5:
return 0;
}
getch();
menu_s();
scanf("%d",&n);
}
}
void menu_s ()
{
printf("----------------------------\n");
printf("***********主菜单***********\n");
printf("*********1:商品入库*********\n");
printf("*********2:商品出库*********\n");
printf("*********3:更新库存*********\n");
printf("*********4:查询库存*********\n");
printf("*********5:退出系统*********\n");
printf("*********请输入选项*********\n");
printf("----------------------------\n");
}
void input()
{
int i=0,s;
//struct infor g;
char next[2]="y";
p=fopen("1.txt","wb+");
if(p==NULL)
{
printf("打开文件失败\n");
fclose(p);
exit(0);
}
while(strcmp(next,"y")==0||strcmp(next,"Y")==0)
{
printf("\n请输入商品编号\n");
scanf("%s",g[i].ID );
printf("\n请输入商品名称\n");
scanf("%s",&g[i].NAME );
printf("\n请输入商品价格\n");
scanf("%f",&g[i].PRICE );
printf("\n请输入商品数量\n");
scanf("%d",&g[i].NUMBER );
fwrite(&g[i],sizeof(struct infor),1,p);/*向文件写入数据*/
printf("\n继续输入y(Y)/n(N)\n");
fflush(stdin);//清空缓存区。
i++;
//next=getch();
scanf("%s",next);
}
fclose(p);
}
int len()//读取当前文件长度
{ int z=0;
p=fopen("1.txt","r+");
if(p==NULL)
{
printf("打开文件失败\n");
exit(1);
}
rewind(p);//将文件指针移至文件开头。
while(!feof(p))
{
if((fread(&g[z],sizeof(struct infor),1,p))==1);
z++;
}
fclose(p);
return z-1;
}
int len2()//读取当前文件长度
{ int z=0;
FILE *fp;
fp=fopen("3.txt","r+");
if(fp==NULL)
{
printf("打开文件失败\n");
exit(1);
}
rewind(fp);//将文件指针移至文件开头。
while(!feof(fp))
{
if((fread(&c,sizeof(struct inf),1,fp))==1);
z++;
}
fclose(fp);
return z;
}
void check()
{
int i,s=0;
long length;
p=fopen("1.txt","rb+");
if(p==NULL)
{
printf("打开文件失败\n");
exit(1);
}
printf("\n----------------------------------------------------\n商品列表\n----------------------------------------------------\n");
printf("编号\t\t品名\t\t价格\t\t数量\n");
rewind(p);//将文件指针移至文件开头。
/* while(!feof(p))
{
if((fread(&g[s],sizeof(struct infor),1,p))==1);
s++;
}*/
s=len();
rewind(p);
fread(g,sizeof(struct infor),s,p);
for(i=0;i<s;i++)
printf("%-10s\t%s\t\t%.1f\t\t%ld\n",g[i].ID ,g[i].NAME ,g[i].PRICE,g[i].NUMBER );
printf("----------------------------------------------------\n");
fclose(p);
}
void output()
{
FILE *p,*fp;
char a[50],next[2]="y";
int i,j,l,sum=0,num=0,s;
p=fopen("1.txt","rb+");
fp=fopen("3.txt","wb+");
while(strcmp(next,"y")==0||strcmp(next,"Y")==0)
{
l=len();
printf("请输入售出的商品名称\n");
scanf("%s",&a[50]);
for(i=0;i<l;i++)
{
fseek(p,sizeof(struct infor)*(i),0);
fscanf(p,"%s%s%d%d",&c.id ,&c.name ,&c.price ,&c.number );
//printf("%-10s\t%s\t\t%.1f\t\t%ld\n",c.id ,c.name ,c.price ,c.number);
if(strcmp(a,c.name )==0)
{
fwrite(&c,sizeof(struct inf),1,fp);
printf("%-10s\t%s\t\t%.1f\t\t%ld\n",c.id ,c.name ,c.price ,c.number);
fclose(p);
void dele2( char a[50] );
sum++;
}
}
fclose(fp);
printf("\n继续输入y(Y)/n(N)\n");
scanf("%s",next);
}
s=len2();
fp=fopen("3.txt","rb+");
printf("\n----------------------------------------------------\n商品列表\n----------------------------------------------------\n");
for(i=0;i<s;i++)
{
fseek(fp,sizeof(struct inf)*(i),0);
fscanf(fp,"%s%s%d%d",&c.id ,&c.name ,&c.price ,&c.number );
num+=c.price ;
printf("%-10s\t%s\t\t%.1f\t\t%ld\n",c.id ,c.name ,c.price ,c.number);
printf("总销售额:\t\t\t\t\t%d\n",num);
}
fclose(fp);
printf("----------------------------------------------------");
}
void updata()
{
int a;
printf("\n请选择1删除商品信息2添加商品信息\n");
scanf("%d",&a);
switch(a)
{
case 2:add();break;
}
}
void add()
{
FILE *P;
int i=0;
char next[2]="y";
//i=len();
//fseek(p,0l,2);
while(strcmp(next,"y")==0||strcmp(next,"Y")==0)
{
printf("\n请输入商品编号\n");
scanf("%s",&g[i].ID );
printf("\n请输入商品名称\n");
scanf("%s",&g[i].NAME );
printf("\n请输入商品价格\n");
scanf("%f",&g[i].PRICE );
printf("\n请输入商品数量\n");
scanf("%d",&g[i].NUMBER );
p=fopen("1.txt","ab");
if(p==NULL)
{
printf("打开文件失败\n");
exit(0);
}
fwrite(&g[i],sizeof(struct infor),1,p);/*向文件写入数据*/
fclose(p);
printf("\n继续输入y(Y)/n(N)\n");
//fflush(stdin);//清空缓存区。
//i++;
//next=getch();
scanf("%s",next);
}
}