Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- using namespace std;
- // Your Code goes here
- class Server{
- protected:
- char ip[46];
- char name[101];
- const void copy_obj(const Server ©){
- strcpy(this->ip, copy.ip);
- strcpy(this->name, copy.name);
- }
- public:
- Server(const char * ip="0.0.0.0", const char * name="NoName"){
- strcpy(this->ip, ip);
- strcpy(this->name, name);
- }
- Server(const Server ©){
- this->copy_obj(copy);
- }
- Server &operator=(const Server ©){
- if(this != ©)
- this->copy_obj(copy);
- return *this;
- }
- ~Server(){}
- virtual const double total_price() const {
- return 0;
- }
- };
- class DedicatedServer : public Server {
- private:
- double basePrice;
- int ram;
- int * hdds;
- int noHdds;
- const void copy_obj(const DedicatedServer ©){
- this->basePrice = copy.basePrice;
- this->ram = copy.ram;
- this->hdds = new int[copy.noHdds + 1];
- for(int i=0; i<copy.noHdds; i++)
- this->hdds[i] = copy.hdds[i];
- this->noHdds = copy.noHdds;
- }
- public:
- DedicatedServer(const char * ip="0.0.0.0", const char * name="NoName", const int basePrice=0, const int ram=0) : Server(ip, name){
- this->basePrice = basePrice;
- this->ram = ram;
- this->hdds = new int[0];
- this->noHdds = 0;
- }
- DedicatedServer(const DedicatedServer ©) : Server(copy){
- this->copy_obj(copy);
- }
- DedicatedServer &operator=(const DedicatedServer ©){
- if(this != ©){
- delete [] this->hdds;
- Server::operator=(copy);
- this->copy_obj(copy);
- }
- return *this;
- }
- ~DedicatedServer(){
- delete [] this->hdds;
- }
- const int hddSpace() const {
- int total=0;
- for(int i=0; i<this->noHdds; i++){
- total += this->hdds[i];
- }
- return total;
- }
- const double total_price() const {
- return this->basePrice + this->hddSpace() * 0.5 + this->ram * 20;
- }
- DedicatedServer &operator+= (int &add){
- int * tmp = new int[this->noHdds + 1];
- for(int i=0; i<this->noHdds; i++)
- tmp[i] = this->hdds[i];
- tmp[this->noHdds++] = add;
- delete [] this->hdds;
- this->hdds = tmp;
- return *this;
- }
- DedicatedServer operator++(int){
- this->ram++;
- return *this;
- }
- friend ostream &operator<<(ostream &os, DedicatedServer &orig){
- os << orig.name << ": " << orig.ip << "\n";
- os << orig.basePrice << ", " << orig.ram << "\n";
- os << orig.noHdds << ", " << orig.hddSpace() << "\n";
- return os;
- }
- };
- class VirtualServer : public Server {
- private:
- int noCores;
- int bandwidth;
- const void copy_obj(const VirtualServer ©){
- this->noCores = copy.noCores;
- this->bandwidth = copy.bandwidth;
- }
- public:
- VirtualServer(const char * ip="0.0.0.0", const char * name="NoName", const int noCores=0, const int bandwidth=0) : Server(ip, name){
- this->noCores = noCores;
- this->bandwidth = bandwidth;
- }
- VirtualServer(const VirtualServer ©) : Server(copy){
- this->copy_obj(copy);
- }
- VirtualServer &operator=(const VirtualServer ©){
- if(this != ©){
- Server::operator=(copy);
- this->copy_obj(copy);
- }
- return *this;
- }
- ~VirtualServer(){}
- const double total_price() const{
- return this->noCores * 5 + this->bandwidth * 10;
- }
- VirtualServer &operator+=(int add){
- this->noCores += add;
- return *this;
- }
- VirtualServer operator++(int){
- this->bandwidth++;
- return *this;
- }
- friend ostream &operator << (ostream &os, VirtualServer &orig){
- os << orig.name << ": " << orig.ip << "\n";
- os << orig.noCores << ", " << orig.bandwidth << "\n";
- return os;
- }
- };
- bool operator>(Server * left, Server &right){
- if(left->total_price() > right.total_price())
- return true;
- return false;
- }
- double totalCost(Server ** servers, int n){
- double total = 0.0;
- for(int i=0; i<n; i++)
- total += servers[i]->total_price();
- return total;
- }
- Server &biggestInvoice(Server ** servers, int n){
- double max=servers[0]->total_price();
- int index = 0;
- for(int i=0; i<n; i++){
- if(servers[i]->total_price() > max){
- max = servers[i]->total_price();
- index = i;
- }
- }
- return *servers[index];
- }
- // Testing
- int main() {
- int test_case;
- char hostname[101];
- char ip[46];
- int ram;
- int basic_price;
- int disk_space;
- int num_cores;
- int bandwith;
- int num_inc;
- cin >> test_case;
- if (test_case == 1) {
- // Test Case Business Invoice - Constructor, operator <<
- cin >> ip >> hostname;
- cin >> basic_price >> ram;
- DedicatedServer ds(ip, hostname, basic_price, ram);
- cout << ds;
- } else if (test_case == 2) {
- // Test Case Business Invoice - Constructor, operators <<, ++
- cin >> ip >> hostname;
- cin >> basic_price >> ram;
- DedicatedServer ds(ip, hostname, basic_price, ram);
- cout << ds;
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- ds++;
- }
- cout << ds;
- } else if (test_case == 3) {
- // Test Case Business Invoice - Constructor, total_price, operators <<,
- cin >> ip >> hostname;
- cin >> basic_price >> ram;
- DedicatedServer ds(ip, hostname, basic_price, ram);
- cout << ds;
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- ds++;
- }
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- cin >> disk_space;
- ds += disk_space;
- }
- cout << ds;
- } else if (test_case == 4) {
- cin >> ip >> hostname;
- cin >> basic_price >> ram;
- DedicatedServer ds(ip, hostname, basic_price, ram);
- cout << ds;
- cout << ds.total_price()<< endl;
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- ds++;
- }
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- cin >> disk_space;
- ds += disk_space;
- }
- cout << ds;
- cout << ds.total_price();
- } else if (test_case == 5) {
- cin >> ip >> hostname;
- cin >> num_cores >> bandwith;
- VirtualServer vs(ip, hostname, num_cores, bandwith);
- cout << vs;
- } else if (test_case == 6) {
- cin >> ip >> hostname;
- cin >> num_cores >> bandwith;
- VirtualServer vs(ip, hostname, num_cores, bandwith);
- cout << vs;
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- vs++;
- }
- cin >> num_inc;
- vs += num_inc;
- cout << vs;
- } else if (test_case == 7) {
- cin >> ip >> hostname;
- cin >> num_cores >> bandwith;
- VirtualServer vs(ip, hostname, num_cores, bandwith);
- cout << vs;
- cout << vs.total_price() << endl;
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- vs++;
- }
- cin >> num_inc;
- vs += num_inc;
- cout << vs;
- cout << vs.total_price();
- } else if (test_case == 8) {
- int num_servers;
- int server_type;
- cin >> num_servers;
- Server **s = new Server *[num_servers];
- for (int j = 0; j < num_servers; ++j) {
- cin >> server_type;
- if (server_type == 1) {
- cin >> ip >> hostname;
- cin >> basic_price >> ram;
- DedicatedServer *dsp =
- new DedicatedServer(ip, hostname, basic_price, ram);
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- (*dsp)++;
- }
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- cin >> disk_space;
- (*dsp) += disk_space;
- }
- cout << *(dsp);
- cout << dsp->total_price() << endl;
- s[j] = dsp;
- }
- if (server_type == 2) {
- cin >> ip >> hostname;
- cin >> num_cores >> bandwith;
- VirtualServer *vsp =
- new VirtualServer(ip, hostname, num_cores, bandwith);
- cin >> num_inc;
- for (int i = 0; i < num_inc; ++i) {
- (*vsp)++;
- }
- cin >> num_inc;
- (*vsp) += num_inc;
- cout << (*vsp);
- cout << vsp->total_price() << endl;
- s[j] = vsp;
- }
- }
- cout << "The cost of all servers is:\n";
- cout << totalCost(s, num_servers);
- cout << endl;
- cout << "Biggest invoice:\n";
- Server &server = biggestInvoice(s, num_servers);
- Server *ss = &server;
- DedicatedServer *bip;
- VirtualServer *pip;
- if (dynamic_cast<DedicatedServer *>(ss)) {
- bip = dynamic_cast<DedicatedServer *>(ss);
- cout << *bip << bip->total_price();
- }
- if (dynamic_cast<VirtualServer *>(ss)) {
- pip = dynamic_cast<VirtualServer *>(ss);
- cout << *pip << pip->total_price();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement