2023年全国大学生数学建模竞赛C题 蔬菜类商品的自动定价与补货决策

本文仅含数据处理(编程位),且c++

题目

在生鲜商超中,一般蔬菜类商品的保鲜期都比较短,且品相随销售时间的增加而变差,大部分品种如当日未售出,隔日就无法再售。因此,商超通常会根据各商品的历史销售和需求情况每天进行补货。
由于商超销售的蔬菜品种众多、产地不尽相同,而蔬菜的进货交易时间通常在凌晨 3:00-4:00,为此商家须在不确切知道具体单品和进货价格的情况下,做出当日各蔬菜品类的补货决策。蔬菜的定价一般采用“成本加成定价”方法,商超对运损和品相变差的商品通常进行打折销售。可靠的市场需求分析,对补货决策和定价决策尤为重要。从需求侧来看,蔬菜类商品的销售量与时间往往存在一定的关联关系;从供给侧来看,蔬菜的供应品种在 4 月至 10月较为丰富,商超销售空间的限制使得合理的销售组合变得极为重要。
附件 1 给出了某商超经销的 6 个蔬菜品类的商品信息;附件 2 和附件 3 分别给出了该商超 2020 年 7 月 1 日至 2023 年 6 月 30 日各商品的销售流水明细与批发价格的相关数据;附件 4 给出了各商品近期的损耗率数据。请根据附件和实际情况建立数学模型解决以下问题:
问题 1 蔬菜类商品不同品类或不同单品之间可能存在一定的关联关系,请分析蔬菜各品类及单品销售量的分布规律及相互关系。
问题 2 考虑商超以品类为单位做补货计划,请分析各蔬菜品类的销售总量与成本加成定价的关系,并给出各蔬菜品类未来一周(2023 年 7 月 1-7 日)的日补货总量和定价策略,使得商超收益最大。
问题 3 因蔬菜类商品的销售空间有限,商超希望进一步制定单品的补货计划,要求可售单品总数控制在 27-33 个,且各单品订购量满足最小陈列量 2.5 千克的要求。根据 2023年 6 月 24-30 日的可售品种,给出 7 月 1 日的单品补货量和定价策略,在尽量满足市场对各品类蔬菜商品需求的前提下,使得商超收益最大。
问题 4 为了更好地制定蔬菜商品的补货和定价决策,商超还需要采集哪些相关数据,这些数据对解决上述问题有何帮助,请给出你们的意见和理由。
附件 1 6 个蔬菜品类的商品信息
附件 2 销售流水明细数据
附件 3 蔬菜类商品的批发价格
附件 4 蔬菜类商品的近期损耗率
注 (1) 附件 1 中,部分单品名称包含的数字编号表示不同的供应来源。
(2) 附件 4 中的损耗率反映了近期商品的损耗情况,通过近期盘点周期的数据计算得到。

问题一

计算单品的总销售额

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
//计算总销售额 
struct goods{
	string s[4];
	double money[36];
}g[N];
signed main(){
	IOS;
	ifstream data1("annex1.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    getline(data1, line1);
    int k=0;
    map<string,double> mp;
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        int t=0;
        while (getline(sin1, word1, ','))
        {
            g[k].s[t++]=word1;
        }
        mp[g[k].s[0]]=0;
        k++;
    }
    data1.close();

	cout<<k<<endl;
//	fer(i,0,k)cout<

	ifstream data2("annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        int i=0;
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        double res=stold(words2[3])*stold(words2[4]);
        mp[words2[2]]+=res;
    }
    data2.close();
//    map< string,double>::iterator it;
//    for(it=mp.begin();it!=mp.end();it++)cout<first<<" "<second<
	map<string,double>::iterator it;
		for(it=mp.begin();it!=mp.end();it++){
			fer(i,0,k){
				if(it->first==g[i].s[0]){
					g[i].money=it->second;
					break;
				}
			}
		}
    ofstream outFile;
    outFile.open("salesVolume.csv",ios::out|ios::trunc);
    outFile<<"code,name,typeCode,typeName,money,typeMoney"<<endl;
	fer(i,0,k){
		outFile<<g[i].s[0]<<","<<g[i].s[1]<<","<<g[i].s[2]<<","<<g[i].s[3]<<",";
		outFile<<g[i].money<<endl;
	}
	outFile.close();
	return 0;
}

计算品类及单品的每月销售额

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
struct goods{
	string s[4];
	double money[36];
}g[N];
string type[6]={"花叶类","花菜类","水生根茎类","茄类","辣椒类","食用菌"};
double money[6][36];
//按月份计算销售额 
signed main(){
	IOS;
	ifstream data1("annex1.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    getline(data1, line1);
    int k=0;
    map<string,double> mp;
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        int t=0;
        while (getline(sin1, word1, ','))
        {
            g[k].s[t++]=word1;
        }
        fer(i,0,36){
        	string tmp=g[k].s[0];
			if(i<10)tmp+="0";
			tmp+=to_string(i);
        	mp[tmp]=0;
		}
        k++;
    }
    data1.close();

//	cout<
//	fer(i,0,k)cout<

	ifstream data2("annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; //分别是日期,编码,销量,单价的字符串 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        int i=0;
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        double res=stold(words2[3])*stold(words2[4]);
        int time=0;
        int year=words2[0][3]-'0';
		int month=(words2[0][5]-'0')*10+words2[0][6]-'0';
		int day=(words2[0][8]-'0')*10+words2[0][9]-'0';
		//cout<
		if(year==0)time=month-7;
		else if(year==1){
			time=month+5;
		}else if(year==2)time=month+17;
		else time=month+29;
		string tmp=words2[2];
		if(time<10)tmp+="0";
		tmp+=to_string(time);
        mp[tmp]+=res;
    }
    data2.close();
    //map< string,double>::iterator it;
    //for(it=mp.begin();it!=mp.end();it++)cout<first<<" "<second<
	map<string,double>::iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		string tmp=it->first;
		string code=tmp.substr(0,15);
		string strtime=tmp.substr(15);
		int time=atoi(strtime.c_str());
		//cout<second<
		fer(i,0,k){
			if(code==g[i].s[0]){
				//cout<<"success"<
				g[i].money[time]=it->second;
				break;
			}
		}
	}
//	fer(i,0,k){
//		cout<
//		fer(j,0,36)cout<
//		cout<
//	}
    ofstream outFile;
    outFile.open("salesTypeMonthly.csv",ios::out|ios::trunc);
    outFile<<"type"<<",";
    fer(i,0,36){
    	if(i<=5)outFile<<"2020 -"<<i+7<<",";
		else if(i<=17)outFile<<"2021 -"<<i-5<<",";
		else if(i<=29)outFile<<"2022 -"<<i-17 <<",";
		else if(i<35)outFile<<"2023 -"<<i-29<<",";
		else outFile<<"2023 -"<<i-29<<endl;
	}
	//输出品类按月的销售额 
	fer(i,0,k){
		fer(j,0,6){
			if(g[i].s[3]==type[j]){
				fer(t,0,36)money[j][t]+=g[i].money[t];
			}
		}
	}
	fer(i,0,6){
		outFile<<type[i]<<",";
		fer(j,0,36){
			outFile<<money[i][j];
			if(j!=35)outFile<<",";
			else outFile<<endl;
		}
	}
	//输出单品按月的销售额 
//	fer(i,0,k){
//		outFile<
//		fer(j,0,36){
//			outFile<
//			if(j!=35)outFile<<",";
//			else outFile<
//		}
//	}
	outFile.close();
	return 0;
}

问题二

打折商品的售价和销量

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
//目标:六品类的打折商品 每日  售价(卖货量*售价)  销售重量(总和)
//日期+单品编号 映射 售价 销量 是否打折 
//单品 映射 品类
//综合以上两个映射 算出 日期+品类 映射 售价 销量
struct node{
	double price,salesV;
	//bool discount;
};
signed main(){
	IOS;

    map<string,node> mp;//记录日期+编码字符串单品映射的
    map<string,string> type;//code映射type 
    map<string,node> pinlei;
    
	ifstream data2("annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        if(words2[6]=="是"){
        	string tmp=words2[0]+words2[2];
	        struct node single=mp[tmp]; 
	        single.price=stod(words2[4]);single.salesV+=stod(words2[3]);
	        //single.discount=1;
	        mp[tmp]=single;
	        //cout<
		}
        
    }
    data2.close();
    cout<<"success2"<<endl;
 
    ifstream data3("annex1.csv",ios::in);
	string line3;
	istringstream sin3;
	vector<string> words3; 
    string word3;
    getline(data3, line3);
    while (getline(data3, line3))
    {
        sin3.clear();
        sin3.str(line3);
        words3.clear();
        while (getline(sin3, word3, ',')) 
        {
        	words3.push_back(word3); 
        }
        type[words3[0]]=words3[3];
//        cout<
    }
    data3.close();
    cout<<"success3"<<endl;
    
    
    map<string,node>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
    	string key=it->first;
    	string date=key.substr(0,10);
    	string code=key.substr(10);
    	string tp=type[code];
    	string pinleiKey=date+tp;
    	if(pinlei.find(pinleiKey)==pinlei.end()){//没找见 
    		node solve;
    		solve.price=it->second.price*it->second.salesV;
    		solve.salesV=it->second.salesV;
    		pinlei[pinleiKey]=solve;
		}else{//找见了 
			node solve=pinlei[pinleiKey];
    		solve.price+=it->second.price*it->second.salesV;
    		solve.salesV+=it->second.salesV;
    		pinlei[pinleiKey]=solve;
		}
	}
	for(it=pinlei.begin();it!=pinlei.end();it++){
		node solve=it->second;
		solve.price/=solve.salesV;
		it->second=solve;
	}
    ofstream outFile;
    outFile.open("discountSS.csv",ios::out|ios::trunc);
    string typeName="水生根茎类";
    outFile<<typeName<<endl;
    outFile<<"日期,平均销售价格,销量(kg)"<<endl;
	int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int year=0,month=7,day=1;
	for(it=pinlei.begin();it!=pinlei.end();it++){
		string tmp=it->first;
		string date=tmp.substr(0,10);
		string typ=tmp.substr(10);
		//cout<
		int nowyear=(tmp[3]-'0');
		int nowmonth=(tmp[5]-'0')*10+tmp[6]-'0';
		int nowday=(tmp[8]-'0')*10+tmp[9]-'0';
		//cout<
		while(year<nowyear||(nowyear==year&&month<nowmonth)||(year==nowyear&&nowmonth==month&&day<nowday)){
			if(year>=3&&month>=7)break;
			outFile<<"202"<<year<<"-";
			if(month<10)outFile<<"0";
			outFile<<month<<"-";
			if(day<10)outFile<<"0";
			outFile<<day<<",";
			outFile<<"0,0"<<endl;
			day++;
			if(day>monthday[month-1]){
				month++;day=1;
			}
			if(month>12){
				year++;month=1;
			}
		}
		if(typ==typeName){
			node solve=it->second;
			outFile<<date<<","<<solve.price<<","<<solve.salesV<<endl;
			day++;
			if(day>monthday[month-1]){
				month++;day=1;
			}
			if(month>12){
				year++;month=1;
			}
		}
	}
	outFile.close();
	cout<<"success4";
	return 0;
}

各品类平均损耗率
这个处理得比较粗糙,直接对损耗率求平均,没有加权重

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e6+1,mod=1e9+7;

string typeName[6]={"花叶类","花菜类","水生根茎类","茄类","辣椒类","食用菌"};
struct node{
	double loss;
	int n;
	//bool discount;
}tp[6];
signed main(){
	IOS;
	//计算各品类的平均损耗率
	map<string,string> type;//code映射type 
	ifstream data1("annex1.csv",ios::in);
	string line1;
	istringstream sin1;
	vector<string> words1; 
    string word1;
    getline(data1, line1);
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        words1.clear();
        while (getline(sin1, word1, ',')) 
        {
        	words1.push_back(word1); 
        }
        type[words1[0]]=words1[3];
        //cout<
    }
    data1.close();
    cout<<"success1"<<endl; 
    
    ifstream data2("annex4.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        string tmp=words2[0]+" ";
        //cout<
		fer(i,0,6){
			if(type[tmp]==typeName[i]){
				tp[i].loss+=stod(words2[2]);
				tp[i].n++;
				//cout<
				break;
			} 
		}
        
    }
    data2.close();
    cout<<"success2"<<endl;
    
    ofstream outFile;
    outFile.open("typeLoss.csv",ios::out|ios::trunc);
    outFile<<"损耗率(%)与1-损耗率"<<endl;
    fer(i,0,6){
    	tp[i].loss/=tp[i].n;
    	outFile<<typeName[i]<<","<<tp[i].loss<<","<<1-tp[i].loss<<endl;
	}
	outFile.close();
	cout<<"success3"<<endl;
	return 0;
}

计算各品类的利润

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
//目标:六品类 每日 批发价格(卖货量*成本) 售价(卖货量*售价) 利润率 销售重量(总和)  最后再平均 
//日期+单品编号 映射 批发价格 售价 销量
//单品 映射 品类
//综合以上两个映射 算出 品类+日期 映射 批发价格 售价 销量

struct node{
	double cost,price,salesV,profitM;
};
signed main(){
	IOS;
	ifstream data1("annex3.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    vector<string> words1; 
    getline(data1, line1);

    map<string,node> mp;//记录日期+编码字符串单品映射的
    map<string,string> type;//code映射type 
    map<string,node> pinlei;
    
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        words1.clear();
        while (getline(sin1, word1, ','))
        {	
            words1.push_back(word1); 
        }
        string tmp=words1[0]+words1[1];
        struct node single;
        single.cost=stod(words1[2]);single.price=0;single.salesV=0;
        mp[tmp]=single;
    }
    data1.close();
	cout<<"success1"<<endl;
	
	ifstream data2("annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        string tmp=words2[0]+words2[2];
        struct node single=mp[tmp]; 
        single.price=stod(words2[4]);single.salesV+=stod(words2[3]);
        mp[tmp]=single;
    }
    data2.close();
    cout<<"success2"<<endl;
 
    ifstream data3("annex1.csv",ios::in);
	string line3;
	istringstream sin3;
	vector<string> words3; 
    string word3;
    getline(data3, line3);
    while (getline(data3, line3))
    {
        sin3.clear();
        sin3.str(line3);
        words3.clear();
        while (getline(sin3, word3, ',')) 
        {
        	words3.push_back(word3); 
        }
        type[words3[0]]=words3[3];
//        cout<
    }
    data3.close();
    cout<<"success3"<<endl;
    
    
    map<string,node>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
    	string key=it->first;
    	string date=key.substr(0,10);
    	string code=key.substr(10);
    	string tp=type[code];
    	string pinleiKey=date+tp;
    	if(pinlei.find(pinleiKey)==pinlei.end()){//没找见 
    		node solve;
    		solve.cost=it->second.cost * it->second.salesV;
    		solve.price=it->second.price*it->second.salesV;
    		solve.salesV=it->second.salesV;
    		pinlei[pinleiKey]=solve;
		}else{//找见了 
			node solve=pinlei[pinleiKey];
			solve.cost+=it->second.cost*it->second.salesV;
    		solve.price+=it->second.price*it->second.salesV;
    		solve.salesV+=it->second.salesV;
    		pinlei[pinleiKey]=solve;
		}
	}
	for(it=pinlei.begin();it!=pinlei.end();it++){
		node solve=it->second;
		solve.cost/=solve.salesV;solve.price/=solve.salesV;
		solve.profitM=(solve.price-solve.cost)/solve.cost;
		it->second=solve;
	}
    ofstream outFile;
    outFile.open("averageLaJiao.csv",ios::out|ios::trunc);
    string typeName="辣椒类";
    outFile<<typeName<<endl;
    outFile<<"日期,平均批发价格,平均销售价格,总销量(kg),利润率"<<endl;

	for(it=pinlei.begin();it!=pinlei.end();it++){
		string tmp=it->first;
		string date=tmp.substr(0,10);
		string typ=tmp.substr(10);
		if(typ==typeName){
			node solve=it->second;
			outFile<<date<<","<<solve.cost<<","<<solve.price<<","<<solve.salesV<<","<<solve.profitM<<endl;
		}
	}
	outFile.close();
	cout<<"success4";
	return 0;
}

问题三

问题三成本与售价

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
//计算售价与对应单价 
//struct goods{
//	string s[4];
//	int year,month,day;
//	int time; 
//	double price[36];
//}g[N];
signed main(){
	IOS;
	ifstream data1("annex3.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    vector<string> words1; 
    getline(data1, line1);

    map<string,string> mp;//记录日期+编码字符串映射的成本价
	map<string,string> mp2;//记录日期+编码字符串映射的售价 
    map<string,string> cdnm;//code映射name
    map<string,string> type;//code映射type 
    //int i=0;
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        words1.clear();
        while (getline(sin1, word1, ','))
        {	
            words1.push_back(word1); 
        }
        string tmp=words1[0]+words1[1];
        mp[tmp]=words1[2];
        //cout<
        //i++;
    }
    //cout<
    data1.close();
	cout<<"success1";
	
	ifstream data2("annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        string tmp=words2[0]+words2[2];
        mp2[tmp]=words2[4];
        //cout<
    }
    data2.close();
    cout<<"success2";
 
    ifstream data3("annex1.csv",ios::in);
	string line3;
	istringstream sin3;
	vector<string> words3; 
    string word3;
    getline(data3, line3);
    while (getline(data3, line3))
    {
        sin3.clear();
        sin3.str(line3);
        words3.clear();
        while (getline(sin3, word3, ',')) 
        {
        	words3.push_back(word3); 
        }
        cdnm[words3[0]]=words3[1];
        
        type[words3[0]]=words3[3];
//        cout<
    }
    data3.close();
    cout<<"success3";
//    map< string,double>::iterator it;
//    for(it=mp.begin();it!=mp.end();it++)cout<first<<" "<second<
	
//    ofstream outFile;
//    outFile.open("salesPrice2.csv",ios::out|ios::trunc);
//    outFile<<"日期,单品编码,单品名称,类别,批发价格,售价"<
    map<string,string>::iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		string tmp=it->first;
		string date=tmp.substr(0,10);
		string code=tmp.substr(10);
//		outFile<
//		//outFile<
//		outFile<
		cout<<date<<","<<code<<","<<cdnm[code]<<","<<type[code]<<","<<mp[tmp]<<","<<mp2[tmp]<<endl;
//		cout<first<<" "<second<
		break;
	}
	//outFile.close();
	cout<<"success4";
	return 0;
}

问题三单品销量

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=300,mod=1e9+7;
//目标:每个单品编号 名字(7天总和)品类 批发价格(卖货量*成本) 售价(卖货量*售价) 利润率 销售重量
//日期+单品编号 映射 批发价格 售价 销量 
//最后再处理平均和7天 
//单品 映射 品类
map<string,double> loss;
struct node{
	double cost,price,salesV,profitM;
};
signed main(){
	IOS;
	loss["花叶类"]=89.7197;
	loss["花菜类"]=85.858;
	loss["水生根茎类"]=88.0253;
	loss["茄类"]=92.878;
	loss["辣椒类"]=91.48467;
	loss["食用菌"]=91.86903;
	
	ifstream data1("624-630annex3.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    vector<string> words1; 

    map<string,node> mp;//记录日期+编码字符串单品映射的
    map<string,string> type;//code映射type 
    map<string,string> name;//code 映射name 
    map<string,node> mp2;//记录单品编码映射的node 
    
    //获取当日单品成本价 
    while (getline(data1, line1))
    {
        sin1.clear();
        sin1.str(line1);
        words1.clear();
        while (getline(sin1, word1, ','))
        {	
            words1.push_back(word1); 
        }
        string tmp=words1[0]+words1[1];
        //tmp.pop_back();
        //cout<
        node single;
        single.cost=stold(words1[2]);
		single.price=0;single.salesV=0;
        mp[tmp]=single;
        cout<<tmp<<","<<mp[tmp].cost<<endl;
    }
    data1.close();
	cout<<"success1"<<endl;
	
	ifstream data2("624-630annex2.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    while (getline(data2, line2))//获取当日单品销售额及销量 
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        string tmp=words2[0]+words2[2];
       // tmp.pop_back();
        struct node single=mp[tmp]; 
        single.price+=stod(words2[4])*stod(words2[3]);
		single.salesV+=stod(words2[3]);
        mp[tmp]=single;
        //cout<
    }
    data2.close();
    cout<<"success2"<<endl;
 
    ifstream data3("annex1.csv",ios::in);
	string line3;
	istringstream sin3;
	vector<string> words3; 
    string word3;
    getline(data3, line3);
    while (getline(data3, line3))//编号映射单品名和种类名 
    {
        sin3.clear();
        sin3.str(line3);
        words3.clear();
        while (getline(sin3, word3, ',')) 
        {
        	words3.push_back(word3); 
        }
        //words3[0].pop_back();
        type[words3[0]]=words3[3];
        name[words3[0]]=words3[1];
    }
    data3.close();
    cout<<"success3"<<endl;
    
    map<string,node>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
    	string key=it->first;
    	string date=key.substr(0,10);
    	string code=key.substr(10);
		//cout<
    	if(mp2.find(code)==mp2.end()){//没找见 
    		node solve;
    		solve.cost=it->second.cost * it->second.salesV;
    		solve.price=it->second.price;
    		solve.salesV=it->second.salesV;
    		mp2[code]=solve;
		}else{//找见了 
			node solve=mp2[code];
			solve.cost+=it->second.cost*it->second.salesV;
    		solve.price+=it->second.price;
    		solve.salesV+=it->second.salesV;
    		mp2[code]=solve;
		}
	}
	
	for(it=mp2.begin();it!=mp2.end();it++){
		node solve=it->second;
		if(solve.salesV!=0){
			solve.cost/=solve.salesV;solve.price/=solve.salesV;
			solve.profitM=(solve.price-solve.cost)/solve.cost;
			solve.salesV/=7;
			it->second=solve;
		}
		
	}
	
    ofstream outFile;
    outFile.open("Q3.csv",ios::out|ios::trunc);
    outFile<<"单品编号,单品名称,单品种类,平均批发价格,平均销售价格,总销量(kg),利润率,真实成本,每日总利润"<<endl;
	for(it=mp2.begin();it!=mp2.end();it++){
		node solve=it->second;
		if(solve.salesV!=0){
			string code=it->first;
			outFile<<code<<","<<name[code]<<","<<type[code]<<",";
			outFile<<solve.cost<<","<<solve.price<<","<<solve.salesV<<","<<solve.profitM<<",";
			outFile<<solve.cost/loss[type[code]]*100<<endl;
		}
	}
	outFile.close();
	cout<<"success4";
	return 0;
}

问题三品类对应

#include
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct node{
	double a[7];
}nd[28];
const int N=1e6+1,mod=1e9+7;
signed main(){
	IOS;
	map<string,string> type;//code映射type 
	
	ifstream data2("annex1.csv",ios::in);
	string line2;
	istringstream sin2;
	vector<string> words2; 
    string word2;
    getline(data2, line2);
    while (getline(data2, line2))
    {
        sin2.clear();
        sin2.str(line2);
        words2.clear();
        while (getline(sin2, word2, ',')) 
        {
        	words2.push_back(word2); 
        }
        type[words2[1]]=words2[3];
        //cout<
    }
    data2.close();
    cout<<"success2"<<endl;
    
	ifstream data1("type.csv",ios::in);
	string line1;
    istringstream sin1;    
    string word1;
    vector<string> words1; 
    getline(data1, line1);
    ofstream outFile;
    outFile.open("typeMatch.csv",ios::out|ios::trunc);
    outFile<<"单品名称,单品类型,总销量(kg),真实成本,实际利润率,平均销售价格,成本,销售额,利润"<<endl;
    while (getline(data1, line1))
    {
    	//cout<<1;
        sin1.clear();
        sin1.str(line1);
        words1.clear();
        while (getline(sin1, word1, ','))
        {	
            words1.push_back(word1); 
        }
        //cout<
        outFile<<words1[0]<<","<<type[words1[0]]<<",";
		fer(i,1,7)outFile<<words1[i]<<",";
		outFile<<words1[7]<<endl;
    }
    data1.close();
	cout<<"success1"<<endl;
	
    outFile.close();
    cout<<"success3"<<endl;
	return 0;
}

你可能感兴趣的:(数学建模,c语言)