#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include <fstream>
using namespace std;
//结构体
struct commodity
{
int num;//商品号
char kind[10];//产品的种类
char name[10];//产品的名称
int count;//产品的总量
char date[15];//进货日期
int sale;//销出数量
char saledate[15];//销售时间
} cod[50],codtemp[50],dod[50];//产品名称cod,产品种类dod
//定义函数
void inputnum();
void inputkind();
void inputname();
void sort();
int count(int n);
void binarysearch();
int choice;
int main(int argc, char *argv[])
{
cout<<"\t\t\t欢迎进入产品进销存管理系统"<<endl;
do{
cout<<"\t1.产品入库"<<endl;
cout<<"\t2.查找产品"<<endl;
cout<<"\t3.退出系统"<<endl;
cin>>choice;
switch(choice) {
case 1:cout<<"\t1.产品类的添加"<<endl;
cout<<"\t2.产品的添加"<<endl;
cout<<"\t3.数量的添加"<<endl;
cin>>choice;
switch(choice){
case 1:inputkind();break;
case 2:inputname();break;
case 3:inputnum();break;
default:cout<<"错误!";break;
}break;
case 2:binarysearch();break;
case 3:exit(0); break;
default:cout<<"错误!"<<endl;
break;
}
} while(1);
system("PAUSE");
return 0;
}
//计算文件中结构体个数
int count(int n)
{
FILE *fp;
int i;
if((fp=fopen("产品","r"))==NULL)
{
cout<<"打不开文件!"<<endl;
exit(0);
}
for(i=0;fread(&cod[i],sizeof(struct commodity),1,fp);i++) //读文件
{
}
return i;
}
//产品种类的添加
void inputkind()
{
FILE *fp1,*fp2;
int i,size,size1,j;
if((fp1=fopen("产品种类","a"))==NULL)
{ //追加
cout<<"打不开文件!"<<endl;
exit(0);
}
cout<<"请输入新入库产品种类的个数:";
cin>>size;
for(i=0;i<size;i++)
{//在产品种类文件中添加
cout<<"请输入第"<<i+1<<"个产品种类的资料"<<endl;
cout<<"产品的种类:";
cin>>dod[i].kind;
cout<<"请输入该产品种类中产品的个数:";
cin>>size1;
if((fp2=fopen("产品","a"))==NULL)
{ //追加
cout<<"打不开文件!"<<endl;
exit(0);
}
for(j=0;j<size1;j++)
{//在产品文件中添加
cout<<"请再次输入种类:";
cin>>cod[j].kind;
cout<<"请输入产品号:";
cin>>cod[j].num;
cout<<"产品的名称:";
cin>>cod[j].name;
cout<<"产品的数量:";
cin>>cod[j].count;
cout<<"产品的进货日期:";
cin>>cod[j].date;
cout<<"销出数量:";
cin>>cod[j].sale;
cout<<"销售时间:";
cin>>cod[j].saledate;
}
fwrite(&cod[i],sizeof(struct commodity),1,fp2);
}
fwrite(&dod[i],10,1,fp1); //把数据写入文件
fclose(fp1);
fclose(fp2);//关闭
}
//产品的添加
void inputname()
{
FILE *fp;
int i,size;
if((fp=fopen("产品","a"))==NULL)
{ //追加
cout<<"打不开文件!"<<endl;
exit(0);
}
cout<<"请输入新入库产品的个数:";
cin>>size;
for(i=0;i<size;i++) {
{
cout<<"请输入第"<<i+1<<"个产品的资料"<<endl;
cout<<"所属产品的种类:";
cin>>cod[i].kind; //
cout<<"请输入产品号:";
cin>>cod[i].num;
cout<<"产品的名称:";
cin>>cod[i].name;
cout<<"产品的数量:";
cin>>cod[i].count;
cout<<"产品的进货日期:";
cin>>cod[i].date;
cout<<"销出数量:";
cin>>cod[i].sale;
cout<<"销售时间:";
cin>>cod[i].saledate;
}
fwrite(&cod[i],sizeof(struct commodity),1,fp); //把数据写入文件
}
fclose(fp); //关闭
}
//数量的添加
void inputnum()
{
FILE *fp;
int i,size,j;
int k=count(1);
char str[10];//记录增加数量的产品名称
sort();
if((fp=fopen("产品","rb"))==NULL)
{ //追加
cout<<"打不开文件!"<<endl;
exit(0);
}
cout<<"请输入增加数量的产品名称:";
cin>>str;
cout<<"请输入数量:";
cin>>size;
for(i=0;fread(&cod[i],sizeof(struct commodity),1,fp);i++)
{
if(strcmp(str,cod[i].name)==0)
{
cod[i].count=cod[i].count+size;
{ //重写
fp=fopen("产品","wb");
for(j=0;j<k;j++)
fwrite(&cod[j],sizeof(struct commodity),1,fp);
fclose(fp);
}
break;
}
}
fclose(fp); //关闭
cout<<"添加成功!"<<endl;
}
//商品排序
void sort() //排序函数
{
int i,j,n,last,w;
int templist[30];
struct commodity temp;
FILE *fp;
if((fp=fopen("产品","rb"))==NULL)
{
cout<<"打不开文件!"<<endl;
exit(0);
}
int k=count(1);
for(i=0;i<k;i++)
{
fread(&cod[i],sizeof(struct commodity),1,fp);
templist[i]=cod[i].num;
}
i--;
while(i>0) //冒泡排序
{
last=0;
for(n=0;n<i;n++)
{
if(templist[n+1]<templist[n])
{
w=templist[n];templist[n]=templist[n+1];templist[n+1]=w;
last=n;
}
}
i=last;
}
for(i=0;i<k;i++)
for(j=0;j<k;j++)
{
fseek(fp,j*sizeof(struct commodity),0);
fread(&cod[j],sizeof(struct commodity),1,fp);
if(templist[i]==cod[j].num) //比较数组中i的值是否与结构体中商品号相等
codtemp[i]=cod[j]; //将文件中存储的结构体数据暂存在临时结构体中
}
fclose(fp);
{ //重写
FILE *fp;
fp=fopen("产品","wb");
for(i=0;i<k;i++)
fwrite(&codtemp[i],sizeof(struct commodity),1,fp);
fclose(fp);
}
}
// 查找商品
void binarysearch()
{
int low=0,high,k,mid=0,m,seek;
char str[10];
k=count(1);
high=k-1;
sort(); //调用
cout<<"请选择查找方式:"<<endl;
cout<<"1.产品号查找"<<endl;
cout<<"2.产品名查找"<<endl;
cin>>seek;
switch(seek)
{ case 1:
{
cout<<"请输入产品号:";
cin>>m;
while(low<=high) //折半查找
{
mid=(low+high)/2;
if(m==cod[mid].num)
{
FILE *fp;
if((fp=fopen("commodity","r"))==NULL)
{
cout<<"打不开文件."<<endl;
exit(0);
}
fseek(fp,mid*sizeof(struct commodity),0);
fread(&cod[mid],sizeof(struct commodity),1,fp);
fclose(fp);
//
cout<<"所属产品类:";
cout<<cod[mid].kind<<endl;
cout<<"产品号:";
cout<<cod[mid].num<<endl;
cout<<"产品名称:";
cout<<cod[mid].name<<endl;
cout<<"产品总量:"<<cod[mid].count<<endl;
cout<<"进货日期:"<<cod[mid].date<<endl;
cout<<"销出数量:"<<cod[mid].sale<<endl;
cout<<"销售日期:"<<cod[mid].saledate<<endl;
break;
}
else if(m<cod[mid].num)
high=mid-1;
else low=mid+1;
}
if(low!=high)
cout<<"不存在该商品!"<<endl;
break;
}
case 2:
{ FILE *fp;
fp=fopen("产品","r");
cout<<"请输入产品名:"<<endl;
cin>>str;
for(int i=0;fread(&cod[i],sizeof(struct commodity),1,fp);i++)
{
if(strcmp(str,cod[i].name)==0) //是否匹配
{
cout<<"所属产品类:";
cout<<cod[i].kind<<endl;
cout<<"产品号:";
cout<<cod[i].num<<endl;
cout<<"产品名称:";
cout<<cod[i].name<<endl;
cout<<"产品总量:"<<cod[i].count<<endl;
cout<<"进货日期:"<<cod[i].date<<endl;
cout<<"销出数量:"<<cod[i].sale<<endl;
cout<<"销售日期:"<<cod[i].saledate<<endl;
break;
}
}
fclose(fp);
break;
}
default :
cout<<"系统关闭!"<<endl;
break;
}
}
参考资料:百度文库
本回答被网友采纳