Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Zac Cooner
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include "Box.h"
- #include "Rocket.h"
- #include "Queue.h"
- using namespace std;
- double checkWeight(Queue * destPtr)
- {
- Box * travel;
- int i = 0;
- double total = 0;
- travel = (*destPtr).head;
- for (double weightCount = 0; i < 5; i++)
- {
- weightCount += travel->weight;
- total = weightCount;
- }
- return total;
- }
- int checkBays(Queue * destPtr)
- {
- int boxCount = 0;
- Box* travel;
- travel = (*destPtr).head;
- for (int checker = 0; travel != NULL; travel = travel->next)
- {
- checker++;
- boxCount = checker;
- }// End for loop that increases checker per box in the list.
- return boxCount; // Will return the number of boxes. If 5 or more, the rocket can be sent.
- }// End checkBays function
- void validate(int i, char c, string& s, double d, Queue* &list, int dayCount, Queue* marsPtr, Queue* issPtr, Queue* moonPtr)
- {
- if (i < 1) // If I is negative it is invalid
- {
- return;
- }
- c = toupper(c); // Make the contents of 'c' uppercase
- if (c != 'S' || c != 'L' || c != 'M') // If the char does not represent one of the destinations, it is invalid
- {
- return;
- }
- if (d < 0) // If weight is negative, it is invalid
- {
- return;
- }
- const char* new_s = s.c_str(); // s is now a c_string
- const char * cPtr = new_s; // Set the pointer to the c_string
- ////////////////////////////////////////////////////////////////////
- //
- //This is where I need to check bays and send if necessary.
- ////////////////////////////////////////////////////////////////////
- if (i != dayCount)
- {
- if (checkBays(marsPtr) >= 5) // Print list, delete list (x5)
- {
- double extra = 0;
- double total = 0;
- if (checkWeight(marsPtr) > 1000) // Check the weight
- {
- extra = checkWeight(marsPtr) - 1000;
- total = list[2].head->weight + list[2].head->next->weight + list[2].head->next->next->weight + list[2].head->next->next->next->weight
- + list[2].head->next->next->next->next->weight; // Add the weights of all the nodes
- total -= extra; // Subtract the extra. weight should = 1000.
- Box * n = new Box(*(list[2].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
- list[2].Enqueue(*n); // Put the new box in the end of the queue.
- cout << list[2].head << endl; //1
- list[2].Dequeue();
- cout << list[2].head << endl; //2
- list[2].Dequeue();
- cout << list[2].head << endl; // 3
- list[2].Dequeue();
- cout << list[2].head << endl; // 4
- list[2].Dequeue();
- list[2].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
- cout << list[2].head << endl; // 5
- list[2].Dequeue(); // Delete the first five nodes in the list
- } // End if where mars weight exceeds 1000
- else
- {
- cout << list[2].head << endl;
- list[2].Dequeue();
- cout << list[2].head << endl;
- list[2].Dequeue();
- cout << list[2].head << endl;
- list[2].Dequeue();
- cout << list[2].head << endl;
- list[2].Dequeue();
- cout << list[2].head << endl;
- list[2].Dequeue(); // Delete the first five nodes in the list
- }
- } // End check mars
- else if (checkBays(moonPtr) >= 5) // Print list, delete list (x5)
- {
- double extra = 0;
- if (checkWeight(moonPtr) > 1000) // Check the weight
- {
- extra = checkWeight(moonPtr) - 1000;
- Box * n = new Box(*(list[1].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
- list[1].Enqueue(*n); // Put the new box in the end of the queue.
- cout << list[1].head << endl; //1
- list[1].Dequeue();
- cout << list[1].head << endl; //2
- list[1].Dequeue();
- cout << list[1].head << endl; // 3
- list[1].Dequeue();
- cout << list[1].head << endl; // 4
- list[1].Dequeue();
- list[1].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
- cout << list[1].head << endl; // 5
- list[1].Dequeue(); // Delete the first five nodes in the list
- } // end if lunar weight exceeds 1000
- else
- {
- cout << list[1].head << endl;
- list[1].Dequeue();
- cout << list[1].head << endl;
- list[1].Dequeue();
- cout << list[1].head << endl;
- list[1].Dequeue();
- cout << list[1].head << endl;
- list[1].Dequeue();
- cout << list[1].head << endl;
- list[1].Dequeue(); // Delete the first five nodes in the list
- }// End else where weight does not exceed 100
- } // End check Moon
- else if (checkBays(issPtr) >= 5) // Print list, delete list (x5)
- {
- double extra = 0;
- if (checkWeight(issPtr) > 1000) // Check the weight
- { extra = checkWeight(issPtr) - 1000;
- Box * n = new Box(*(list[0].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
- list[0].Enqueue(*n); // Put the new box in the end of the queue.
- cout << list[0].head << endl; //1
- list[0].Dequeue();
- cout << list[0].head << endl; //2
- list[0].Dequeue();
- cout << list[0].head << endl; // 3
- list[0].Dequeue();
- cout << list[0].head << endl; // 4
- list[0].Dequeue();
- list[0].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
- cout << list[0].head << endl; // 5
- list[0].Dequeue(); // Delete the first five nodes in the list
- }
- else
- {
- cout << list[0].head << endl;
- list[0].Dequeue();
- cout << list[0].head << endl;
- list[0].Dequeue();
- cout << list[0].head << endl;
- list[0].Dequeue();
- cout << list[0].head << endl;
- list[0].Dequeue();
- cout << list[0].head << endl;
- list[0].Dequeue(); // Delete the first five nodes in the list
- }
- } // End check ISS
- }// End if where i != dayCount
- Box *p = new Box(i, c, *cPtr, d);
- if (c == 'S') // If destination is ISS, stack the ISS queue.
- {
- list[0].Enqueue(*p);
- }
- else if (c == 'L') //If destination is Moon, stack the Moon queue.
- {
- list[1].Enqueue(*p);
- }
- else if (c == 'M') //If destination is Mars, stack the Mars queue.
- {
- list[2].Enqueue(*p);
- }
- }
- int main()
- {
- ifstream myfile("cargo.txt");
- string line;
- //Queue mars = *new Queue(&b);
- Queue *Destinations[3]; // 0 = ISS. 1 = Lunar. 2 = Mars.
- Queue *marsPtr = *(Destinations + 2); // Set the mars pointer to the third element
- Queue *moonPtr = *(Destinations + 1); // Set the moon pointer to the second element
- Queue *issPtr = *(Destinations); // Set the iss pointer to the first element
- bool countFound = false;
- int dayCount; // To keep track of the day.
- // ifstream out("launch.txt", ios::out); // Declare the output file stream object.
- if (myfile.is_open())
- {
- while (getline(myfile, line))
- {
- //validate each line as it comes
- istringstream iss(line);
- do
- {
- int i; // To represent the day
- char c; // To represent the destination character
- string s; // To represent the cargo name
- double d; // To represent the weight of the cargo
- if ((iss >> i) && (iss >> c) && (iss >> s) && (iss >> d))
- {
- if (countFound == false)
- {
- dayCount = i; // Set count to the integer in the first succesful line process
- countFound = true; // countFound is true now.
- } // End if countFound is false.
- validate(i, c, s, d, *Destinations, dayCount, marsPtr, issPtr, moonPtr);
- }// End if all stream data was sent to the proper variables
- //else
- //{break;}
- } while (iss);
- //send each cargo to its appropiate destination
- }// While the file can still get the line.
- myfile.close();
- }// end if file is open
- }// End main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement