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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
//********Structures********
struct MachineRecord
{
int IDNumber;
string Description1;
string Description2;
string DescriptionComplete;
int InstallMonth;
int InstallDay;
int InstallYear;
int PrevMain;
int ServiceDuration;
int ServiceMonth;
int ServiceDay;
int ServiceYear;
int JulianDate;
int ServiceDate;
};
MachineRecord Machine;
//******End Structures******
//**********Function Prototypes**********
int JulianDateConversion(int, int, int);
void GregorianDateConversion(int, int &, int &, int &);
void SetDateDescription();
void DisplayMachineStats(int, string);
void CalcAndDisplayServiceDates();
//********End Function Prototypes********
//Declare file streams
ifstream inFile;
ofstream outFile;
int main()
{
//Declare variables
string FileLine;
//Open inFile
inFile.open("machines.dat");
while(!inFile)
{
cout << "Error opening the inFile." << endl;
return 1;
}
//Open outFile
outFile.open("Schedule.txt");
while(!outFile)
{
cout << "Error opening the outFile." << endl;
return 1;
}
//Prime the read
inFile >> Machine.IDNumber >> Machine.Description1 >> Machine.Description2 >> Machine.InstallMonth >> Machine.InstallDay >> Machine.InstallYear >> Machine.PrevMain >> Machine.ServiceDuration;
//Read
while(inFile)
{
//Convert Install Date to Julian Date
Machine.JulianDate = JulianDateConversion(Machine.InstallMonth, Machine.InstallDay, Machine.InstallYear);
//Set Current Machine Date and Description
SetDateDescription();
//Display Current Machine stats
DisplayMachineStats(Machine.IDNumber, Machine.DescriptionComplete);
//Calculate and Display Service Dates
CalcAndDisplayServiceDates();
//Get the next line from file
inFile >> Machine.IDNumber >> Machine.Description1 >> Machine.Description2 >> Machine.InstallMonth >> Machine.InstallDay >> Machine.InstallYear >> Machine.PrevMain >> Machine.ServiceDuration;
}//EndWhile
//Close the files
inFile.close();
outFile.close();
return 0;
}
//**********Function Definitions**********
int JulianDateConversion(int Month, int Day, int Year)
{
//Declare variables
int a, m, y, date;
//Formula to convert Date to Julian
a = (14 - Month) / 12;
m = Month + (12 * a) -3;
y = Year + 4800 - a;
date = Day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
return date;
}
void GregorianDateConversion(int nDate, int &Month, int &Day, int &Year)
{
//Declare Variables
int a, b, c, d, e, m;
//Formula to convert Julian to Gregorian
a = nDate + 32044;
b = ((4 * a) + 3) / 146097;
c = a - (146097 * b) / 4;
d = ((4 * c)+ 3) / 1461;
e = c - ((1461 * d) / 4);
m = ((5 * e) + 2) / 153;
//Assign Date to variable by ref
Month = m + 3 - 12 * (m / 10);
Day = e - (((153 * m) + 2) / 5) + 1;
Year = (100 * b) + d - 4800 + (m / 10);
}
void SetDateDescription()
{
Machine.ServiceDate = Machine.JulianDate;
Machine.DescriptionComplete = Machine.Description1 + " " + Machine.Description2;
}
void DisplayMachineStats(int, string)
{
//Display Current Machine stats
cout << Machine.IDNumber << " - " << Machine.DescriptionComplete << endl;
outFile << Machine.IDNumber << " - " << Machine.DescriptionComplete << endl;
}
void CalcAndDisplayServiceDates()
{
for(int i = 0; i < 20; i++)
{
//Add Preventive Maintenance to Install Julian Date
Machine.ServiceDate = Machine.ServiceDate + Machine.PrevMain;
//Convert Julian to Gregorian Date
GregorianDateConversion(Machine.ServiceDate, Machine.ServiceMonth, Machine.ServiceDay, Machine.ServiceYear);
//Display Service Date
if(Machine.ServiceMonth > 2 && Machine.ServiceMonth < 8 && Machine.ServiceYear == 2012)
{
cout << "Scheduled Service Date: " << Machine.ServiceMonth << "/" << Machine.ServiceDay << "/" << Machine.ServiceYear << endl;
outFile << "Scheduled Service Date: " << Machine.ServiceMonth << "/" << Machine.ServiceDay << "/" << Machine.ServiceYear << endl;
}//EndIf
//Add Service Duration to Date
if(Machine.ServiceDuration == 1)
{
//Only Takes 1 Day to Service
Machine.ServiceDate = Machine.ServiceDate + 0;
}
if(Machine.ServiceDuration == 2)
{
//Add 1 additional day to service
Machine.ServiceDate = Machine.ServiceDate + 1;
}
if(Machine.ServiceDuration == 3)
{
//Add 2 additional days to service
Machine.ServiceDate = Machine.ServiceDate + 2;
}
}
cout << endl;
outFile << endl;
}
//********End Function Definitions********
|