⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 super_market.cpp

📁 C语言做的超市进销存源代码文件
💻 CPP
字号:
#include<iomanip>
#include<fstream>
#include<iostream>
#include<sstream>
#include<conio.h>
#include<vector>
#include<windows.h>
#include"super_marker.h"
using namespace std;	
void  inline get_number(int& number)
{
	while(1)
	{
		cin>>number;
		if(cin.fail())
		{
			cin.clear(ios::goodbit);
			fflush(stdin);
			cout<<"出错啦!重试!"<<endl;
			
		}
		else break;
	}
}

void inline clear()
{
	system("cls");
	cout<<"                     ***********************"<<endl;
	cout<<"                     *                     *"<<endl;
	cout<<"                     *     ——  超市      *"<<endl;
	cout<<"                     *                     *"<<endl;
	cout<<"                     *    "<<__DATE__<<"      *"<<endl;
	cout<<"                     *     "<<__TIME__<<"        *"<<endl;
	cout<<"                     ***********************"<<endl;	
}

Market::Market()
{

	goods_file.open("goods.dat",ios_base::in|ios_base::out|ios::binary);

	if(goods_file.fail())
	{
		cerr<<"Error:没有货物记录“goods.dat”!!!"<<endl;
		exit(1);
		
	}
	strcpy(good.bar_code,"\0");
	strcpy(good.buy_date,"0");
	strcpy(good.name,"0");
	good.getin_price=0.0;
	good.quantity=0;
	good.sell_price=0.0;
	good.serial_number=0;
	goods_file.seekg(0,ios_base::end);
	streampos pos=goods_file.tellp();
	if((int)pos==0)
	{
		cout<<"正在初始化货物文件……"<<endl;
		for(int i=0;i<10000;i++)
		{
			goods_file.write((char*)&good,sizeof(goods));
		}
	}
	fstream f_time;//流水号刷新
	f_time.open("business_time.dat");
	f_time.seekg(0,ios::end);
	pos=f_time.tellp();
	if((int)pos==0)
	{
		cout<<"正在初始化流水号文件……"<<endl;
		f_time<<0;
	}
	f_time.close();
	
}
Market::~Market()
{
	goods_file.close();
}
void Market::stock()
{
	cout<<"\n__________________进货系统______________________"<<endl;
	cout<<"\n商品类型:\n1--食品    \t2--生活用品\t3--工具    \n";
	cout<<"4--学习用品\t5--休闲用品\t6--电子产品\n0--所有商品\n";
	while(1)
	{
		cout<<"Esc退出,回车继续"<<endl;
		char get_ok;
		fflush(stdin);
		get_ok=getch();
		if(get_ok==27)break;
		else 
		{
			fflush(stdin);
			cout<<"\n货物编码:";
			get_number(good.serial_number);
			int save_serial_number=good.serial_number;
			goods_file.seekg(good.serial_number*(sizeof(goods)));	
			goods_file.read((char*)&good,sizeof(goods));
			good.serial_number=save_serial_number;
			if(strcmp(good.bar_code,"\0")!=0)
			//比较两个字符串str1和str2 str1大于str2时返回整数;等于返回0;小于返回负数
			{
				cout<<"该商品已经存在,输入你要增加的数量:"<<endl;
				cout<<"货品数量 :";
				int quantity;
				get_number(quantity);
				good.quantity+=quantity;
				goods_file.seekg(good.serial_number*(sizeof(goods)));
				goods_file.write((char*)&good,sizeof(goods));
				cout<<"记录已经存储,还有吗?(yes/no)"<<endl;
			}
			else
			{
				cout<<"货品名:";
				cin>>good.name;	
				cout<<"条形码:";
				cin>>good.bar_code;
				cout<<"货品数量:";
				get_number(good.quantity);
				cout<<"进货价格:";
				cin>>good.getin_price;		
				while(1)
				{
					cout<<"\n卖出价格:";
					cin>>good.sell_price;
					if(good.getin_price>=good.sell_price)
					{
						cerr<<"\n!!!! 难道你要亏本卖出吗?(回车确认)……\n";
						char get_ok=getch();
						if(get_ok==13)break;
					}
					else break;
				}
				ostringstream oss;
				SYSTEMTIME st; 
				GetLocalTime(&st);
				oss<<st.wYear<<"/"<<st.wMonth<<"/"<<st.wDay<<"/"<<st.wHour<<":"<<st.wMinute
				<<":"<<st.wSecond<<endl;
				string s6=oss.str();
				const char* c6=s6.c_str();
				strcpy(good.buy_date,c6);

				cout<<"进货日期为:"<<s6;
				goods_file.seekg((good.serial_number)*(sizeof(goods)));
				goods_file.write((char*)&good,sizeof(goods));
				cout<<"\n货物部分记录已经存储,还有吗?";
			}
		}
	}
}
void Market::view_good()
{
	goods_file.seekg(0,ios::beg);
	cout<<"\n请选择你要查询的商品类型:\n1--食品    \t2--生活用品\t3--工具    \n";
	cout<<"4--学习用品\t5--休闲用品\t6--电子产品\n0--所有商品\n";
	char get_select=getch();
	int choice=(int)get_select-48;
	goods_file.seekg(choice*1000*sizeof(goods));
	
	cout<<"|编号|       名称       |数量|商品进价| 商品售价 |   条形码   |  进货日期  |"<<endl;      
	cout<<"============================================================================"<<endl;			
	int max_view=0;
	if(choice==0)max_view=10000;
	else max_view=1000;
	int view=0;
	while(goods_file.read((char*)&good,sizeof(goods)))
	{
		if(view++==max_view)break;

		if(strcmp(good.bar_code,"\0")==0)continue;
		else
		{
			cout<<setfill(' ')<<setw(4)<<good.serial_number
				<<setw(18)<<good.name
				<<setw(5)<<good.quantity;
			printf("%8.2f",good.getin_price);
			printf("%8.2f",good.sell_price);
			cout<<"    "
				<<setw(12)<<good.bar_code
				<<"  "
				<<setw(18)<<good.buy_date;	
		}
	}
	goods_file.clear(ios::goodbit);
	cout<<"\n________________________________________________________________\n";
}
void Market::set_price()
{	
		while(1)
			{
			cout<<"\n请输入你要调整的商品的编号: ";
			cin>>good.serial_number;
			int n=sizeof(goods)*good.serial_number;
			goods_file.seekg(sizeof(goods)*good.serial_number);		
			goods_file.read((char*)&good,sizeof(goods));
			if(strcmp(good.bar_code,"\0")==0)cout<<"\n该编号尚未实现,请检查你的输入!!\n";
			else
			{
				cout<<"|编号|  名称  |数量|商品进价| 商品标价 |   条形码   "<<      
					"|  进货日期  |"<<endl;
				
				cout<<setw(4)<<good.serial_number
					<<setw(10)<<good.name
					<<setw(5)<<good.quantity;
				printf("%8.2f",good.getin_price);
				printf("%8.2f",good.sell_price);
				cout<<"    "
					<<setw(10)<<good.bar_code
					<<"  "
					<<setw(20)<<good.buy_date
					<<endl;	
				cout<<"请输入你要调整后的价格: ";
				cin>>good.sell_price;
				goods_file.seekg((good.serial_number)*(sizeof(goods)));
				goods_file.write((char*)&good,sizeof(goods));
				cout<<"\n货物记录已经存储,还有吗?(回车确认)";
				char get_ok;
				get_ok=getch();
				if(get_ok==13){}
				else break;
			}
		}	
}

		

	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -