Advertisement
rinab333

CSCI 480 assignment 4

Jul 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.30 KB | None | 0 0
  1.  
  2.  
  3. //Terina Burr
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string>
  7. #include <cstring>
  8. #include <sstream>
  9. #include <vector>
  10. #include <utility>
  11. #include <iterator>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <cstdlib>
  16. #include <cstdio>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. class Flight
  23. {
  24. public:
  25. Flight(string date,string flightNum,string cityPair,string flightLeg,string lastName, string firstName)
  26. {
  27. this->date = date;
  28. this->flightNum = flightNum;
  29. this->cityPair = cityPair;
  30. this->flightLeg = flightLeg;
  31. this->lastName = lastName;
  32. this->firstName = firstName;
  33. };
  34. string getDate() {return this->date;};
  35. string getFlightNum() {return this->flightNum;};
  36. string getCityPair() {return this->cityPair;};
  37. string getFlightLeg() {return this->flightLeg;};
  38. string getLastName() {return this->lastName;};
  39. string getFirstName() {return this->firstName;};
  40.  
  41. string getMonthDay()
  42. {
  43. if(this->getMonth() == 1)
  44. return "January";
  45. if(this->getMonth() == 2)
  46. return "Feburary";
  47. if(this->getMonth() == 3)
  48. return "March";
  49. if(this->getMonth() == 4)
  50. return "April";
  51. if(this->getMonth() == 5)
  52. return "May";
  53. if(this->getMonth() == 6)
  54. return "June";
  55. if(this->getMonth() == 7)
  56. return "July";
  57. if(this->getMonth() == 8)
  58. return "August";
  59. if(this->getMonth() == 9)
  60. return  "September";
  61. if(this->getMonth() == 10)
  62. return  "October";
  63. if(this->getMonth() == 11)
  64. return "November";
  65. if(this->getMonth() == 1)
  66. return "December";
  67. };
  68. //returns the integer for the month
  69. int getMonth()
  70. {
  71. string s = this ->date;
  72. string month = s.substr(2,3);
  73. if(month == "JAN")
  74. month = "1";
  75. if(month == "FEB")
  76. month = "2";
  77. if(month == "MAR")
  78. month = "3";
  79. if(month == "APR")
  80. month = "4";
  81. if(month == "MAY")
  82. month = "5";
  83. if(month == "JUN")
  84. month = "6";
  85. if(month == "JUL")
  86. month = "7";
  87. if(month == "AUG")
  88. month = "8";
  89. if(month == "SEP")
  90. month = "9";
  91. if(month == "OCT")
  92. month = "10";
  93. if(month == "NOV")
  94. month == "11";
  95. if(month == "DEC")
  96. month == "12";
  97. int m = atoi(month.c_str());
  98. return m;
  99. };
  100. int getDay()
  101. {
  102. string s = this->date;
  103. string day = s.substr(0,2);
  104. if(day == "01")
  105. day= "1";
  106. int d = atoi(day.c_str());
  107.  
  108. return d;
  109. };
  110. int getYear()
  111. {
  112. string s = this ->date;
  113. string year = s.substr(5,2);
  114. int r = 1;
  115. while(!atoi(year.c_str()))
  116. {
  117. year = s.substr(5+r);
  118. }
  119.  
  120. int y = atoi( year.c_str());
  121. y+=2000;
  122. return y;
  123.  
  124.  
  125.  
  126. };
  127. private:
  128. string date,flightNum,cityPair,flightLeg,lastName,firstName;
  129. };
  130. class ComparatorByFlightNum{
  131. public:
  132. bool operator()(Flight *a, Flight *b){
  133. return a->getFlightNum() < b->getFlightNum();
  134. };
  135. };
  136. class ComparatorByCityPair{
  137. public:
  138. bool operator()(Flight *a, Flight *b){
  139. return a->getCityPair() < b->getCityPair();
  140. };
  141. };
  142. class ComparatorByLastName{
  143. public:
  144. bool operator()(Flight *a, Flight *b){
  145. return a->getLastName() < b->getLastName();
  146. };
  147. };
  148. class ComparatorByFirstName{
  149. public:
  150. bool operator()(Flight *a, Flight *b){
  151. return a->getFirstName() < b->getFirstName();
  152. };
  153. };
  154.  
  155. class ComparatorByDate{
  156. public:
  157. bool operator()(Flight *a, Flight *b)
  158. {
  159. if(a->getYear() < b->getYear())
  160. return true;
  161. if((a->getYear() == b->getYear()) &&( a->getMonth() < b->getMonth()))
  162. return true;
  163. if((a->getYear() == b->getYear()) && (a->getMonth() == b->getMonth())&&( a->getDay() < b->getDay()))
  164. return true;
  165. return false;
  166.  
  167. };
  168. };
  169. void showFlights(vector<Flight*> &flights);
  170. int passengerCount(vector<Flight*> &flights);
  171. /********************************************************************************
  172. FUNCTION: IntToString
  173.  
  174. ARGUMENTS: const int a - integer t convert
  175.  
  176. RETURNS: string - string that was an int
  177.  
  178. NOTES: converts integer to string
  179. ********************************************************************************/
  180. string IntToString (int a)
  181. {
  182. ostringstream temp;
  183. temp<<a;
  184. return temp.str();
  185. }
  186. /********************************************************************************
  187. FUNCTION: split
  188.  
  189. ARGUMENTS: const string str - string to split
  190. char deliminter - delimiter
  191.  
  192. RETURNS: vector<char*> - container of the new strings
  193.  
  194. NOTES: parses string of commands, spliting string by delimiter
  195. ********************************************************************************/
  196. vector<string> split(string str,char delimiter)
  197. {
  198. vector<string> internal;
  199. stringstream ss(str);
  200. string tok;
  201.  
  202. while(getline(ss,tok,delimiter)){
  203. internal.push_back(tok);
  204. }
  205. return internal;
  206. }
  207. /********************************************************************************
  208. FUNCTION: compareFlightDate
  209.  
  210. ARGUMENTS: Flight *a - flight to compare
  211. Flight *b - flight compared to
  212.  
  213. RETURNS: bool - whether or not the condition was true
  214.  
  215. NOTES: sees whether or not the flight dates are equal
  216. ********************************************************************************/
  217.  
  218. bool compareFlightDate(Flight *a,Flight *b)
  219. {
  220. if(a->getDate() == b->getDate())
  221. return true;
  222. return false;
  223. }
  224. /********************************************************************************
  225. FUNCTION: compareFlightNum
  226.  
  227. ARGUMENTS: Flight *a - flight to compare
  228. Flight *b - flight compared to
  229.  
  230. RETURNS: bool - whether or not the condition was true
  231.  
  232. NOTES: sees whether or not the flight numbers are equal
  233. ********************************************************************************/
  234.  
  235. bool compareFlightNum(Flight *a, Flight *b)
  236. {
  237. if(a->getFlightNum() == b->getFlightNum())
  238. return true;
  239. return false;
  240. }
  241. /********************************************************************************
  242. FUNCTION: compareFlightCity
  243.  
  244. ARGUMENTS: Flight *a - flight to compare
  245. Flight *b - flight compared to
  246.  
  247. RETURNS: bool - whether or not the condition was true
  248.  
  249. NOTES: sees whether or not the flight City Pairs are equal
  250. ********************************************************************************/
  251.  
  252. bool compareFlightCity(Flight *a, Flight *b)
  253. {
  254. if(a->getCityPair() == b->getCityPair())
  255. return true;
  256. return false;
  257. }
  258. /********************************************************************************
  259. FUNCTION: compareFlightLeg
  260.  
  261. ARGUMENTS: Flight *a - flight to compare
  262. Flight *b - flight compared to
  263.  
  264. RETURNS: bool - whether or not the condition was true
  265.  
  266. NOTES: sees whether or not the flight legs are equal
  267. ********************************************************************************/
  268.  
  269. bool compareFlightLeg(Flight *a, Flight *b)
  270. {
  271. if(a->getFlightLeg() == b->getFlightLeg())
  272. return true;
  273. return false;
  274. }
  275.  
  276.  
  277. int main(int argc, char* argv[])
  278. {
  279. ifstream the_file; //the file to read from
  280. char read_msg[100];
  281. char num_bytes[10];
  282.  
  283. int fds[2],pid,ret;
  284. ret = pipe(fds);
  285. //open a pipe
  286. pid = fork();
  287. //fork into two proceses
  288. if (pid < 0) // error occurred
  289. {
  290. cout << "Fork failed " << ret << endl;
  291. exit(-1);
  292. }
  293.  
  294.  
  295. else if (pid ==0){
  296. //child process = consumer
  297. close(fds[1]);
  298. int loopx =5;
  299. vector<Flight*> flights;
  300. string date,flightNum,cityPair,flightLeg,lastName,firstName;
  301.  
  302. while(loopx>0){
  303. read(fds[0],&num_bytes,3);//sizeof(num_bytes));
  304. int num = atoi(num_bytes);
  305. read(fds[0],read_msg,num+1);
  306.  
  307. vector<string> path(split(read_msg, '/'));
  308. date = path[0];
  309. flightNum = path[1];
  310. cityPair = path[2];
  311. flightLeg = path[3];
  312. lastName = path[4];
  313. firstName = path[5];
  314. flights.push_back(new Flight(date,flightNum,cityPair,flightLeg,lastName,firstName));
  315.  
  316. loopx--;
  317. }
  318. stable_sort(flights.begin(),flights.end(),ComparatorByDate());
  319. stable_sort(flights.begin(),flights.end(),ComparatorByFlightNum());
  320. stable_sort(flights.begin(),flights.end(),ComparatorByCityPair());
  321. stable_sort(flights.begin(),flights.end(),ComparatorByLastName());
  322. stable_sort(flights.begin(),flights.end(),ComparatorByFirstName());
  323. showFlights(flights);
  324.  
  325. close(fds[0]);
  326. //close read end
  327. }
  328. else{
  329. //parent proces
  330. close(fds[0]);
  331. ifstream the_file; //the file to read from
  332.  
  333. if ( argc != 2 )// argc should be 2 for correct execution
  334. {
  335. // We print argv[0] assuming it is the program name
  336. cout<<"usage: "<< argv[0] <<" <filename>\n";
  337. the_file.open("/home/turing/t90rkf1/d480/dhw/hw4-air/resv.txt");
  338. }
  339. else {
  340. // We assume argv[1] is a filename to open
  341. the_file.open ( argv[1] );
  342. // Always check to see if file opening succeeded
  343. }
  344. if ( the_file.fail())
  345. {
  346. cout<<"Could not open file\n";
  347. }
  348.  
  349.  
  350. string line;
  351. int i =0;
  352. string length;
  353. int size = 0;
  354. stringstream ss;
  355. stringstream issa; 
  356. string reading;
  357. int writing;
  358. string read;
  359. cout << "Parent: ";
  360. while(!the_file.eof())
  361. {
  362. getline(the_file,line);
  363. size = line.length();
  364. length = IntToString(size);
  365. const char * const cstr = line.c_str();
  366. write(fds[1],length.c_str(),3);
  367. write(fds[1],cstr,line.length()+1);
  368. i++;
  369. ss << line << ' ' << size;
  370. ss >> reading >> writing;
  371. cout << " Reading: "<< cstr <<'\n';
  372. cout << " Writing: "<<size<<" bytes: "<<line<<'\n'<<'\n';
  373. }
  374. string num_rec;
  375. issa << i;
  376. issa >> num_rec;
  377. cout<<"Number of input records read: "<<num_rec<<'\n';
  378. the_file.close();
  379. close(fds[1]);
  380.  
  381.  
  382. }
  383.  
  384.  
  385.  
  386. return 0;
  387. }
  388. /********************************************************************************
  389. FUNCTION: compareFlightDate
  390.  
  391. ARGUMENTS: vector<Flight*> &flights - vector of flight objects
  392.  
  393. RETURNS: NONE
  394.  
  395. NOTES: Displays the contents of the vector
  396. ********************************************************************************/
  397.  
  398. void showFlights(vector<Flight*> &flights)
  399. {
  400. stringstream ss;
  401. string s,day;
  402. int passAllTot=1;
  403. int dateTotal = 1;
  404. int monthTot = 1;
  405. int grandTotal = 1;
  406. string num,city,first,last,leg,mo,mon;
  407.  
  408. //for first element so has something to compare to
  409. cout << flights[0]->getYear()<<" "
  410. << flights[0]->getMonthDay()<<" "
  411. << flights[0] ->getDay()<<" "
  412. << flights[0]->getFlightNum()<<" "
  413. << flights[0]->getCityPair()<<" "
  414. << setw(10)<< flights[0]->getFlightLeg()<<" "
  415. << flights[0]->getLastName()<<","
  416. << flights[0]->getFirstName()<<endl;
  417.  
  418.  
  419. vector<int>passLegTotal(flights.size()); //made vector for seperate legs
  420. fill(passLegTotal.begin(),passLegTotal.end(),1);//fill vectors with all ones
  421.  
  422.  
  423. for(unsigned int i = 1; i<flights.size(); ++i)
  424. {
  425.  
  426. if(compareFlightDate(flights[i-1],flights[i])==true &&
  427. compareFlightNum(flights[i-1],flights[i])==true &&
  428. compareFlightCity(flights[i-1],flights[i])==true)
  429. passLegTotal[i]++;
  430. if(compareFlightDate(flights[i-1],flights[i])==true &&
  431. compareFlightNum(flights[i-1],flights[i])==true)
  432. passAllTot++;
  433. if(compareFlightDate(flights[i-1],flights[i]))
  434. dateTotal++;
  435. if(flights[i-1]->getMonth() == flights[i]->getMonth())
  436. monthTot++;
  437.  
  438.  
  439.  
  440. if(((flights[i-1]->getDate()==flights[i]->getDate())&&
  441. (flights[i-1]->getFlightNum()==flights[i]->getFlightNum()) &&
  442. (flights[i-1]->getCityPair()== flights[i]->getCityPair()))){
  443. cout<<setw(23)<< flights[i]->getFlightNum()<<" "
  444. << flights[i]->getCityPair()<<" "
  445. <<setw(10)<< flights[i]->getFlightLeg()<<" "
  446. << flights[i]->getLastName()<<","
  447. << flights[i]->getFirstName()<<endl;
  448.  
  449. }
  450.  
  451. else
  452. {
  453. cout << flights[i]->getYear()<<" "
  454. << flights[i]->getMonthDay()<<" "
  455. << flights[i] ->getDay()<<" "
  456. << flights[i]->getFlightNum()<<" "
  457. << flights[i]->getCityPair()<<" "
  458. << setw(10)<< flights[i]->getFlightLeg()<<" "
  459. << flights[i]->getLastName()<<","
  460. << flights[i]->getFirstName()<<endl;
  461.  
  462.  
  463. }
  464. cout << flights[i]->getYear()<<" "
  465. << flights[i]->getMonthDay()<<" "
  466. << flights[i] ->getDay()<<" "
  467. << flights[i]->getFlightNum()<<" "
  468. << flights[i]->getCityPair()<<" "
  469. << setw(3) << passLegTotal[i]<<" " <<"*"<<setw(26)<<"Passengers this leg"
  470. <<endl;
  471.  
  472. //this checks to see if its the last value
  473. if(flights[i] == flights.back() || !(compareFlightDate(flights[i+1],flights[i])) )
  474. {
  475. cout << flights[i]->getYear()<<" "
  476. << flights[i]->getMonthDay()<<" "
  477. << flights[i] ->getDay()<<" "
  478. << flights[i]->getFlightNum()<<" "
  479. << setw(11) << passAllTot << " "<<"**"<<setw(25) << "Passengers all Legs"<<endl;
  480. passAllTot = 1;
  481. }
  482. if(flights[i] == flights.back() || !(compareFlightDate(flights[i+1],flights[i])) )
  483. {
  484.  
  485. cout << flights[i]->getYear()<<" "
  486. << flights[i]->getMonthDay()<<" "
  487. << flights[i] ->getDay()<<" "
  488. << setw(17) << dateTotal << " "<<"***"<< setw(15)<<"Date Total"<<endl;
  489. dateTotal = 1;
  490.  
  491. }
  492. if(flights[i] == flights.back() ||!(flights[i+1]->getMonth() == flights[i]->getMonth()))
  493. {
  494. cout << flights[i]->getYear()<<" "
  495. << flights[i]->getMonthDay()<<" "
  496. <<setw(19) << monthTot << " "<<"****"<<" "<< setw(14)<<"Month Total"<<endl;
  497. monthTot = 1;
  498. }
  499. grandTotal++;
  500. if(flights[i] == flights.back())
  501. {
  502. cout << setw(32)<< grandTotal << " "<<"*****"<<" "<<setw(13) <<"Grand Total"<<endl;
  503. }
  504. cout<<endl;
  505.  
  506.  
  507. }
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement