Menu
Coddy logo textTech

Mostrare le competenze

Lezione 6 di 8 del corso Classe Utente - Progetto di programmazione a oggetti di Coddy.

challenge icon

Sfida

Facile

Aggiungi un metodo chiamato showSkills che stampa informazioni sulle competenze dell'utente in un formato specifico.

Ad esempio, se le competenze dell'utente sono:

  • "Python" è al livello 2
  • "C++" è al livello 1
  • "HTML" è al livello 3

La chiamata a showSkills() produrrà:

C++: 1
HTML: 3
Python: 2

Le competenze sono mostrate in ordine alfabetico in base al nome della competenza.

Provalo tu

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"

int main() {
    User u;
    User_init(&u, "", "");
    char line[1024];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "new") == 0) {
            char* un = strtok(NULL, " \t");
            char* em = strtok(NULL, " \t");
            User_init(&u, un ? un : "", em ? em : "");
        }
        if (strcmp(cmd, "username") == 0) { printf("%s\n", u.username); }
        if (strcmp(cmd, "email") == 0) { printf("%s\n", u.email); }
        if (strcmp(cmd, "setLocation") == 0) { char* v = strtok(NULL, " \t"); if (v) User_setLocation(&u, v); }
        if (strcmp(cmd, "getLocation") == 0) { printf("%s\n", User_getLocation(&u)); }
        if (strcmp(cmd, "info") == 0) { User_info(&u); }
        if (strcmp(cmd, "learn") == 0) { char* v = strtok(NULL, " \t"); if (v) User_learn(&u, v); }
        if (strcmp(cmd, "showSkills") == 0) { User_showSkills(&u); }
    }
    return 0;
}

Tutte le lezioni di Classe Utente - Progetto di programmazione a oggetti

Esercitati da solo: Compilatore C online