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>
- using namespace std;
- class Box // Box represents the package to be sent off
- {
- public:
- int arrivalDay;
- char * cargo;
- char destination;
- double weight;
- Box( int i, char c, const char dest, double d)//Overloaded constructor
- {
- arrivalDay =i;
- cargo = &c;
- destination = dest;
- weight = d;
- }
- double operator+(Box b) // Binary plus operator
- {
- return this->weight + b.weight;//Return the added weights.
- }
- ostream& operator<<(Box* &b)//The stuff starting from 'operator' are function parameters.
- {
- double w = b->weight;
- stringstream os;
- char c=*cargo;
- double gravity;
- if(c=='S') // If destination is ISS, gravity is 1.
- {
- gravity=1;
- }else if (c=='L') // If destination is Moon, gravity is .1655
- {
- gravity=.1655;//The gravity constants.
- }else // // If destination is Mars, gravity is .3895
- {
- gravity=.3895;
- }
- w=w*gravity; // Weight equals the weight times the gravity factor at destination
- os << cargo << " " << w <<endl; // Needs formatting. Will return the cargo name and the destination weight.
- return os;
- }
- Box *next; // Next pointer
- };
- class Rocket
- {
- Box bay[5]; // Each rocket has 5 bays that need to be filled before launch.
- int dest; // Will number the destinations using the notation within the list array. Ex, 0 = ISS, 1 = Moon, 2 = Mars.
- void setDest(int i)
- {
- dest=i;
- }
- double getWeight()
- {
- double d;
- for(int i=0;i<=4;i++)
- {
- d+=bay[i].weight; //Add weight to the overall weight of the Rocket.
- }
- return d;
- }// End getWeight()
- ostream& operator<<(Rocket* &b)//Same idea as above, except using the << operator now. Send a pointer to a rocket by reference.
- {
- stringstream os;
- for(int i=0;i<=4;i++)
- {
- os<<&bay[i];
- }
- return os;
- }
- };
- class Queue
- {
- public:
- bool setup=true;
- Box *head; // Head pointer
- Box *tails; // Tail pointer
- void Enqueue(Box &b)
- {
- if(setup) // On the first go, set the head to the first box.
- {
- head=&b;
- head->next=tails;
- setup=false; // Turn off the bool so it doesn't set head again.
- }else{
- //add to last on the list
- tails->next = &b;
- tails = &b;
- }
- }
- void Dequeue() //Delete first one on the list.
- {
- head = head->next;
- }
- };
- void validate(int i, char c, string& s, double d, Queue* &list)
- {
- if (i < 1)
- {return;}
- c = toupper(c); // Make the contents of 'c' uppercase
- if (c != 'S' || c != 'L' || c != 'M')
- {return;}
- if (d < 0)
- {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
- 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.
- if (myfile.is_open())
- {
- while (getline(myfile,line))
- {
- //validate each line as it comes
- istringstream iss(line);
- do
- {
- int i;
- char c;
- string s;
- double d;
- if((iss>>i)&&(iss>>c)&&(iss>>s)&&(iss>>d))
- {
- validate(i,c,s,d,*Destinations);
- }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