Last active
September 28, 2016 19:24
-
-
Save Steffo99/01fd7cfa0a67b197ce35cff14fae036f to your computer and use it in GitHub Desktop.
Homework...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| #define ERRORE_TIPO_INESISTENTE -1 | |
| #define ERRORE_MANCANO_GIORNI -2 | |
| const int TRATTENUTA_MALATTIA = 3; | |
| const int TRATTENUTA_FERIE = 5; | |
| const int GIORNI_MALATTIA = 40; | |
| const int GIORNI_FERIE = 30; | |
| const int NUMERO_DIPENDENTI = 3; | |
| class Dipendente | |
| { | |
| private: | |
| char nome[255]; | |
| char cognome[255]; | |
| int ferie; | |
| int malattia; | |
| public: | |
| char matricola[10]; | |
| //Inizializza le variabili. | |
| void init() | |
| { | |
| this->ferie = GIORNI_FERIE; | |
| this->malattia = GIORNI_MALATTIA; | |
| //Richiedi l'inserimento di nome, cognome e matricola. | |
| cout << "Nome: "; | |
| cin >> this->nome; | |
| cout << "Cognome: "; | |
| cin >> this->cognome; | |
| cout << "Matricola (max 10 caratteri): "; | |
| cin >> this->matricola; | |
| } | |
| //Visualizza lo stato attuale dell'utente. | |
| void display() | |
| { | |
| cout << "Nome: " << this->nome << "\n"; | |
| cout << "Cognome: " << this->cognome << "\n"; | |
| cout << "Matricola: " << this->matricola << "\n"; | |
| cout << "Ferie: " << this->ferie << "\n"; | |
| cout << "Malattia: " << this->malattia << "\n"; | |
| } | |
| int giornitotali() | |
| { | |
| return this->ferie + this->malattia; | |
| } | |
| //Restituisce la trattenuta se la richiesta ha successo; in caso di fallimento restituisce un numero negativo corrispondente all'errore. | |
| int permesso(char tipo, unsigned int giorni) | |
| { | |
| if(tipo == 'f') | |
| { | |
| if(giorni <= this->ferie) | |
| { | |
| this->ferie -= giorni; | |
| return giorni * TRATTENUTA_FERIE; | |
| } | |
| else | |
| { | |
| return ERRORE_MANCANO_GIORNI; | |
| } | |
| } | |
| else if(tipo == 'm') | |
| { | |
| if(giorni <= this->malattia) | |
| { | |
| this->malattia -= giorni; | |
| return giorni * TRATTENUTA_MALATTIA; | |
| } | |
| else | |
| { | |
| return ERRORE_MANCANO_GIORNI; | |
| } | |
| } | |
| else | |
| { | |
| return ERRORE_TIPO_INESISTENTE; | |
| } | |
| } | |
| }; | |
| //Esegui un selection sort della lista dei dipendenti e visualizza in ordine lo stato | |
| void sortanddisplay(Dipendente listaoriginale[NUMERO_DIPENDENTI]) | |
| { | |
| //Copia la lista originale | |
| Dipendente lista[NUMERO_DIPENDENTI]; | |
| for(int i = 0; i < NUMERO_DIPENDENTI; i++) | |
| { | |
| lista[i] = listaoriginale[i]; | |
| } | |
| //Sort | |
| for(int i = 0; i < NUMERO_DIPENDENTI; i++) | |
| { | |
| //Trova la posizione del massimo | |
| int max_found = i; | |
| for(int j = i+1; j < NUMERO_DIPENDENTI; j++) | |
| { | |
| if(lista[j].giornitotali() > lista[max_found].giornitotali()) | |
| { | |
| max_found = j; | |
| } | |
| } | |
| //Inverti il massimo con il numero attuale | |
| Dipendente temp = lista[i]; | |
| lista[i] = lista[max_found]; | |
| lista[max_found] = temp; | |
| } | |
| //Display | |
| cout << "ELENCO ORDINATO DELLE FERIE\n"; | |
| for(int i = 0; i < NUMERO_DIPENDENTI; i++) | |
| { | |
| cout << lista[i].matricola << ": " << lista[i].giornitotali() << "\n"; | |
| } | |
| } | |
| int main() | |
| { | |
| Dipendente lista[NUMERO_DIPENDENTI]; | |
| //Inserisci i dati di tutti i dipendenti | |
| for(int i = 0; i < NUMERO_DIPENDENTI; i++) | |
| { | |
| cout << "Dipendente #" << i+1 << "\n"; | |
| lista[i].init(); | |
| } | |
| //Ciclo principale | |
| while(true) | |
| { | |
| char tipopermesso; | |
| int idutente; | |
| unsigned int giorni; | |
| int risultato; | |
| //Ricevi in input i dati necessari | |
| cout << "Dipendente che richiede il permesso (0-" << NUMERO_DIPENDENTI-1 << "): "; | |
| cin >> idutente; | |
| cout << "(f)erie o (m)alattia? "; | |
| cin >> tipopermesso; | |
| cout << "Durata del permesso in giorni: "; | |
| cin >> giorni; | |
| //Controlla possibile errori | |
| if(idutente >= NUMERO_DIPENDENTI || idutente < 0) | |
| { | |
| cout << "L'utente selezionato non esiste.\n"; | |
| continue; | |
| } | |
| if(tipopermesso != 'f' && tipopermesso != 'm') | |
| { | |
| cout << "Il tipo di permesso selezionato non esiste.\n"; | |
| continue; | |
| } | |
| //Esegui il comando | |
| risultato = lista[idutente].permesso(tipopermesso, giorni); | |
| //Visualizza il risultato | |
| if(risultato == ERRORE_MANCANO_GIORNI) | |
| { | |
| cout << "Il dipendente selezionato non ha abbastanza giorni di permesso a disposizione.\n"; | |
| } | |
| else if(risultato > 0) | |
| { | |
| cout << "Il permesso prevede una trattenuta di " << risultato << " euro.\n"; | |
| } | |
| //Visualizza lo stato del dipendente. | |
| lista[idutente].display(); | |
| //Visualizza l'elenco dei dipendenti ordinato. | |
| sortanddisplay(lista); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment