1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include <iostream>
#include <fstream> //Input/output files
#include <string> //For strings
#include <sstream> //For stringstream()
#include <iomanip> //For setw()
using namespace std;
//File descriptors
ifstream infile;
ofstream outfile;
//Declare constants
const int MAX_RECORDS = 6;
//This is a hierarchical struct since Date is a struct type within member list of MachineRecord
struct Date
{
int month;
int day;
int year;
};
struct MachineRecord
{
string machineNbr;
string name;
Date InstallationDate;
int preventMaint;
int serviceTimeInDays;
};
//Create an array of machine records
MachineRecord pMSchedule[MAX_RECORDS];
//Prototypes
void DisplayHeader();
int OpenInputOutput();
void ReadInData(string inputString, MachineRecord record[MAX_RECORDS]);
void FillStruct(string inputString, int count, MachineRecord record[MAX_RECORDS]);
void ConvertDateToJulian(int year, int month, int day, int pmDays, int serviceTimeInt );
void ConvertDateToGreg(int julianDateToConvert);
void CreateSchedule(int installDateJul, int pMCycle, int month, int day, int year, int serviceTime);
//Main program
int main()
{
//Variable declarations
string inputString;
//Call functions
DisplayHeader();
OpenInputOutput();
ReadInData(inputString, pMSchedule);
//Close input/output files
infile.close();
outfile.close();
return 0;//Quit
}//End of main program
//******************************************************************************
//Function reads from input file
void ReadInData(string inputString, MachineRecord record[MAX_RECORDS])
{//Passes in each line read in, and array of records
//While in input file, read in all lines of data
while (!infile.eof())
{ //For all records
for(int count = 0; count < MAX_RECORDS; count++)
{
getline(infile, inputString);//Get first line of input file
FillStruct(inputString, count, pMSchedule);//Calls function to plug records into array
}//Falls out of inner loop once all records have been read in
}//Falls out of loop once at end of file
}//End of function
//******************************************************************************
//Function takes data and puts it into struct
void FillStruct(string currentString, int count, MachineRecord record[MAX_RECORDS])
{//Passes in each line read in, the counter, and array of records
//Variables to hold string parts after each line is read-in
string numbers;
int monthInt;
int dayInt;
int yearInt;
int pmInt;
int serviceTimeInt;
//Gets string value from positions 0-4
record[count].machineNbr = currentString.substr(0, 5);//Assigns to record
//Gets string value from positions 5-19
record[count].name = currentString.substr(5, 15);//Assigns to record
//Gets rest of string from position 20 on
numbers = currentString.substr(20, 20);//Gets all numbers after machine name
//Converts string to numerical ints that can be used in struct
stringstream(numbers) >> monthInt >> dayInt >> yearInt >> pmInt >> serviceTimeInt;
record[count].InstallationDate.month = monthInt;//Assigns to record
record[count].InstallationDate.day = dayInt;//Assigns to record
record[count].InstallationDate.year = yearInt;//Assigns to record
record[count].preventMaint = pmInt;//Assigns to record
record[count].serviceTimeInDays = serviceTimeInt;//Assigns to record
DisplayTitle();//Calls function
//Displays name and number and pm stats
cout << "________________________________________" << endl;
cout << "Machine :" << record[count].name << endl;
cout << "Machine Number is :" << record[count].machineNbr << endl;
cout << "Needs maint. every :" << record[count].preventMaint << " days" << endl;
cout << "Service Time (days) :" << record[count].serviceTimeInDays << endl << endl;
outfile << "________________________________________" << endl;
outfile << "Machine :" << record[count].name << endl;
outfile << "Machine Number is :" << record[count].machineNbr << endl;
outfile << "Needs maint. every :" << record[count].preventMaint << " days" << endl;
outfile << "Service Time (days) :" << record[count].serviceTimeInDays << endl << endl;
DisplayLabel();//Calls function
ConvertDateToJulian(yearInt, monthInt, dayInt, pmInt, serviceTimeInt);//Calls function
}//End of function
//******************************************************************************
//Function to take date of installation and convert it to a julian date in order to calculate
//service schedule.
void ConvertDateToJulian(int year, int month, int day, int pmDays, int serviceTime)
{//Passes in Installation date
//Declare variables to be used in converting date
int julianDate;
int I;
int J;
int K;
I = year;
J = month;
K = day;
//Formula to convert gregorian date to julian date
julianDate = K - 32075 + 1461 *(I + 4800 +(J - 14)/ 12)/ 4 + 367 *
(J - 2 -(J - 14)/ 12 *12)/ 12 - 3 * ((I + 4900 +(J-14)/12)/100)/4;
ConvertDateToGreg(julianDate);//Calls function
CreateSchedule(julianDate, pmDays, J, K, I, serviceTime);//Calls function
}//End of function
//******************************************************************************
//Function converts Julian date back to Gregorian date
void ConvertDateToGreg(int julianDateToConvert)
{//Passes in julian date
int I;
int J;
int K;
int L;
int N;
int YEAR;
int MONTH;
int DAY;
//Formula to convert julian date to regular date (gregorian)
L = julianDateToConvert + 68569;
N = 4 * L/ 146097;
L = L - (146097 * N + 3)/ 4;
I = 4000 *(L + 1)/ 1461001;
L = L - 1461 * I/ 4 + 31;
J = 80 * L/ 2447;
K = L - 2447 * J/ 80;
L = J/ 11;
J = J + 2 - 12 * L;
I = 100 *(N - 49) + I + L;
YEAR = I;
MONTH = J;
DAY = K;
//Displays installation date and all pm days
cout << setw(2) << MONTH << "-" << setw(2) << DAY << "-" << YEAR << endl;
outfile << setw(2) << MONTH << "-" << setw(2) << DAY << "-" << YEAR << endl;
}//End of function
//******************************************************************************
//Function determines dates to schedule required preventive maintenance
void CreateSchedule(int installDateJul, int pMCycle, int month, int day, int year, int serviceTime)
{//Passes in julian install date, how often machine needs pm, and the date of last scheduled pm
int scheduleDay; //Variable to hold each maintenance day
int fiveMonthsOut;//Variable to hold julian date for end of five months
int today; //Variable to hold julian date for March 1, 2012
fiveMonthsOut = 2456141;//Julian date for Aug 1, 2012
today = 2455988; //Julian date for March 1.2012
scheduleDay = installDateJul + pMCycle;//First pm date is set amount of days after install
while (scheduleDay <= fiveMonthsOut)//While we are within 5 month period
{
while (scheduleDay < today)//While pm day is before today
{
scheduleDay = scheduleDay + pMCycle + serviceTime;//Get next pm day
}
ConvertDateToGreg(scheduleDay);//Call function when pm dayis at least March 1
//Calculate next pm date by adding how many days between service
//AND the # of days service takes
scheduleDay = scheduleDay + pMCycle + serviceTime;
}//Fall out of loop once scheduled date is after 5 month mark
}//End of function
|