开发者

C++ file handling and calculations

开发者 https://www.devze.com 2022-12-27 00:40 出处:网络
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?

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 month

Your 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 ha开发者_如何学JAVAs the minimum spending per month by month
  • List which department has the minimum spending by month by month

Write 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    10000

and 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;}
}


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.


The first thing I recommend you is improving the legibility of your program. Try adding functions that will perform the simple operations for you, for example, reading a single line of input into the struct (BTW, there is one more field in the struct that in the input).

Declare your variables close to where you need them, there is no point in declaring upfront everything you will need, you can even forget what it was by the time you actually use it. Use data structures that simplify the code (instead of N min_month, max_month variables just use a vector of minimum / maximum values indexed by month; use vectors instead of arrays; if you find that you need lookup tables, consider using maps...)

But first of all, implement your algorithm with pen and paper. I had a professor that used to say that until you know how to implement a process with pen and paper there was no point in even considering implementing it in a computer as it would just fail in a lot faster and be harder to diagnose.

0

精彩评论

暂无评论...
验证码 换一张
取 消