Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Terina Burr
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <cstring>
- #include <sstream>
- #include <vector>
- #include <utility>
- #include <iterator>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <cstdlib>
- #include <cstdio>
- #include <fstream>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- class Flight
- {
- public:
- Flight(string date,string flightNum,string cityPair,string flightLeg,string lastName, string firstName)
- {
- this->date = date;
- this->flightNum = flightNum;
- this->cityPair = cityPair;
- this->flightLeg = flightLeg;
- this->lastName = lastName;
- this->firstName = firstName;
- };
- string getDate() {return this->date;};
- string getFlightNum() {return this->flightNum;};
- string getCityPair() {return this->cityPair;};
- string getFlightLeg() {return this->flightLeg;};
- string getLastName() {return this->lastName;};
- string getFirstName() {return this->firstName;};
- string getMonthDay()
- {
- if(this->getMonth() == 1)
- return "January";
- if(this->getMonth() == 2)
- return "Feburary";
- if(this->getMonth() == 3)
- return "March";
- if(this->getMonth() == 4)
- return "April";
- if(this->getMonth() == 5)
- return "May";
- if(this->getMonth() == 6)
- return "June";
- if(this->getMonth() == 7)
- return "July";
- if(this->getMonth() == 8)
- return "August";
- if(this->getMonth() == 9)
- return "September";
- if(this->getMonth() == 10)
- return "October";
- if(this->getMonth() == 11)
- return "November";
- if(this->getMonth() == 1)
- return "December";
- };
- //returns the integer for the month
- int getMonth()
- {
- string s = this ->date;
- string month = s.substr(2,3);
- if(month == "JAN")
- month = "1";
- if(month == "FEB")
- month = "2";
- if(month == "MAR")
- month = "3";
- if(month == "APR")
- month = "4";
- if(month == "MAY")
- month = "5";
- if(month == "JUN")
- month = "6";
- if(month == "JUL")
- month = "7";
- if(month == "AUG")
- month = "8";
- if(month == "SEP")
- month = "9";
- if(month == "OCT")
- month = "10";
- if(month == "NOV")
- month == "11";
- if(month == "DEC")
- month == "12";
- int m = atoi(month.c_str());
- return m;
- };
- int getDay()
- {
- string s = this->date;
- string day = s.substr(0,2);
- if(day == "01")
- day= "1";
- int d = atoi(day.c_str());
- return d;
- };
- int getYear()
- {
- string s = this ->date;
- string year = s.substr(5,2);
- int r = 1;
- while(!atoi(year.c_str()))
- {
- year = s.substr(5+r);
- }
- int y = atoi( year.c_str());
- y+=2000;
- return y;
- };
- private:
- string date,flightNum,cityPair,flightLeg,lastName,firstName;
- };
- class ComparatorByFlightNum{
- public:
- bool operator()(Flight *a, Flight *b){
- return a->getFlightNum() < b->getFlightNum();
- };
- };
- class ComparatorByCityPair{
- public:
- bool operator()(Flight *a, Flight *b){
- return a->getCityPair() < b->getCityPair();
- };
- };
- class ComparatorByLastName{
- public:
- bool operator()(Flight *a, Flight *b){
- return a->getLastName() < b->getLastName();
- };
- };
- class ComparatorByFirstName{
- public:
- bool operator()(Flight *a, Flight *b){
- return a->getFirstName() < b->getFirstName();
- };
- };
- class ComparatorByDate{
- public:
- bool operator()(Flight *a, Flight *b)
- {
- if(a->getYear() < b->getYear())
- return true;
- if((a->getYear() == b->getYear()) &&( a->getMonth() < b->getMonth()))
- return true;
- if((a->getYear() == b->getYear()) && (a->getMonth() == b->getMonth())&&( a->getDay() < b->getDay()))
- return true;
- return false;
- };
- };
- void showFlights(vector<Flight*> &flights);
- int passengerCount(vector<Flight*> &flights);
- /********************************************************************************
- FUNCTION: IntToString
- ARGUMENTS: const int a - integer t convert
- RETURNS: string - string that was an int
- NOTES: converts integer to string
- ********************************************************************************/
- string IntToString (int a)
- {
- ostringstream temp;
- temp<<a;
- return temp.str();
- }
- /********************************************************************************
- FUNCTION: split
- ARGUMENTS: const string str - string to split
- char deliminter - delimiter
- RETURNS: vector<char*> - container of the new strings
- NOTES: parses string of commands, spliting string by delimiter
- ********************************************************************************/
- vector<string> split(string str,char delimiter)
- {
- vector<string> internal;
- stringstream ss(str);
- string tok;
- while(getline(ss,tok,delimiter)){
- internal.push_back(tok);
- }
- return internal;
- }
- /********************************************************************************
- FUNCTION: compareFlightDate
- ARGUMENTS: Flight *a - flight to compare
- Flight *b - flight compared to
- RETURNS: bool - whether or not the condition was true
- NOTES: sees whether or not the flight dates are equal
- ********************************************************************************/
- bool compareFlightDate(Flight *a,Flight *b)
- {
- if(a->getDate() == b->getDate())
- return true;
- return false;
- }
- /********************************************************************************
- FUNCTION: compareFlightNum
- ARGUMENTS: Flight *a - flight to compare
- Flight *b - flight compared to
- RETURNS: bool - whether or not the condition was true
- NOTES: sees whether or not the flight numbers are equal
- ********************************************************************************/
- bool compareFlightNum(Flight *a, Flight *b)
- {
- if(a->getFlightNum() == b->getFlightNum())
- return true;
- return false;
- }
- /********************************************************************************
- FUNCTION: compareFlightCity
- ARGUMENTS: Flight *a - flight to compare
- Flight *b - flight compared to
- RETURNS: bool - whether or not the condition was true
- NOTES: sees whether or not the flight City Pairs are equal
- ********************************************************************************/
- bool compareFlightCity(Flight *a, Flight *b)
- {
- if(a->getCityPair() == b->getCityPair())
- return true;
- return false;
- }
- /********************************************************************************
- FUNCTION: compareFlightLeg
- ARGUMENTS: Flight *a - flight to compare
- Flight *b - flight compared to
- RETURNS: bool - whether or not the condition was true
- NOTES: sees whether or not the flight legs are equal
- ********************************************************************************/
- bool compareFlightLeg(Flight *a, Flight *b)
- {
- if(a->getFlightLeg() == b->getFlightLeg())
- return true;
- return false;
- }
- int main(int argc, char* argv[])
- {
- ifstream the_file; //the file to read from
- char read_msg[100];
- char num_bytes[10];
- int fds[2],pid,ret;
- ret = pipe(fds);
- //open a pipe
- pid = fork();
- //fork into two proceses
- if (pid < 0) // error occurred
- {
- cout << "Fork failed " << ret << endl;
- exit(-1);
- }
- else if (pid ==0){
- //child process = consumer
- close(fds[1]);
- int loopx =5;
- vector<Flight*> flights;
- string date,flightNum,cityPair,flightLeg,lastName,firstName;
- while(loopx>0){
- read(fds[0],&num_bytes,3);//sizeof(num_bytes));
- int num = atoi(num_bytes);
- read(fds[0],read_msg,num+1);
- vector<string> path(split(read_msg, '/'));
- date = path[0];
- flightNum = path[1];
- cityPair = path[2];
- flightLeg = path[3];
- lastName = path[4];
- firstName = path[5];
- flights.push_back(new Flight(date,flightNum,cityPair,flightLeg,lastName,firstName));
- loopx--;
- }
- stable_sort(flights.begin(),flights.end(),ComparatorByDate());
- stable_sort(flights.begin(),flights.end(),ComparatorByFlightNum());
- stable_sort(flights.begin(),flights.end(),ComparatorByCityPair());
- stable_sort(flights.begin(),flights.end(),ComparatorByLastName());
- stable_sort(flights.begin(),flights.end(),ComparatorByFirstName());
- showFlights(flights);
- close(fds[0]);
- //close read end
- }
- else{
- //parent proces
- close(fds[0]);
- ifstream the_file; //the file to read from
- if ( argc != 2 )// argc should be 2 for correct execution
- {
- // We print argv[0] assuming it is the program name
- cout<<"usage: "<< argv[0] <<" <filename>\n";
- the_file.open("/home/turing/t90rkf1/d480/dhw/hw4-air/resv.txt");
- }
- else {
- // We assume argv[1] is a filename to open
- the_file.open ( argv[1] );
- // Always check to see if file opening succeeded
- }
- if ( the_file.fail())
- {
- cout<<"Could not open file\n";
- }
- string line;
- int i =0;
- string length;
- int size = 0;
- stringstream ss;
- stringstream issa;
- string reading;
- int writing;
- string read;
- cout << "Parent: ";
- while(!the_file.eof())
- {
- getline(the_file,line);
- size = line.length();
- length = IntToString(size);
- const char * const cstr = line.c_str();
- write(fds[1],length.c_str(),3);
- write(fds[1],cstr,line.length()+1);
- i++;
- ss << line << ' ' << size;
- ss >> reading >> writing;
- cout << " Reading: "<< cstr <<'\n';
- cout << " Writing: "<<size<<" bytes: "<<line<<'\n'<<'\n';
- }
- string num_rec;
- issa << i;
- issa >> num_rec;
- cout<<"Number of input records read: "<<num_rec<<'\n';
- the_file.close();
- close(fds[1]);
- }
- return 0;
- }
- /********************************************************************************
- FUNCTION: compareFlightDate
- ARGUMENTS: vector<Flight*> &flights - vector of flight objects
- RETURNS: NONE
- NOTES: Displays the contents of the vector
- ********************************************************************************/
- void showFlights(vector<Flight*> &flights)
- {
- stringstream ss;
- string s,day;
- int passAllTot=1;
- int dateTotal = 1;
- int monthTot = 1;
- int grandTotal = 1;
- string num,city,first,last,leg,mo,mon;
- //for first element so has something to compare to
- cout << flights[0]->getYear()<<" "
- << flights[0]->getMonthDay()<<" "
- << flights[0] ->getDay()<<" "
- << flights[0]->getFlightNum()<<" "
- << flights[0]->getCityPair()<<" "
- << setw(10)<< flights[0]->getFlightLeg()<<" "
- << flights[0]->getLastName()<<","
- << flights[0]->getFirstName()<<endl;
- vector<int>passLegTotal(flights.size()); //made vector for seperate legs
- fill(passLegTotal.begin(),passLegTotal.end(),1);//fill vectors with all ones
- for(unsigned int i = 1; i<flights.size(); ++i)
- {
- if(compareFlightDate(flights[i-1],flights[i])==true &&
- compareFlightNum(flights[i-1],flights[i])==true &&
- compareFlightCity(flights[i-1],flights[i])==true)
- passLegTotal[i]++;
- if(compareFlightDate(flights[i-1],flights[i])==true &&
- compareFlightNum(flights[i-1],flights[i])==true)
- passAllTot++;
- if(compareFlightDate(flights[i-1],flights[i]))
- dateTotal++;
- if(flights[i-1]->getMonth() == flights[i]->getMonth())
- monthTot++;
- if(((flights[i-1]->getDate()==flights[i]->getDate())&&
- (flights[i-1]->getFlightNum()==flights[i]->getFlightNum()) &&
- (flights[i-1]->getCityPair()== flights[i]->getCityPair()))){
- cout<<setw(23)<< flights[i]->getFlightNum()<<" "
- << flights[i]->getCityPair()<<" "
- <<setw(10)<< flights[i]->getFlightLeg()<<" "
- << flights[i]->getLastName()<<","
- << flights[i]->getFirstName()<<endl;
- }
- else
- {
- cout << flights[i]->getYear()<<" "
- << flights[i]->getMonthDay()<<" "
- << flights[i] ->getDay()<<" "
- << flights[i]->getFlightNum()<<" "
- << flights[i]->getCityPair()<<" "
- << setw(10)<< flights[i]->getFlightLeg()<<" "
- << flights[i]->getLastName()<<","
- << flights[i]->getFirstName()<<endl;
- }
- cout << flights[i]->getYear()<<" "
- << flights[i]->getMonthDay()<<" "
- << flights[i] ->getDay()<<" "
- << flights[i]->getFlightNum()<<" "
- << flights[i]->getCityPair()<<" "
- << setw(3) << passLegTotal[i]<<" " <<"*"<<setw(26)<<"Passengers this leg"
- <<endl;
- //this checks to see if its the last value
- if(flights[i] == flights.back() || !(compareFlightDate(flights[i+1],flights[i])) )
- {
- cout << flights[i]->getYear()<<" "
- << flights[i]->getMonthDay()<<" "
- << flights[i] ->getDay()<<" "
- << flights[i]->getFlightNum()<<" "
- << setw(11) << passAllTot << " "<<"**"<<setw(25) << "Passengers all Legs"<<endl;
- passAllTot = 1;
- }
- if(flights[i] == flights.back() || !(compareFlightDate(flights[i+1],flights[i])) )
- {
- cout << flights[i]->getYear()<<" "
- << flights[i]->getMonthDay()<<" "
- << flights[i] ->getDay()<<" "
- << setw(17) << dateTotal << " "<<"***"<< setw(15)<<"Date Total"<<endl;
- dateTotal = 1;
- }
- if(flights[i] == flights.back() ||!(flights[i+1]->getMonth() == flights[i]->getMonth()))
- {
- cout << flights[i]->getYear()<<" "
- << flights[i]->getMonthDay()<<" "
- <<setw(19) << monthTot << " "<<"****"<<" "<< setw(14)<<"Month Total"<<endl;
- monthTot = 1;
- }
- grandTotal++;
- if(flights[i] == flights.back())
- {
- cout << setw(32)<< grandTotal << " "<<"*****"<<" "<<setw(13) <<"Grand Total"<<endl;
- }
- cout<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement