- 代码预览
- 运行结果
- 全部代码
写在这里了
全部代码// // Created by Alan on 2022/5/6. // //#include#include #include #include #include #include #include using namespace std; class Sales { public: string serial; //流水号 int market{}; //门店号 string date; //日期 int sn{}; //购买商品序号 int id{}; //商品id float num{}; //购买数量 float price{}; //销售单价 };//构建三个cube的源文件的存储类 class cube_txt{ public: double ware_10010; double ware_10020; double ware_10030; double ware_10088; double ware_Sum_InOneDay; };//三个cube的存储类 string current_working_directory() { //在wsl环境下使用#include char *path; path = get_current_dir_name(); string Path = path; return Path; }//获取当前工作目录完整地址路径 //函数声明 void construct_Cube();//Cube三个维度用三个Cube.txt存储,调用一次construct——Cube(),自主选择构造一个Cube.txt ifstream get_infile(string path_from);//根据完整的路径得到文件的输入流(即输入内存的流) bool isConstructedAll();//判断三个cube_txt是否都构造完毕 void queryInOperation();//所有查询执行开始函数 void queryInCube();//三店七天四物总额 void queryInCube_M(string market);//某店七天四物总额 void queryInCube_S(string species);//某物三店七天总额 void queryInCube_D(string date);//某天三店四物总额 void queryInCube_MD(string market,string date);//某店某天总额 void queryInCube_MS(string market,string species);//某店某物七天总额 void queryInCube_DS(string date,string species);//某天某物三店总额 void queryInCube_MDS(string market,string date,string species);//某店某物某天销售额 //函数实现 int main() { int number = 1; while(number) { cout << "******************************选择操作**********************************" << endl; cout << "**************************construct the cube***************************" << endl; cout << "**************************query from cube******************************" << endl; cout <<"********************************exit************************************" << endl; string instruction; getline(cin,instruction); if(instruction == "construct the cube"){ construct_Cube(); //number++; } else if(instruction == "query from cube"){ bool symbol = false; symbol = isConstructedAll(); if(!symbol){ cout << "三个Cube.txt未全部构造完,请继续执行construct the cube" << endl; } else { queryInOperation(); } }else if(instruction == "exit"){ break; } } return 0; } void queryInOperation(){ string query_string_first; { cout << "Now select the first parameter of query Or Now query" << endl; cout << "1、Select the market,please input: 1019、1020、1021" << endl; cout << "2、Select the date,please input: 13、14、15、16、17、18、19" << endl; cout << "3、Select the species,please input: 10010、10020、10030、10088" << endl; cout << "4、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "5、Give Up And Exit please input: exit" << endl; } getline(cin,query_string_first); if(query_string_first == "Now query"){ queryInCube(); }else if(query_string_first == "1019" || query_string_first == "1020" || query_string_first == "1021"){ string query_string_M; { cout << "Now select the second parameter of query Or Now query" << endl; cout << "1、Select the date: 13、14、15、16、17、18、19" << endl; cout << "2、Select the species,please input: 10010、10020、10030、10088" << endl; cout << "3、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "4、Give Up And Exit please input: exit" << endl; } getline(cin,query_string_M); if(query_string_M == "Now query") { queryInCube_M(query_string_first); } else if(query_string_M == "13" || query_string_M == "14" || query_string_M == "15" || query_string_M == "16" || query_string_M == "17" || query_string_M == "18" || query_string_M == "19"){ { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the species: 10010、10020、10030、10088" << endl; cout << "2、Not select, Based on the Current situation And Begin query : Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_MD; getline(cin,query_string_MD); if(query_string_MD == "Now query"){ queryInCube_MD(query_string_first,query_string_M); } else if(query_string_MD == "10010" || query_string_MD == "10020" || query_string_MD == "10030" || query_string_MD == "10088"){ { cout << "1、All parameters are selected, Now query ?" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_first,query_string_M,query_string_MD); } else if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_MD == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_M == "10010" || query_string_M == "10020" || query_string_M == "10030" || query_string_M == "10088"){ { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the date,please input: 13、14、15、16、17、18、19" << endl; cout << "2、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_MD; getline(cin,query_string_MD); if(query_string_MD == "Now query"){ queryInCube_MS(query_string_first,query_string_M); } else if(query_string_MD == "13" || query_string_MD == "14" || query_string_MD == "15" || query_string_MD == "16" || query_string_MD == "17" || query_string_MD == "18" || query_string_MD == "19"){ { cout << "1、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_first,query_string_MD,query_string_M); } else if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_MD == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } }else if(query_string_first == "13" || query_string_first == "14" || query_string_first == "15" || query_string_first == "16" ||query_string_first == "17" || query_string_first == "18" || query_string_first == "19" ) { { cout << "Now select the second parameter of query Or Now query" << endl; cout << "1、Select the market,please input: 1019、1020、1021" << endl; cout << "2、Select the species,please input: 10010、10020、10030、10088" << endl; cout << "3、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "4、Give Up And Exit please input: exit" << endl; } string query_string_D; getline(cin,query_string_D); if(query_string_D == "Now query"){ queryInCube_D(query_string_first); } else if(query_string_D == "1019" || query_string_D == "1020" || query_string_D == "1021") { { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the species,please input: 10010、10020、10030、10088" << endl; cout << "2、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_DM; getline(cin,query_string_DM); if(query_string_DM == "Now query"){ queryInCube_MD(query_string_D,query_string_first); } else if(query_string_DM == "10010" || query_string_DM == "10020" || query_string_DM == "10030" || query_string_DM == "10088"){ { cout << "1、All parameters are selected, Now query ?" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_D,query_string_first,query_string_DM); } else if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_DM == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_D == "10010" || query_string_D == "10020" || query_string_D == "10030" || query_string_D == "10088"){ { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the market,please input: 1019、1020、1021" << endl; cout << "2、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_DS; getline(cin,query_string_DS); if(query_string_DS == "Now query"){ queryInCube_DS(query_string_first,query_string_D); } else if(query_string_DS == "1019" || query_string_DS == "1020" || query_string_DS == "1021"){ { cout << "1、All parameters are selected, Now query ?" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_DS,query_string_first,query_string_D); } if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_DS == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_D == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } }else if(query_string_first == "10010" || query_string_first == "10020" || query_string_first == "10030" || query_string_first == "10088"){ { cout << "Now select the second parameter of query Or Now query" << endl; cout << "1、Select the market,please input: 1019、1020、1021" << endl; cout << "2、Select the date,please input: 13、14、15、16、17、18、19" << endl; cout << "3、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "4、Give Up And Exit please input: exit" << endl; } string query_string_S; getline(cin,query_string_S); if(query_string_S == "Now query"){ queryInCube_S(query_string_first); } else if(query_string_S == "1019" || query_string_S == "1020" || query_string_S == "1021"){ { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the date,please input: 13、14、15、16、17、18、19" << endl; cout << "2、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_SM; getline(cin,query_string_SM); if(query_string_SM == "Now query"){ queryInCube_MS(query_string_S,query_string_first); } else if(query_string_SM == "13" || query_string_SM == "14" || query_string_SM == "15" || query_string_SM == "16" || query_string_SM == "17" || query_string_SM == "18" || query_string_SM == "19"){ { cout << "1、All parameters are selected, Now query ?" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_S,query_string_SM,query_string_first); } if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_SM == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_S == "13" || query_string_S == "14" || query_string_S == "15" || query_string_S == "16" || query_string_S == "17" || query_string_S == "18" || query_string_S == "19"){ { cout << "Now select the third parameter of query Or Now query" << endl; cout << "1、Select the market,please input: 1019、1020、1021" << endl; cout << "2、Not select, Based on the Current situation And Begin query please input: Now query" << endl; cout << "3、Give Up And Exit please input: exit" << endl; } string query_string_SD; getline(cin,query_string_SD); if(query_string_SD == "Now query"){ queryInCube_DS(query_string_S,query_string_first); } if(query_string_SD == "1019" || query_string_SD == "1020" || query_string_SD == "1021"){ { cout << "1、All parameters are selected, Now query ?" << endl; cout << "2、Give Up And Exit please input: exit" << endl; } string query_string_end; getline(cin,query_string_end); if(query_string_end == "Now query"){ queryInCube_MDS(query_string_SD,query_string_S,query_string_first); } if(query_string_end == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } if(query_string_SD == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } } else if(query_string_S == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } }else if(query_string_first == "exit"){ cout << "Now exit from the queryInOperation()" << endl; return; } else { cout << "输入非法!!!退出查询" << endl; } }//所有查询执行开始函数 bool isConstructedAll(){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string from_txt_1019 = "cube_1019.txt"; string from_txt_1020 = "cube_1020.txt"; string from_txt_1021 = "cube_1021.txt"; string path_from_1019 = path_first + from_txt_1019; string path_from_1020 = path_first + from_txt_1020; string path_from_1021 = path_first + from_txt_1021; ifstream infile_1019 = get_infile(path_from_1019); ifstream infile_1020 = get_infile(path_from_1020); ifstream infile_1021 = get_infile(path_from_1021); string head_txt[4]={}; if(!infile_1019.eof()){ infile_1019 >> head_txt[0]; } if(!infile_1020.eof()){ infile_1020 >> head_txt[1]; } if(!infile_1021.eof()){ infile_1021 >> head_txt[2]; } if(head_txt[0].empty() || head_txt[1].empty() || head_txt[2].empty()){ return false; } else{ return true; } }//判断三个cube_txt是否都构造完毕 ifstream get_infile(string path_from){ ifstream infile(path_from, ios::in); if (infile.fail()) { cout << "error open!" << path_from << "打开发生错误,无法正常打开" << endl; exit(1); } else{ return infile; } }//获取指定路径位置的ifstream流对象 void queryInCube(){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string from_txt_1019 = "cube_1019.txt"; string from_txt_1020 = "cube_1020.txt"; string from_txt_1021 = "cube_1021.txt"; string path_from_1019 = path_first + from_txt_1019; string path_from_1020 = path_first + from_txt_1020; string path_from_1021 = path_first + from_txt_1021; ifstream infile_1019 = get_infile(path_from_1019); ifstream infile_1020 = get_infile(path_from_1020); ifstream infile_1021 = get_infile(path_from_1021); cube_txt cubeTxt_1019[32]; cube_txt cubeTxt_1020[32]; cube_txt cubeTxt_1021[32]; int cubeTxt_num_1019 = 0; int cubeTxt_num_1020 = 0; int cubeTxt_num_1021 = 0; string head_txt[4]; if(infile_1019.fail()){ cout << from_txt_1019 << "文件打开失败,查询失败" << endl; return; } if(infile_1020.fail()){ cout << from_txt_1020 << "文件打开失败,查询失败" << endl; return; } if(infile_1021.fail()){ cout << from_txt_1021 << "文件打开失败,查询失败" << endl; return; } if(!infile_1019.eof()){ infile_1019 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while(!infile_1019.eof()){ infile_1019 >> cubeTxt_1019[cubeTxt_num_1019].ware_10010 >> cubeTxt_1019[cubeTxt_num_1019].ware_10020 >> cubeTxt_1019[cubeTxt_num_1019].ware_10030 >> cubeTxt_1019[cubeTxt_num_1019].ware_10088 >> cubeTxt_1019[cubeTxt_num_1019].ware_Sum_InOneDay; cubeTxt_num_1019++; } if(!infile_1020.eof()){ infile_1020 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while(!infile_1020.eof()){ infile_1020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10010 >> cubeTxt_1020[cubeTxt_num_1020].ware_10020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10030 >> cubeTxt_1020[cubeTxt_num_1020].ware_10088 >> cubeTxt_1020[cubeTxt_num_1020].ware_Sum_InOneDay; cubeTxt_num_1020++; } if(!infile_1021.eof()){ infile_1021 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while(!infile_1021.eof()){ infile_1021 >> cubeTxt_1021[cubeTxt_num_1021].ware_10010 >> cubeTxt_1021[cubeTxt_num_1021].ware_10020 >> cubeTxt_1021[cubeTxt_num_1021].ware_10030 >> cubeTxt_1021[cubeTxt_num_1021].ware_10088 >> cubeTxt_1021[cubeTxt_num_1021].ware_Sum_InOneDay; cubeTxt_num_1021++; } cubeTxt_num_1019 = cubeTxt_num_1019 - 2; cubeTxt_num_1020 = cubeTxt_num_1020 - 2; cubeTxt_num_1021 = cubeTxt_num_1021 - 2; double total_All; total_All = cubeTxt_1019[cubeTxt_num_1019].ware_Sum_InOneDay + cubeTxt_1020[cubeTxt_num_1020].ware_Sum_InOneDay + cubeTxt_1021[cubeTxt_num_1021].ware_Sum_InOneDay; cout << "1019、1020、1021三家店,在13-19七天的总销售额为:" << fixed << setprecision(2) << total_All << endl; }//三店七天四物总额 void queryInCube_M(string market){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string path_from = "cube_" + market + ".txt"; ifstream infile = get_infile(path_from); cube_txt cubeTxt[32]; int cubeTxt_num = 0; string head_txt[4]; if(infile.fail()){ cout << market << "文件打开失败,查询失败" << endl; return; } if(!infile.eof()){ infile >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while(!infile.eof()){ infile >> cubeTxt[cubeTxt_num].ware_10010 >> cubeTxt[cubeTxt_num].ware_10020 >> cubeTxt[cubeTxt_num].ware_10030 >> cubeTxt[cubeTxt_num].ware_10088 >>cubeTxt[cubeTxt_num].ware_Sum_InOneDay; cubeTxt_num++; } cubeTxt_num = cubeTxt_num - 2; double total = cubeTxt[cubeTxt_num].ware_Sum_InOneDay; cout << market << "这家店13-19七天里商品10010、10020、10030、10088的销售总额为:" << total << endl; }//某店七天四物总额 void queryInCube_S(string species){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string from_txt_1019 = "cube_1019.txt"; string from_txt_1020 = "cube_1020.txt"; string from_txt_1021 = "cube_1021.txt"; string path_from_1019 = path_first + from_txt_1019; string path_from_1020 = path_first + from_txt_1020; string path_from_1021 = path_first + from_txt_1021; ifstream infile_1019 = get_infile(path_from_1019); ifstream infile_1020 = get_infile(path_from_1020); ifstream infile_1021 = get_infile(path_from_1021); cube_txt cubeTxt_1019[32]; cube_txt cubeTxt_1020[32]; cube_txt cubeTxt_1021[32]; int cubeTxt_num_1019 = 0; int cubeTxt_num_1020 = 0; int cubeTxt_num_1021 = 0; string head_txt[4]; if(infile_1019.fail()){ cout << from_txt_1019 << "文件打开失败,查询失败" << endl; return; } if(infile_1020.fail()){ cout << from_txt_1020 << "文件打开失败,查询失败" << endl; return; } if(infile_1021.fail()){ cout << from_txt_1021 << "文件打开失败,查询失败" << endl; return; } if (!infile_1019.eof()) { infile_1019 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1019.eof()) { infile_1019 >> cubeTxt_1019[cubeTxt_num_1019].ware_10010 >> cubeTxt_1019[cubeTxt_num_1019].ware_10020 >> cubeTxt_1019[cubeTxt_num_1019].ware_10030 >> cubeTxt_1019[cubeTxt_num_1019].ware_10088 >> cubeTxt_1019[cubeTxt_num_1019].ware_Sum_InOneDay; cubeTxt_num_1019++; } if (!infile_1020.eof()) { infile_1020 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1020.eof()) { infile_1020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10010 >> cubeTxt_1020[cubeTxt_num_1020].ware_10020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10030 >> cubeTxt_1020[cubeTxt_num_1020].ware_10088 >> cubeTxt_1020[cubeTxt_num_1020].ware_Sum_InOneDay; cubeTxt_num_1020++; } if (!infile_1021.eof()) { infile_1021 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1021.eof()) { infile_1021 >> cubeTxt_1021[cubeTxt_num_1021].ware_10010 >> cubeTxt_1021[cubeTxt_num_1021].ware_10020 >> cubeTxt_1021[cubeTxt_num_1021].ware_10030 >> cubeTxt_1021[cubeTxt_num_1021].ware_10088 >> cubeTxt_1021[cubeTxt_num_1021].ware_Sum_InOneDay; cubeTxt_num_1021++; } cubeTxt_num_1019 = cubeTxt_num_1019 - 2; cubeTxt_num_1020 = cubeTxt_num_1020 - 2; cubeTxt_num_1021 = cubeTxt_num_1021 - 2; double total = 0; if(species == "10010"){ total = cubeTxt_1019[cubeTxt_num_1019].ware_10010 + cubeTxt_1020[cubeTxt_num_1020].ware_10010 + cubeTxt_1021[cubeTxt_num_1021].ware_10010; cout << "商品:" << species << "在1019、1020、1021三家店,从13-19七天的销售总额为:" < total = cubeTxt_1019[cubeTxt_num_1019].ware_10020 + cubeTxt_1020[cubeTxt_num_1020].ware_10020 + cubeTxt_1021[cubeTxt_num_1021].ware_10020; cout << "商品:" << species << "在1019、1020、1021三家店,从13-19七天的销售总额为:" << fixed << setprecision(2) << total << endl; } else if(species == "10030"){ total = cubeTxt_1019[cubeTxt_num_1019].ware_10030 + cubeTxt_1020[cubeTxt_num_1020].ware_10030 + cubeTxt_1021[cubeTxt_num_1021].ware_10030; cout << "商品:" << species << "在1019、1020、1021三家店,从13-19七天的销售总额为:" << fixed << setprecision(2) << total << endl; } else if(species == "10088"){ total = cubeTxt_1019[cubeTxt_num_1019].ware_10088 + cubeTxt_1020[cubeTxt_num_1020].ware_10088 + cubeTxt_1021[cubeTxt_num_1021].ware_10088; cout << "商品:" << species << "在1019、1020、1021三家店,从13-19七天的销售总额为:" << fixed << setprecision(2) << total << endl; } else{ cout << "species输入非法" << endl; } }//某物三店七天总额 void queryInCube_D(string date){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string from_txt_1019 = "cube_1019.txt"; string from_txt_1020 = "cube_1020.txt"; string from_txt_1021 = "cube_1021.txt"; string path_from_1019 = path_first + from_txt_1019; string path_from_1020 = path_first + from_txt_1020; string path_from_1021 = path_first + from_txt_1021; ifstream infile_1019 = get_infile(path_from_1019); ifstream infile_1020 = get_infile(path_from_1020); ifstream infile_1021 = get_infile(path_from_1021); cube_txt cubeTxt_1019[32]; cube_txt cubeTxt_1020[32]; cube_txt cubeTxt_1021[32]; int cubeTxt_num_1019 = 0; int cubeTxt_num_1020 = 0; int cubeTxt_num_1021 = 0; string head_txt[4]; if(infile_1019.fail()){ cout << from_txt_1019 << "文件打开失败,查询失败" << endl; return; } if(infile_1020.fail()){ cout << from_txt_1020 << "文件打开失败,查询失败" << endl; return; } if(infile_1021.fail()){ cout << from_txt_1021 << "文件打开失败,查询失败" << endl; return; } if (!infile_1019.eof()) { infile_1019 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1019.eof()) { infile_1019 >> cubeTxt_1019[cubeTxt_num_1019].ware_10010 >> cubeTxt_1019[cubeTxt_num_1019].ware_10020 >> cubeTxt_1019[cubeTxt_num_1019].ware_10030 >> cubeTxt_1019[cubeTxt_num_1019].ware_10088 >> cubeTxt_1019[cubeTxt_num_1019].ware_Sum_InOneDay; cubeTxt_num_1019++; } if (!infile_1020.eof()) { infile_1020 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1020.eof()) { infile_1020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10010 >> cubeTxt_1020[cubeTxt_num_1020].ware_10020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10030 >> cubeTxt_1020[cubeTxt_num_1020].ware_10088 >> cubeTxt_1020[cubeTxt_num_1020].ware_Sum_InOneDay; cubeTxt_num_1020++; } if (!infile_1021.eof()) { infile_1021 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1021.eof()) { infile_1021 >> cubeTxt_1021[cubeTxt_num_1021].ware_10010 >> cubeTxt_1021[cubeTxt_num_1021].ware_10020 >> cubeTxt_1021[cubeTxt_num_1021].ware_10030 >> cubeTxt_1021[cubeTxt_num_1021].ware_10088 >> cubeTxt_1021[cubeTxt_num_1021].ware_Sum_InOneDay; cubeTxt_num_1021++; } cubeTxt_num_1019 = cubeTxt_num_1019 - 2; cubeTxt_num_1020 = cubeTxt_num_1020 - 2; cubeTxt_num_1021 = cubeTxt_num_1021 - 2; double total = 0; if(!date.empty() && date.size()==2) { char weekday = date[1]; int week_day_num = (int)(weekday - '0'); if(week_day_num >=3 && week_day_num <=9){ week_day_num = week_day_num - 3; total = cubeTxt_1019[week_day_num].ware_Sum_InOneDay + cubeTxt_1020[week_day_num].ware_Sum_InOneDay + cubeTxt_1021[week_day_num].ware_Sum_InOneDay; cout << "在" << date << "这天,1019、1020、1021三家店的10010、10020、10030、10088四种商品的销售总额为:" << fixed << setprecision(2) << total << endl; } else{ cout << "date输入非法" << endl; } } else{ cout << "date输入非法" << endl; } }//某天三店四物总额 void queryInCube_MD(string market,string date){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string path_from = "cube_" + market + ".txt"; ifstream infile = get_infile(path_from); if(infile.fail()){ cout << market << "文件打开失败,查询失败" << endl; return; } cube_txt cubeTxt[32]; int cubeTxt_num = 0; string head_txt[4]; if (!infile.eof()) { infile >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile.eof()) { infile >> cubeTxt[cubeTxt_num].ware_10010 >> cubeTxt[cubeTxt_num].ware_10020 >> cubeTxt[cubeTxt_num].ware_10030 >> cubeTxt[cubeTxt_num].ware_10088 >> cubeTxt[cubeTxt_num].ware_Sum_InOneDay; cubeTxt_num++; } cubeTxt_num = cubeTxt_num - 2; double total = 0; if(!date.empty() && date.size()==2) { char weekday = date[1]; int week_day_num = (int)(weekday - '0'); if(week_day_num >=3 && week_day_num <=9){ week_day_num = week_day_num - 3; total = cubeTxt[week_day_num].ware_Sum_InOneDay; cout << "在" << date << "这天," << market <<"这家店的10010、10020、10030、10088四种商品的销售总额为:" << fixed << setprecision(2) << total << endl; } else{ cout << "date输入非法" << endl; } } else{ cout << "date输入非法" << endl; } }//某店某天总额 void queryInCube_MS(string market,string species){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string path_from = "cube_" + market + ".txt"; ifstream infile = get_infile(path_from); if(infile.fail()){ cout << market << "文件打开失败,查询失败" << endl; return; } cube_txt cubeTxt[32]; int cubeTxt_num = 0; string head_txt[4]; if (!infile.eof()) { infile >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile.eof()) { infile >> cubeTxt[cubeTxt_num].ware_10010 >> cubeTxt[cubeTxt_num].ware_10020 >> cubeTxt[cubeTxt_num].ware_10030 >> cubeTxt[cubeTxt_num].ware_10088 >> cubeTxt[cubeTxt_num].ware_Sum_InOneDay; cubeTxt_num++; } cubeTxt_num = cubeTxt_num - 2; double total = 0; if(species == "10010"){ total = cubeTxt[cubeTxt_num].ware_10010; cout << "商品:" << species << "在" << market <<"这家店,从13-19七天的销售总额为:" < total = cubeTxt[cubeTxt_num].ware_10020; cout << "商品:" << species << "在" << market <<"这家店,从13-19七天的销售总额为:" < total = cubeTxt[cubeTxt_num].ware_10030; cout << "商品:" << species << "在" << market <<"这家店,从13-19七天的销售总额为:" < total = cubeTxt[cubeTxt_num].ware_10088; cout << "商品:" << species << "在" << market <<"这家店,从13-19七天的销售总额为:" < cout << "species输入非法" << endl; return; } }//某店某物七天总额 void queryInCube_DS(string date,string species){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string from_txt_1019 = "cube_1019.txt"; string from_txt_1020 = "cube_1020.txt"; string from_txt_1021 = "cube_1021.txt"; string path_from_1019 = path_first + from_txt_1019; string path_from_1020 = path_first + from_txt_1020; string path_from_1021 = path_first + from_txt_1021; ifstream infile_1019 = get_infile(path_from_1019); ifstream infile_1020 = get_infile(path_from_1020); ifstream infile_1021 = get_infile(path_from_1021); cube_txt cubeTxt_1019[32]; cube_txt cubeTxt_1020[32]; cube_txt cubeTxt_1021[32]; int cubeTxt_num_1019 = 0; int cubeTxt_num_1020 = 0; int cubeTxt_num_1021 = 0; string head_txt[4]; if(infile_1019.fail()){ cout << from_txt_1019 << "文件打开失败,查询失败" << endl; return; } if(infile_1020.fail()){ cout << from_txt_1020 << "文件打开失败,查询失败" << endl; return; } if(infile_1021.fail()){ cout << from_txt_1021 << "文件打开失败,查询失败" << endl; return; } if (!infile_1019.eof()) { infile_1019 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1019.eof()) { infile_1019 >> cubeTxt_1019[cubeTxt_num_1019].ware_10010 >> cubeTxt_1019[cubeTxt_num_1019].ware_10020 >> cubeTxt_1019[cubeTxt_num_1019].ware_10030 >> cubeTxt_1019[cubeTxt_num_1019].ware_10088 >> cubeTxt_1019[cubeTxt_num_1019].ware_Sum_InOneDay; cubeTxt_num_1019++; } if (!infile_1020.eof()) { infile_1020 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1020.eof()) { infile_1020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10010 >> cubeTxt_1020[cubeTxt_num_1020].ware_10020 >> cubeTxt_1020[cubeTxt_num_1020].ware_10030 >> cubeTxt_1020[cubeTxt_num_1020].ware_10088 >> cubeTxt_1020[cubeTxt_num_1020].ware_Sum_InOneDay; cubeTxt_num_1020++; } if (!infile_1021.eof()) { infile_1021 >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile_1021.eof()) { infile_1021 >> cubeTxt_1021[cubeTxt_num_1021].ware_10010 >> cubeTxt_1021[cubeTxt_num_1021].ware_10020 >> cubeTxt_1021[cubeTxt_num_1021].ware_10030 >> cubeTxt_1021[cubeTxt_num_1021].ware_10088 >> cubeTxt_1021[cubeTxt_num_1021].ware_Sum_InOneDay; cubeTxt_num_1021++; } cubeTxt_num_1019 = cubeTxt_num_1019 - 2; cubeTxt_num_1020 = cubeTxt_num_1020 - 2; cubeTxt_num_1021 = cubeTxt_num_1021 - 2; double total = 0; char weekday; int week_day_num; if(!date.empty() && date.size()==2) { weekday = date[1]; week_day_num = (int)(weekday - '0'); if(week_day_num >=3 && week_day_num <=9){ week_day_num = week_day_num - 3; } else{ cout << "date输入非法" << endl; return; } } else{ cout << "date输入非法" << endl; return; } if(species == "10010"){ total = cubeTxt_1019[week_day_num].ware_10010 + cubeTxt_1020[week_day_num].ware_10010 + cubeTxt_1021[week_day_num].ware_10010; cout << "商品:" << species << "在" <<"1019、1020、1021三家店," << date << "日这天的销售总额为:" < total = cubeTxt_1019[week_day_num].ware_10020 + cubeTxt_1020[week_day_num].ware_10020 + cubeTxt_1021[week_day_num].ware_10020; cout << "商品:" << species << "在" <<"1019、1020、1021三家店," << date << "日这天的销售总额为:" < total = cubeTxt_1019[week_day_num].ware_10030 + cubeTxt_1020[week_day_num].ware_10030 + cubeTxt_1021[week_day_num].ware_10030; cout << "商品:" << species << "在" <<"1019、1020、1021三家店," << date << "日这天的销售总额为:" < total = cubeTxt_1019[week_day_num].ware_10088 + cubeTxt_1020[week_day_num].ware_10088 + cubeTxt_1021[week_day_num].ware_10088; cout << "商品:" << species << "在" <<"1019、1020、1021三家店," << date << "日这天的销售总额为:" < cout << "species输入非法" << endl; } }//某天某物三店总额 void queryInCube_MDS(string market,string date,string species){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; //选店 string path_from = "cube_" + market + ".txt"; ifstream infile = get_infile(path_from); if(infile.fail()){ cout << market << "文件打开失败,查询失败" << endl; return; } cube_txt cubeTxt[32]; int cubeTxt_num = 0; string head_txt[4]; if (!infile.eof()) { infile >> head_txt[0] >> head_txt[1] >> head_txt[2] >> head_txt[3]; } while (!infile.eof()) { infile >> cubeTxt[cubeTxt_num].ware_10010 >> cubeTxt[cubeTxt_num].ware_10020 >> cubeTxt[cubeTxt_num].ware_10030 >> cubeTxt[cubeTxt_num].ware_10088 >> cubeTxt[cubeTxt_num].ware_Sum_InOneDay; cubeTxt_num++; } cubeTxt_num = cubeTxt_num - 2; double total = 0; //选天 char weekday; int week_day_num; if(!date.empty() && date.size()==2) { weekday = date[1]; week_day_num = (int)(weekday - '0'); if(week_day_num >=3 && week_day_num <=9){ week_day_num = week_day_num - 3; } else{ cout << "date输入非法" << endl; return; } } else{ cout << "date输入非法" << endl; return; } //选物 if(species == "10010"){ total = cubeTxt[week_day_num].ware_10010; cout << "商品:" << species << "在" << market <<"这家店,在" << date << "这天的销售总额为:" < total = cubeTxt[week_day_num].ware_10020; cout << "商品:" << species << "在" << market <<"这家店,在" << date << "这天的销售总额为:" < total = cubeTxt[week_day_num].ware_10030; cout << "商品:" << species << "在" << market << "这家店,在" << date << "这天的销售总额为:" < total = cubeTxt[week_day_num].ware_10088; cout << "商品:" << species << "在" << market << "这家店,在" << date << "这天的销售总额为:" < cout << "species输入非法" << endl; return; } }//某店某物某天销售额 void construct_Cube(){ string path_first = current_working_directory(); string x = "\"; //而如果在Linux环境下,根本没有direct.h这个头文件,可以使用unistd.h的get_current_dir_name()方法来获取当前工作目录 //但是注意:在Linux环境下整个工作目录都被解析成以 '/'分隔的路径,所以要执行: +'/textname', // 如下所示: path_first = path_first + "/"; string path_from; string path_to; string from_txt; string to_txt; cout << "请输入获取源数据的txt文件的文件名(fl_1019.txt、fl_1020.txt、fl_1021.txt):“xxx.txt” n"; getline(cin, from_txt); if (!from_txt.empty()) { path_from = path_first + from_txt; cout << "获取数据的文件名为:" << path_from << endl; } else { cout << "输入获取数据的文件名时:存在非法输入"; exit(1); } cout << "请输入存放cube数据的的txt文件的文件名(cube_1019.txt,cube_1020.txt,cube_1021.txt):“xxx.txt” n"; getline(cin, to_txt); if (!to_txt.empty()) { path_to = path_first + to_txt; cout << "存放数据文件的绝对路径为:" << path_to << endl; } else { cout << "输入存放数据文件名时:存在非法输入"; exit(1); } ifstream infile(path_from, ios::in); if (infile.fail()) { cout << "error open!" << from_txt << "打开发生错误,无法正常打开" << endl; exit(1); } ofstream outfile(path_to, ios::out); if (!outfile) { cout << "open error!" << to_txt << "打开发生错误,无法正常打开" << endl; exit(1); } Sales sal[10000]; int sal_size = 0; string table_head[7]; //读入第一行 if (!infile.eof()) { infile >> table_head[0] >> table_head[1] >> table_head[2] >> table_head[3] >> table_head[4] >> table_head[5] >> table_head[6]; printf("先读出第一行表头如下:n"); for (int i = 0; i < 7; i++) { printf("%st", table_head[i].c_str()); } printf("n"); } //读入第二行开始的数据 while (!infile.eof()) { if (sal_size == 10000) { cout << "默认处理数据10000条,而此数据文件有大于10000条数据,只处理到前10000条,后面数据不读入不处理n"; break; } infile >> sal[sal_size].serial >> sal[sal_size].market >> sal[sal_size].date >> sal[sal_size].sn >> sal[sal_size].id >> sal[sal_size].num >> sal[sal_size].price; //sal[sal_size].print(); sal_size++; } double total[8][5] = {0}; sal_size = sal_size - 1; cout << "构建该cube的源数据文件为:" << path_from << ",其共有" << sal_size << "行数据记录n"; //total数组,前七行代表七天,前四列代表四个商品种类, // total的前七行四列每个元素存放某商品种类某天销售额总和 //第八行存放某个商品种类一星期七天销售额总和 //第五列存放某天四个商品种类的销售额总和 //存放前七行四列元素 for (int i = 0; i < sal_size; i++) { char p = sal[i].date[7]; if (sal[i].id / 100 == 10010) { switch (p) { case '3': { total[0][0] += sal[i].num * sal[i].price; break; } case '4': { total[1][0] += sal[i].num * sal[i].price; break; } case '5': { total[2][0] += sal[i].num * sal[i].price; break; } case '6': { total[3][0] += sal[i].num * sal[i].price; break; } case '7': { total[4][0] += sal[i].num * sal[i].price; break; } case '8': { total[5][0] += sal[i].num * sal[i].price; break; } case '9': { total[6][0] += sal[i].num * sal[i].price; break; } default: break; } } else if (sal[i].id / 100 == 10020) { switch (p) { case '3': { total[0][1] += sal[i].num * sal[i].price; break; } case '4': { total[1][1] += sal[i].num * sal[i].price; break; } case '5': { total[2][1] += sal[i].num * sal[i].price; break; } case '6': { total[3][1] += sal[i].num * sal[i].price; break; } case '7': { total[4][1] += sal[i].num * sal[i].price; break; } case '8': { total[5][1] += sal[i].num * sal[i].price; break; } case '9': { total[6][1] += sal[i].num * sal[i].price; break; } default: break; } } else if (sal[i].id / 100 == 10030) { switch (p) { case '3': { total[0][2] += sal[i].num * sal[i].price; break; } case '4': { total[1][2] += sal[i].num * sal[i].price; break; } case '5': { total[2][2] += sal[i].num * sal[i].price; break; } case '6': { total[3][2] += sal[i].num * sal[i].price; break; } case '7': { total[4][2] += sal[i].num * sal[i].price; break; } case '8': { total[5][2] += sal[i].num * sal[i].price; break; } case '9': { total[6][2] += sal[i].num * sal[i].price; break; } default: break; } } else if (sal[i].id / 100 == 10088) { switch (p) { case '3': { total[0][3] += sal[i].num * sal[i].price; break; } case '4': { total[1][3] += sal[i].num * sal[i].price; break; } case '5': { total[2][3] += sal[i].num * sal[i].price; break; } case '6': { total[3][3] += sal[i].num * sal[i].price; break; } case '7': { total[4][3] += sal[i].num * sal[i].price; break; } case '8': { total[5][3] += sal[i].num * sal[i].price; break; } case '9': { total[6][3] += sal[i].num * sal[i].price; break; } default: break; } } } //存放第五列元素 double total_day_In_AllId = 0; for (int i = 0; i < 7; i++) { total_day_In_AllId = 0; for (int j = 0; j < 4; j++) { total_day_In_AllId += total[i][j]; if (j == 3) { total[i][j + 1] = total_day_In_AllId; break; } } } //存放第八行元素 for (int j = 0; j < 4; j++) { double total_week_In_OneId = 0; for (int i = 0; i < 7; i++) { total_week_In_OneId += total[i][j]; if (i == 6) { total[i + 1][j] = total_week_In_OneId; break; } } } double total_1 = 0; double total_2 = 0; for (int i = 0; i < 7; i++) { total_1 += total[i][4]; } for (int j = 0; j < 4; j++) { total_2 += total[7][j]; } //计算该店一星期的四种商品销售额总和(理想情况下第八行的和等于第四列的和等于我们要的这个销售额总和) // 但是由于double类型乘除法存在误差,所以这里将行的和与列的和相加除以二 total[7][4] = (total_1 + total_2) / 2.0; //#include 实现对double数据指定保留位数输出 // << fixed << setprecision(n) << data << outfile << "10010油 t" << "10020面制品 t" << "10030米和粉 t" << "10088其他粮油 t" << endl; for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { outfile << fixed << setprecision(2) << total[i][j] << " t"; } outfile << endl; } cout << path_to <<"的cube构建完毕" << endl; infile.close();//关闭文件 outfile.close();//关闭文件 }//Cube三个维度用三个Cube.txt存储,调用一次construct——Cube(),自主选择构造一个Cube.txt



