我几乎完成了我的程序,但我遇到了一个我似乎无法解决的问题。 我如何获得最低消费?
我的讲师给了我任务:
您工作的公司每月收到一份文本格式的报告。 该报告包含以下信息。
部门名称 部门负责人姓名 月 本月最低消费 本月的最大支出您的程序是从用户获取输入文件的名称。 实现一个结构来表示数据:
一旦文件被读入你的程序,打印出用户的以下统计数据:
列出每个月每月最低消费的部门 列出哪个部门逐月开支最低将信息写入名为“MaxMin.txt”的文件
然后进行处理,使部门名称,部门名称负责人,最低支出和最大支出根据月份(例如1月,2月,3月等)写入单独的文件。
以下是我得到的文本文件:
Engineering Bill Jan 2000 15000 IT Jack Jan 300 20000 HR Jill Jan 1500 10000 Engineering Bill Feb 5000 45000 IT Jack Feb 4500 7000 HR Jill Feb 5600 60000 Engineering Bill Mar 5000 45000 IT Jack Mar 4500 7000 HR Jill Mar 5600 60000 Engineering Bill Apr 5000 45000 IT Jack Apr 4500 7000 HR Jill Apr 5600 60000 Engineering Bill May 2000 15000 IT Jack May 300 20000 HR Jill May 1500 10000 Engineering Bill Jun 2000 15000 IT Jack Jun 300 20000 HR Jill Jun 1500 10000这里是我写的c ++代码
ue#include <iostream> #include <fstring> #include <string> using namespace std; struct Record { string depName; string head; string month; float max; float min; string name; } myRecord[19]; int main () { string line; ofstream minmax,jan,feb,mar,apr,may,jun; char a[50]; char b[50]; int i = 0,j,k; float temp; //float maxjan=myRecord[0].max,maxfeb=myRecord[0].max,maxmar=myRecord[0].max,maxapr=myRecord[0].max,maxmay=myRecord[0].max,maxjune=myRecord[0].max; float minjan=myRecord[1].min,minfeb=myRecord[1].min,minmar=myRecord[1].min,minapr=myRecord[1].min,minmay=myRecord[1].min,minjune=myRecord[1].min; float maxjan=0,maxfeb=0,maxmar=0,maxapr=0,maxmay=0,maxjune=0; //float minjan=0,minfeb=0,minmar=0,minapr=0,minmay=0,minjune=0; string maxjanDep,maxfebDep,maxmarDep,maxaprDep,maxmayDep,maxjunDep; string minjanDep,minfebDep,minmarDep,minaprDep,minmayDep,minjunDep; cout<<"Enter file name: "; cin>>a; ifstream myfile (a); //minmax.open ("MaxMin.txt"); if (myfile.is_open()){ while (! myfile.eof()){ myfile>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max; cout << myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; i++; } myfile.close(); } else{ cout << "Unable to open file"<<endl;} cout<<"Enter file name: "; cin>>b; ifstream myfile1 (b); minmax.open ("MaxMin.txt"); jan.open ("Jan.txt"); feb.open ("Feb.txt"); mar.open ("March.txt"); apr.open ("April.txt"); may.open ("May.txt"); jun.open ("Jun.txt"); if (myfile1.is_open()){ while (! myfile1.eof()){ myfile1>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max; if (myRecord[i].month == "Jan"){ jan<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxjan< myRecord[i].max){ maxjan=myRecord[i].max; maxjanDep=myRecord[i].depName;} for (k=1;k<=3;k++){ for (j=0;j<2;j++){ if (myRecord[j].min>myRecord[j+1].min){ temp=myRecord[j].min; myRecord[j].min=myRecord[j+1].min; myRecord[j+1].min=temp; minjanDep=myRecord[j].depName; }}} } if (myRecord[i].month == "Feb"){ feb<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxfeb< myRecord[i].max){ maxfeb=myRecord[i].max; maxfebDep=myRecord[i].depName;} for (k=1;k<=3;k++){ for (j=0;j<2;j++){ if (myRecord[j].min>myRecord[j+1].min){ temp=myRecord[j].min; myRecord[j].min=myRecord[j+1].min; myRecord[j+1].min=temp; minfebDep=myRecord[j+1].depName; }}} } if (myRecord[i].month == "Mar"){ mar<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxmar< myRecord[i].max){ maxmar=myRecord[i].max; maxmarDep=myRecord[i].depName;} if (myRecord[i].min<minmar){ minmar=myRecord[i].min; minmarDep=myRecord[i].depName; }} if (myRecord[i].month == "Apr"){ apr<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxapr< myRecord[i].max){ maxapr=myRecord[i].max; maxaprDep=myRecord[i].depName;} if (minapr>myRecord[i].min){ minapr=myRecord[i].min; minaprDep=myRecord[i].min;} } if (myRecord[i].month == "May"){ may<<myRecord[i].depName<<myRecord[i].head<<myRecord[i].month<<myRecord[i].min<<myRecord[i].max<<endl; if (maxmay< myRecord[i].max){ maxmay=myRecord[i].max; maxmayDep=myRecord[i].depName;} if (minmay>myRecord[i].min){ minmay=myRecord[i].min; minmayDep=myRecord[i].depName;} } if (myRecord[i].month == "Jun"){ jun<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxjune< myRecord[i].max){ maxjune=myRecord[i].max; maxjunDep=myRecord[i].depName;} if (minjune>myRecord[i].min){ minjune=myRecord[i].min; minjunDep=myRecord[i].depName;} } i++; myfile.close(); } minmax<<"department that has maximum spending at jan "<<maxjanDep<<endl; minmax<<"department that has minimum spending at jan "<<minjanDep<<endl; minmax<<"department that has maximum spending at Feb "<<maxfebDep<<endl; minmax<<"department that has minimum spending at Feb "<<minfebDep<<endl; minmax<<"department that has maximum spending at March "<<maxmarDep<<endl; minmax<<"department that has minimum spending at March "<<minmarDep<<endl; minmax<<"department that has maximum spending at April "<<maxaprDep<<endl; minmax<<"department that has minimum spending at April "<<minaprDep<<endl; minmax<<"department that has maximum spending at May "<<maxmayDep<<endl; minmax<<"department that has minimum spending at May "<<minmayDep<<endl; minmax<<"department that has maximum spending at June "<<maxjunDep<<endl; minmax<<"department that has minimum spending at June "<<minjunDep<<endl; } else{ cout << "Unable to open file"<<endl;} }I'm almost finished with my program, but I have one problem that I can't seem to solve. How do I get the minimum spending?
My lecturer gave me the task:
The company you work at receives a monthly report in a text format. The report contains the following information.
Department Name Head of Department Name Month Minimum spending of the month Maximum spending of the monthYour program is to obtain the name of the input file from the user. Implement a structure to represent the data:
Once the file has been read into your program, print out the following statistics for the user:
List which department has the minimum spending per month by month List which department has the minimum spending by month by monthWrite the information into a file called “MaxMin.txt”
Then do a processing so that the Department Name, Head of Department Name, Minimum spending and Maximum spending are written to separate files based on the month, eg Jan, Feb, March and so on.
Here is the text file I was given to work with:
Engineering Bill Jan 2000 15000 IT Jack Jan 300 20000 HR Jill Jan 1500 10000 Engineering Bill Feb 5000 45000 IT Jack Feb 4500 7000 HR Jill Feb 5600 60000 Engineering Bill Mar 5000 45000 IT Jack Mar 4500 7000 HR Jill Mar 5600 60000 Engineering Bill Apr 5000 45000 IT Jack Apr 4500 7000 HR Jill Apr 5600 60000 Engineering Bill May 2000 15000 IT Jack May 300 20000 HR Jill May 1500 10000 Engineering Bill Jun 2000 15000 IT Jack Jun 300 20000 HR Jill Jun 1500 10000and here's the c++ code I've written
ue#include <iostream> #include <fstring> #include <string> using namespace std; struct Record { string depName; string head; string month; float max; float min; string name; } myRecord[19]; int main () { string line; ofstream minmax,jan,feb,mar,apr,may,jun; char a[50]; char b[50]; int i = 0,j,k; float temp; //float maxjan=myRecord[0].max,maxfeb=myRecord[0].max,maxmar=myRecord[0].max,maxapr=myRecord[0].max,maxmay=myRecord[0].max,maxjune=myRecord[0].max; float minjan=myRecord[1].min,minfeb=myRecord[1].min,minmar=myRecord[1].min,minapr=myRecord[1].min,minmay=myRecord[1].min,minjune=myRecord[1].min; float maxjan=0,maxfeb=0,maxmar=0,maxapr=0,maxmay=0,maxjune=0; //float minjan=0,minfeb=0,minmar=0,minapr=0,minmay=0,minjune=0; string maxjanDep,maxfebDep,maxmarDep,maxaprDep,maxmayDep,maxjunDep; string minjanDep,minfebDep,minmarDep,minaprDep,minmayDep,minjunDep; cout<<"Enter file name: "; cin>>a; ifstream myfile (a); //minmax.open ("MaxMin.txt"); if (myfile.is_open()){ while (! myfile.eof()){ myfile>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max; cout << myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; i++; } myfile.close(); } else{ cout << "Unable to open file"<<endl;} cout<<"Enter file name: "; cin>>b; ifstream myfile1 (b); minmax.open ("MaxMin.txt"); jan.open ("Jan.txt"); feb.open ("Feb.txt"); mar.open ("March.txt"); apr.open ("April.txt"); may.open ("May.txt"); jun.open ("Jun.txt"); if (myfile1.is_open()){ while (! myfile1.eof()){ myfile1>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max; if (myRecord[i].month == "Jan"){ jan<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxjan< myRecord[i].max){ maxjan=myRecord[i].max; maxjanDep=myRecord[i].depName;} for (k=1;k<=3;k++){ for (j=0;j<2;j++){ if (myRecord[j].min>myRecord[j+1].min){ temp=myRecord[j].min; myRecord[j].min=myRecord[j+1].min; myRecord[j+1].min=temp; minjanDep=myRecord[j].depName; }}} } if (myRecord[i].month == "Feb"){ feb<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxfeb< myRecord[i].max){ maxfeb=myRecord[i].max; maxfebDep=myRecord[i].depName;} for (k=1;k<=3;k++){ for (j=0;j<2;j++){ if (myRecord[j].min>myRecord[j+1].min){ temp=myRecord[j].min; myRecord[j].min=myRecord[j+1].min; myRecord[j+1].min=temp; minfebDep=myRecord[j+1].depName; }}} } if (myRecord[i].month == "Mar"){ mar<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxmar< myRecord[i].max){ maxmar=myRecord[i].max; maxmarDep=myRecord[i].depName;} if (myRecord[i].min<minmar){ minmar=myRecord[i].min; minmarDep=myRecord[i].depName; }} if (myRecord[i].month == "Apr"){ apr<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxapr< myRecord[i].max){ maxapr=myRecord[i].max; maxaprDep=myRecord[i].depName;} if (minapr>myRecord[i].min){ minapr=myRecord[i].min; minaprDep=myRecord[i].min;} } if (myRecord[i].month == "May"){ may<<myRecord[i].depName<<myRecord[i].head<<myRecord[i].month<<myRecord[i].min<<myRecord[i].max<<endl; if (maxmay< myRecord[i].max){ maxmay=myRecord[i].max; maxmayDep=myRecord[i].depName;} if (minmay>myRecord[i].min){ minmay=myRecord[i].min; minmayDep=myRecord[i].depName;} } if (myRecord[i].month == "Jun"){ jun<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl; if (maxjune< myRecord[i].max){ maxjune=myRecord[i].max; maxjunDep=myRecord[i].depName;} if (minjune>myRecord[i].min){ minjune=myRecord[i].min; minjunDep=myRecord[i].depName;} } i++; myfile.close(); } minmax<<"department that has maximum spending at jan "<<maxjanDep<<endl; minmax<<"department that has minimum spending at jan "<<minjanDep<<endl; minmax<<"department that has maximum spending at Feb "<<maxfebDep<<endl; minmax<<"department that has minimum spending at Feb "<<minfebDep<<endl; minmax<<"department that has maximum spending at March "<<maxmarDep<<endl; minmax<<"department that has minimum spending at March "<<minmarDep<<endl; minmax<<"department that has maximum spending at April "<<maxaprDep<<endl; minmax<<"department that has minimum spending at April "<<minaprDep<<endl; minmax<<"department that has maximum spending at May "<<maxmayDep<<endl; minmax<<"department that has minimum spending at May "<<minmayDep<<endl; minmax<<"department that has maximum spending at June "<<maxjunDep<<endl; minmax<<"department that has minimum spending at June "<<minjunDep<<endl; } else{ cout << "Unable to open file"<<endl;} }最满意答案
我的第一个建议是,教授想要一份记录列表 - 而不是19硬编码,使用一个矢量并在你去时填充它。
vector<Record> records;我的第二个建议是使用循环来处理每个月。
vector<string> months; months.push_back("Jan"); months.push_back("Feb"); for (int i = 0; i < months.size(); ++i) { string month = months[i]; ReadInMonthBudget(&records, month); // Then use a function to read in each month file. }然后你可以开始收集你的数据:
for (int i = 0; i < months.size(); ++i) { string month = months[i]; cout << "Min spender in month " << month << ": " << FindMinSpenderInMonth(records, month); }使用函数会使你的代码更容易理解(和写),因为它将问题分解成更小的块,这些块更容易解决。
首先让它工作,然后改进它。 如果这是一个大学水平的课程,你的教授可能会希望你提出一个比O(n ^ 2)更好的解决方案。
My first suggestion is that the prof wants a list of Record - rather than hardcoding in 19, use a vector and fill it as you go.
vector<Record> records;My second suggestion is to use a loop to process each month.
vector<string> months; months.push_back("Jan"); months.push_back("Feb"); for (int i = 0; i < months.size(); ++i) { string month = months[i]; ReadInMonthBudget(&records, month); // Then use a function to read in each month file. }Then you can work on collecting your stats:
for (int i = 0; i < months.size(); ++i) { string month = months[i]; cout << "Min spender in month " << month << ": " << FindMinSpenderInMonth(records, month); }Using functions will make your code much easier to understand (and write), because it breaks the problem down into much smaller chunks that are easier to solve.
Start by getting it working, then improve on it. If this is a college level class, your professor may want you to come up with a better than O(n^2) solution.
更多推荐
发布评论