C++仓库管理系统

#include<iostream>


#include<fstream>


#include<iomanip>


#include<string>


using namespace std;


struct HUOWU //定义一个货物结构体
{
         int number;//货物编号
		 string name;//货物名称
		 float price;//货物价格
		 int count;//货物数量
		 void ZengJia();//增加货物函数
		 void ShanChu();//删除货物函数
		 void NameChaXun();//查询货物函数
		 void IdChaXun();//按照编号查询货物函数
		 void XianShi();//显示货物函数
		 void menu();//主功能菜单函数
		 void Buzu();//显示不足一定数量的货物
		 int JiHangShu();//计算行数函数


} h1;
//在类外定义一个用于计算txt文档个行数的函数,用于最后的Buzu()函数
int HUOWU::JiHangShu()
{
	int lineCnt=0;
	char c;
	fstream file("KuCun.txt");
	while(file.get(c))
	{
		if(c=='\n')
			lineCnt++;
	}
	return lineCnt;
}
//在类外定义增加货物信息函数
void HUOWU::ZengJia()
{
	
	cout<<"开始录入货物信息"<<endl;
	cout<<"货物编号"<<endl;
	cin>>number;
	cout<<"货物名称"<<endl;
	cin>>name;
	cout<<"货物价格"<<endl;
	cin>>price;
	cout<<"货物数量"<<endl;
	cin>>count;
    //将录入的信息保存在txt文档中
	ofstream KuCunFile("KuCun.txt",ios::app);//文件输入流 用于向文件里写入数据
	KuCunFile<<setiosflags(ios::left)<<setw(20)<<number<<setw(20)<<name<<setw(20)<<price<<setw(20)<<count<<endl;//分配储存空间
	KuCunFile.close();


	cout<<"录入完成"<<endl;


	system("pause");//设置延迟
	h1.menu();//返回主界面


}
void HUOWU::menu()
{
	system("cls");
	//主界面菜单
	cout<<" * * * * * * * * * * * * * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * *仓库管理系统* * * * * * * * * * * *            "<<endl;
	cout<<" *  * * * 1.增加货物信息* * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * 2.删除货物信息* * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * 3.名称查询货物* * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * 4.编号查询货物* * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * 5.显示货物信息* * * * * * * * * * * *           "<<endl;
	cout<<" *  * * * 6.显示不足货物* * * * * * * * * * * *           "<<endl;
	
	cout<<"请输入序号来实现想要的功能"<<endl;


	//调用switch函数 a为传入的参数 实现四个不同功能
	int a;


	cin>>a;//从键盘输入想要实现的功能


	switch(a)
	{
	case 1:  h1.ZengJia();break;
	case 2:  h1.ShanChu();break;
	case 3:  h1.NameChaXun(); break;
	case 4:  h1.IdChaXun();break;
	case 5:  h1.XianShi();break;
	case 6:  h1.Buzu();break;
	default:cout<<"请输入正确的序号"<<endl;
	}
}
//在类外定义删除货物信息函数
void HUOWU::ShanChu()
{
	ifstream file("KuCun.txt");
    string line;
    int n, count = 0;
    ofstream outfile("KuCun1.txt", ios::out | ios::trunc);
    cout << "请输入你想要删除的行数" << endl;
    cin >> n;
    while (!file.eof())//eof end of file 用于判断是否读到文件的结尾字符
	{
        getline(file, line);
        if (count != n - 1)//如果要修改内容就在这修改line的内容,再存到文件中就行了
            outfile << line << endl;
            count++;
    }
    outfile.close();
    file.close();
    ofstream outfile1("KuCun.txt", ios::out | ios::trunc);
    fstream file1("KuCun1.txt");
    while (!file1.eof()) {
        getline(file1, line);
        outfile1 << line << endl;
    }
    outfile1.close();
    file1.close();
    system("del KuCun1.txt");//删除中间文件


	system("pause");//设置延迟
	h1.menu();//返回主界面
	
}
//在类外定义按名称查询函数
void HUOWU::NameChaXun()
{
	string a;
	cout<<"请输入你想要查询的货物名称"<<endl;
	cin>>a;
	fstream fout("KuCun.txt");
	while(1)
	{
		fout>>number>>name>>price>>count;
		if(name==a)
		{
			cout<<number<<" "<<name<<" "<<price<<" "<<count<<endl;
			break;
		}
		else if(fout.eof())
		{
			cout<<"查无此货"<<endl;
			break;
		}
	}
	system("pause");//设置延迟
	h1.menu();//返回主界面
}
//在类外定义按编号查询函数
void HUOWU::IdChaXun()
{
	int a;
	cout<<"请输入你想要查询的货物编号"<<endl;
	cin>>a;
	fstream fout("KuCun.txt");
	while(1)
	{
		fout>>number>>name>>price>>count;
		if(number==a)
		{
			cout<<number<<" "<<name<<" "<<price<<" "<<count<<endl;
			break;
		}
		else if(fout.eof())
		{
			cout<<"查无此货"<<endl;
			break;
		}
	}
	system("pause");//设置延迟
	h1.menu();//返回主界面
}
//在类外定义显示货物信息函数
void HUOWU::XianShi()
{   
	system("cls");
	fstream myfile("KuCun.txt",ios::in|ios::out);
	string line;
	if(myfile.fail())
	{
		cerr<<"error oprening file KuCun"<<endl;
		exit(-1);
	}
	while(getline(myfile,line))
		cout<<line<<endl;
/*	ifstream in("KuCun.txt");
	for(string str;getline(in,str);)
		cout<<str<<endl;*/ //与上面相同 更简洁 都是显示所有货物信息的
	system("pause");//设置延迟
	h1.menu();//返回主界面
}
//在类外定义显示不足一定数量的货物函数
void HUOWU::Buzu()
{
	system("cls");
	fstream myfile("KuCun.txt",ios::in|ios::out);
	string line;
	int a;
	cout<<"输入想要查询的货物数量"<<endl;
	cin>>a;
	
	int index=0;


	
	if(myfile.fail())
	{
		cerr<<"error opening file KuCun"<<endl;
		exit(-1);
	}
	if(!myfile.fail())
	{
		while(index<h1.JiHangShu())
		{
			myfile>>number>>name>>price>>count;
			if(count<a)
			{
				cout<<number<<" "<<name<<" "<<price<<" "<<count<<endl;
				index++;
				continue;
			}
			else
			{
				cout<<"货物充足"<<endl;
				index++;
				continue;
			}
		}
	}




	
	system("pause");//设置延迟
	h1.menu();//返回主界面
}
	
int main()
{
	h1.menu();
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/qq_41530545/article/details/80910543
今日推荐