1° Appello - 01/01/2020 - Parte Pratica

Parte Pratica

immagine

MAIN CPP

#include <iostream>
#include "dati.h"

int main(int argc, char** argv) {
	Tcloud* lst=NULL;
	Tserver s;
	for(int i=0;i<10;i++){
		creaServer(&s);
		lst =inserisciServer(lst,s);
	}
	stampaServer(lst);
	salvaServer(lst);
	serverEquivalente(lst,MAC).stampa();
	return 0;
}

dati.h

#ifndef __DATI_H__
#define __DATI_H__
#include <cstdlib>

typedef enum Tsistoper{ 
	WIN, 
	MAC,
	LINUX
}Tsistoper;
const char* so2String(Tsistoper ts){
	switch(ts){
		case 0: return "WIN";
		case 1: return "MAC";
		case 2: return "LINUX";
	}
}
typedef struct Tserver{
	char hostname[20];
	int numCPU, ram;
	Tsistoper so;
	Tserver(){
		hostname[0] = '\0';
		numCPU = 0;
		ram = 0;
		so = WIN;
	}
	void stampa() const{
		printf("%s(%s) RAM=%d CPU=%d\n",hostname, so2String(so), ram, numCPU);
	}
}Tserver;

typedef struct Tcloud{
	Tserver server;
	Tcloud* next;
	Tcloud(){
		server = Tserver();
		next = NULL;
	}
	Tcloud(Tserver s, Tcloud* n){
		server = s;
		next = n;
	}
	void stampa(){
		server.stampa();
	}
}Tcloud;
int casuale(int min, int max){
	return rand()%(max-min+1)+min;
}
//C
void creaServer(Tserver* s){
	switch(casuale(0,2)){
		case 0:
			s->so = WIN;
			break;
		case 1:
			s->so = MAC;
			break;
		case 2:
			s->so = LINUX;
			break;
	}
	s->numCPU = casuale(4,8);
	do{
		printf("Ram:\n>");
		scanf("%d",&(s->ram));
	}while(s->ram < 4 || s->ram > 16 || s->ram%2!=0);
	printf("Hostname:\n>");
	fflush(stdin);
	scanf("%s",&(s->hostname));
}
Tcloud* insertLast(Tcloud* lst, Tserver s){
	Tcloud* cur = lst;
	if(cur!=NULL){
		while(cur->next!=NULL){
			cur = cur->next;
		}
		cur->next = new Tcloud(s, NULL);
		return lst;
	}
	else{
		return new Tcloud(s, NULL);
	}
}
//D
Tcloud* inserisciServer(Tcloud* pc, Tserver s){
	return insertLast(pc, s);
}

//E
void stampaServer(Tcloud* pc){
	if(pc!=NULL){
		Tcloud* cur = pc;
		while(cur!=NULL){
			cur->stampa();
			cur = cur->next;
		}
	}
	else{
		printf("Lista vuota!\n");
	}
}

//F
void salvaServer(Tcloud* pc){	
	if(pc!=NULL){
		FILE* fp = fopen("server.txt", "a");
		if(fp!=NULL){
			Tcloud* cur = pc;
				while(cur!=NULL){
					if(cur->server.ram > 8 && cur->server.numCPU==6){
						fprintf(fp,"%s(%s) RAM=%d CPU=%d\n",cur->server.hostname, so2String(cur->server.so), cur->server.ram, cur->server.numCPU);
					}
				cur = cur->next;
			}
				
		}else{
			printf("Errore apertura file!\n");
		}
		fclose(fp);
	}
}

//G
Tserver serverEquivalente(Tcloud* pc, Tsistoper sis){
	int countRAM = 0;
	int countCPU = 0;
	if(pc!=NULL){		
		Tcloud* cur = pc;
		while(cur!=NULL){
			if(cur->server.so == sis){
				countRAM += cur->server.ram;
				countCPU += cur->server.numCPU;
			}
			cur = cur->next;
		}
	}
	Tserver ret = Tserver();
	ret.numCPU = countCPU;
	ret.ram = countRAM;
	ret.so = sis;
	return ret;
}
#endif