Untitled
unknown
plain_text
4 years ago
3.5 kB
8
Indexable
#include <bits/stdc++.h>
using namespace std;
class Planet
{
friend class Galaxy;
public:
Planet(string planet,int a,int b,int c,float m)
:name(planet),x(a),y(b),z(c),mass(m),next(NULL){}
Planet(const Planet &a)
:next(a.next),name(a.name),x(a.x),y(a.y),z(a.z),mass(a.mass){}
Planet *next;
float mass;
string name;
int x,y,z;
//Todo Constructor to initialize Planet
//Todo operator overloading
Planet operator +(const Planet &b){
Planet* pl= new Planet(*this);
pl->next=NULL;
pl->name+=b.name;
pl->x=floor((pl->x*pl->mass+b.x*b.mass)/(pl->mass+b.mass));
pl->y=floor((pl->y*pl->mass+b.y*b.mass)/(pl->mass+b.mass));
pl->z=floor((pl->z*pl->mass+b.z*b.mass)/(pl->mass+b.mass));
pl->mass+=b.mass;
return *pl;
}
bool operator ==(const Planet &b)
{
if(name!=b.name||next!=b.next||mass!=b.mass||x!=b.x||y!=b.y||z!=b.z)
return false;
return true;
}
bool operator >(const Planet &b){
return (mass>b.mass);
}
bool operator <(const Planet &b){
return (mass<b.mass);
}
void Info()
{
cout<<name<<'('<<x<<','<<y<<','<<z<<','<<mass<<")\n";
}
};
bool cmp(pair<float,Planet*> a,pair<float,Planet*> b){
return a.first<b.first;
}
class Galaxy
{
private:
string name;
Planet *head;
public:
Galaxy(string na, Planet A) : name(na)
{
head = &A;
}
void Add(Planet A)
{
if(head==NULL){
head = &A;
return;
}
Planet *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = &A;
}
void Delete(Planet &A)
{
if(head==NULL)
return;
if(*head==A)
{
Planet* q=head;
head=head->next;
delete q;
}
else
{
Planet* pre=head;
Planet* cur=head->next;
while(cur!=NULL)
{
if(*cur==A)
{
pre->next=cur->next;
delete cur;
return;
}
else
{
cur=cur->next;
pre=pre->next;
}
}
}
}
void Status()
{
cout<<name<<'\n';
Planet *q=head;
vector<pair<float,Planet*>> A;
while(q!=NULL)
{
A.push_back(make_pair(q->mass,q));
q=q->next;
}
sort(A.begin(),A.end(),cmp);
for(int i=A.size()-1;i>=0;i--)
A[i].second->Info();
}
~Galaxy()
{
Planet *q=head;
while(head!=NULL)
{
head=head->next;
delete q;
}
}
};
int main(){
Planet A = Planet("Alen",788,224,-301,4723.3);
Planet B = Planet("Grace",110,-8,23,80.9);
Galaxy a("Pretty",A);
if (A>B) cout<<"A>B"<<endl;
else cout<<"A<B"<<endl;
a.Status();
Planet C = Planet("Mary",472,-5,999,0.001);
Planet CC = Planet("Mary",472,-5,999,0.001);
if (C==CC) cout<<"!twins!"<<endl;
Galaxy b("Awesome",C);
Planet D = Planet("Jupyter",49,0,-1002,1231);
D.Info();
Planet F = C+B;
F.Info();
Galaxy c("Chill",F);
c.Delete(F);
c.Status();
Planet G = F+D;
b.Add(G);
b.Add(D);
a.Delete(A);
a.Add(F);
a.Add(B);
a.Add(G);
a.Status();
}Editor is loading...