This code implements a program that allows the user to...

August 27, 2025 at 02:16 PM

#include <stdio.h> #include <string.h> // Struct Cliente typedef struct { char nome[20]; char cognome[20]; char email[20]; int puntiFedelta; } Cliente; // Struct Ordine typedef struct { char prodotto[20]; char codiceid[20]; double prezzo; } Ordine; // Enum per distinguere cosa contiene il registro typedef enum { StructCliente, StructOrdine, Vuoto } TipoStruttura; // Unione Cliente/Ordine con tag typedef struct { TipoStruttura tipo; union { Cliente client; Ordine ordine; } data; } Registro; // Stampa con gestione di tipo void stampaRegistro(Registro* registro) { // Controllo il tipo di dati da stampare if (registro->tipo == StructCliente) { printf("\n CLIENTE\n"); printf("Nome: %s Cognome: %s\nE-mail: %s\nPunti Fedelta: %d\n", registro->data.client.nome, registro->data.client.cognome, registro->data.client.email, registro->data.client.puntiFedelta); } else if (registro->tipo == StructOrdine) { printf("\n Ordine\n"); printf("Prodotto: %s\nCodice ID: %s\nPrezzo: %.2f\n", registro->data.ordine.prodotto, registro->data.ordine.codiceid, registro->data.ordine.prezzo); } else { printf("Registro vuoto o non valido.\n"); } } int main() { // Inizializzo il registro come vuoto Registro registro; registro.tipo = Vuoto; // Scelta del tipo fra Cliente e Ordine dal utente char scelta[20]; printf("Vuoi aggiungere: Cliente o Ordine? \n"); fgets(scelta, sizeof(scelta), stdin); scelta[strcspn(scelta, "\n")] = 0; if (strcmp(scelta, "Cliente") == 0 || strcmp(scelta, "cliente") == 0) { registro.tipo = StructCliente; printf("Nome: "); fgets(registro.data.client.nome, sizeof(registro.data.client.nome), stdin); registro.data.client.nome[strcspn(registro.data.client.nome, "\n")] = 0; printf("Cognome: "); fgets(registro.data.client.cognome, sizeof(registro.data.client.cognome), stdin); registro.data.client.cognome[strcspn(registro.data.client.cognome, "\n")] = 0; printf("E-mail: "); fgets(registro.data.client.email, sizeof(registro.data.client.email), stdin); registro.data.client.email[strcspn(registro.data.client.email, "\n")] = 0; printf("Punti Fedelta: "); scanf("%d", &registro.data.client.puntiFedelta); } else if (strcmp(scelta, "Ordine") == 0 || strcmp(scelta, "ordine") == 0) { registro.tipo = StructOrdine; printf("Prodotto: "); fgets(registro.data.ordine.prodotto, sizeof(registro.data.ordine.prodotto), stdin); registro.data.ordine.prodotto[strcspn(registro.data.ordine.prodotto, "\n")] = 0; printf("Codice Identificativo: "); fgets(registro.data.ordine.codiceid, sizeof(registro.data.ordine.codiceid), stdin); registro.data.ordine.codiceid[strcspn(registro.data.ordine.codiceid, "\n")] = 0; printf("Prezzo: "); scanf("%lf", &registro.data.ordine.prezzo); } else { printf("Scelta non valida.\n"); } // Passo i dati inseriti alla funzione di stampa stampaRegistro(&registro); return 0; }

This code implements a program that allows the user to store and view information about either a customer ("Cliente") or an order ("Ordine") using a tagged union within a structure (Registro). Here's a breakdown of what it does:

Key Components:

1. Structures:

  • Cliente: Stores information about a customer, including nome (name), cognome (surname), email, and puntiFedelta (loyalty points).
  • Ordine: Stores details of an order, including prodotto (product), codiceid (ID code), and prezzo (price).

2. Union and Enum:

  • union inside Registro allows either a Cliente or an Ordine to be stored in the same memory space, saving memory by storing one data type at a time.
  • TipoStruttura (an enum) is used as a tag to determine which type of data (Cliente, Ordine, or Vuoto) is currently stored in the union.

3. Functions:

  • stampaRegistro: Prints the contents of the Registro based on the type of data it holds (StructCliente or StructOrdine).
  • Example:
    • If the tag indicates StructCliente, it prints customer details.
    • If the tag indicates StructOrdine, it prints order details.
    • If the tag is Vuoto or invalid, it prints a message saying the register is empty or invalid.

4. Main Routine:

  • Initializes an empty Registro with Vuoto as its type.
  • Prompts the user to input their choice ("Cliente" or "Ordine").
  • Reads and stores the user-provided data based on their choice. For Cliente or Ordine, the respective part of the union is populated with data.
  • Uses fgets for string input to handle whitespace.
  • Calls stampaRegistro to print the information back to the user.

Execution Flow:

  1. The program asks whether the user wants to add a Cliente (customer) or an Ordine (order).
  2. Based on the input:
    • It collects the required details (nome, email, etc., for a Cliente or prodotto, prezzo, etc., for an Ordine).
    • The respective portion of the union is filled, and the enum tag (StructCliente or StructOrdine) is set accordingly.
  3. The stampaRegistro function is called to display the stored data.
  4. If the user's choice is invalid, the program informs them and exits.

Example Trial:

Input 1:

Vuoi aggiungere: Cliente o Ordine?
Cliente
Nome: Maria
Cognome: Rossi
E-mail: maria.rossi@example.com
Punti Fedelta: 120

Output:

        CLIENTE
Nome: Maria Cognome: Rossi
E-mail: maria.rossi@example.com
Punti Fedelta: 120

Input 2:

Vuoi aggiungere: Cliente o Ordine?
Ordine
Prodotto: Laptop
Codice Identificativo: X123YZ
Prezzo: 799.99

Output:

        Ordine
Prodotto: Laptop
Codice ID: X123YZ
Prezzo: 799.99

Input 3:

Vuoi aggiungere: Cliente o Ordine?
Altro

Output:

Scelta non valida.

Summary:

This C program effectively demonstrates how tagged unions can be used for memory optimization while managing heterogeneous data. It allows the user to store and retrieve information about either customers or orders based on their choice, ensuring type safety and clear structure via the enum tag.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node