This code represents a structure-based system to manage and update...

August 27, 2025 at 03:30 PM

#include <stdio.h> #include <stdlib.h> #include <string.h> // Struct Cliente typedef struct { char nome[20]; char cognome[20]; char email[30]; double puntiFedelta; } Cliente; // Struct Ordine typedef struct { char cid[20]; int quantita; double prezzo; char prodotto[20]; } Ordine; // Enum per distinguere il tipo di struttura typedef enum { StructCliente, StructOrdine } TipoStruct; // Union per un Cliente o un Ordine typedef union { Cliente cliente; Ordine ordine; } DatiRegistro; // Registro che associa tipo e dati typedef struct { TipoStruct tipo; DatiRegistro dati; } Registro; // Funzione polimorfica di stampa sicura void gestisciRegistro(Registro* reg) { if (reg->tipo == StructCliente) { Cliente* cliente = &reg->dati.cliente; printf("Cliente:\n"); printf(" %s %s - %s - Punti: %.2f\n", cliente->nome, cliente->cognome, cliente->email, cliente->puntiFedelta); } else if (reg->tipo == StructOrdine) { Ordine* ordine = &reg->dati.ordine; double totale = ordine->quantita * ordine->prezzo; printf("Ordine:\n"); printf(" [%s] %s Q:%d Prezzo:%.2f Totale:%.2f\n", ordine->cid, ordine->prodotto, ordine->quantita, ordine->prezzo, totale); } } // Aggiorna un registro Cliente ordine Ordine void aggiornaRegistro(const char* filename, const char* chiave, double nuovoValore, int isCliente) { FILE* f = fopen(filename, "r"); if (!f) { perror("Errore apertura file reg"); return; } Registro regs[100]; int n = 0; // Carico i dati dal file if (isCliente) { while (fscanf(f, "%19s %19s %29s %lf", regs[n].dati.cliente.nome, regs[n].dati.cliente.cognome, regs[n].dati.cliente.email, &regs[n].dati.cliente.puntiFedelta) == 4) { regs[n].tipo = StructCliente; n++; } } else { while (fscanf(f, "%19s %19s %d %lf", regs[n].dati.ordine.cid, regs[n].dati.ordine.prodotto, &regs[n].dati.ordine.quantita, &regs[n].dati.ordine.prezzo) == 4) { regs[n].tipo = StructOrdine; n++; } } fclose(f); // Aggiornamento int trovato = 0; for (int i = 0; i < n; i++) { if (regs[i].tipo == StructCliente && isCliente) { Cliente* cliente = &regs[i].dati.cliente; if (strcmp(cliente->email, chiave) == 0) { printf("Trovato Cliente %s %s, aggiorno punti da %.2f a %.2f\n", cliente->nome, cliente->cognome, cliente->puntiFedelta, cliente->puntiFedelta + nuovoValore); cliente->puntiFedelta += nuovoValore; trovato = 1; gestisciRegistro(&regs[i]); break; } } else if (regs[i].tipo == StructOrdine && !isCliente) { Ordine* ordine = &regs[i].dati.ordine; if (strcmp(ordine->cid, chiave) == 0) { printf("Trovato Ordine [%s], aggiorno prezzo da %.2f a %.2f\n", ordine->cid, ordine->prezzo, nuovoValore); ordine->prezzo = nuovoValore; trovato = 1; gestisciRegistro(&regs[i]); break; } } } if (!trovato) { printf("Record %s non trovato.\n", chiave); return; } // Riscrittura file aggiornata f = fopen(filename, "w"); if (!f) { perror("Errore scrittura file reg"); return; } for (int i = 0; i < n; i++) { if (regs[i].tipo == StructCliente) { Cliente* cliente = &regs[i].dati.cliente; fprintf(f, "%s %s %s %.2f\n", cliente->nome, cliente->cognome, cliente->email, cliente->puntiFedelta); } else { Ordine* ordine = &regs[i].dati.ordine; fprintf(f, "%s %s %d %.2f\n", ordine->cid, ordine->prodotto, ordine->quantita, ordine->prezzo); } } fclose(f); } int main() { char scelta[20]; printf("Vuoi aggiornare Cliente o Ordine? "); scanf("%19s", scelta); if (strcmp(scelta, "Cliente") == 0 || strcmp(scelta, "cliente") == 0) { char email[30]; double punti; printf("Inserisci email cliente da aggiornare: "); scanf("%29s", email); printf("Punti omaggio: "); scanf("%lf", &punti); aggiornaRegistro("clienti.txt", email, punti, 1); } else if (strcmp(scelta, "Ordine") == 0 || strcmp(scelta, "ordine") == 0) { char cid[20]; double prezzo; printf("Inserisci CID ordine da aggiornare: "); scanf("%19s", cid); printf("Nuovo prezzo: "); scanf("%lf", &prezzo); aggiornaRegistro("ordini.txt", cid, prezzo, 0); } else { printf("Scelta non valida.\n"); } return 0; }

This code represents a structure-based system to manage and update records for either customers (Cliente) or orders (Ordine). Here's a breakdown of what it does:


1. Struct Definitions

  • Defined two structures, Cliente and Ordine, to store customer data and order details respectively.
  • Defined an enum (TipoStruct) to differentiate between these two structures.
  • Used a union (DatiRegistro) to hold either a Cliente or an Ordine.
  • Finally, defined a Registro structure that combines TipoStruct (type) and DatiRegistro (data) for flexible record handling.

2. Functionality

The main functionalities are encapsulated in the functions below:

A. gestisciRegistro

  • Prints the details of a Registro object.
  • Depending on whether the type is a Cliente or an Ordine, it formats and prints respective fields.

B. aggiornaRegistro

This function manages updating either a Cliente or Ordine record:

  • Reads from a file (filename) that stores predefined Cliente or Ordine data.
  • Updates a specific record based on a key (chiave), which is either:
    • The email for a Cliente or
    • The cid (order ID) for an Ordine.
  • For Cliente, modifies the loyalty points (puntiFedelta), and for Ordine, updates the price.
  • Writes the updated data back to the file.
  • The file is handled safely with reopening for reading and writing.

3. main Function

The program's entry point handles interaction with the user:

  • Prompts the user to choose between updating a Cliente or an Ordine.
  • Depending on the choice, it asks for:
    • For Cliente:
      1. The customer's email as the key.
      2. The amount of loyalty points to add.
    • For Ordine:
      1. The order's cid as the key.
      2. The new price to assign.
  • It calls aggiornaRegistro with the appropriate parameters (filename, keys, and update values).

4. File Operations

  • File Input: Reads records of Cliente or Ordine from a file (clienti.txt or ordini.txt) based on the operation type.
  • File Update: Writes the entire updated record set back to the same file after making necessary changes.
  • Uses fscanf for reading and fprintf for writing.

5. Key Features

  • Polymorphic Behavior: Registers (Registro) can store different types of data (Cliente or Ordine) using a combination of enum and union.
  • Dynamic Updating: The program allows selective updating of fields in data files (clienti.txt for customer data, ordini.txt for order data).
  • Safe Input Handling: Uses formatted reading (fscanf) and writing (fprintf) to handle data safely.

6. Limitations

  • Assumes a maximum of 100 records (regs[100] array).
  • File format is assumed to match the expected structure for Cliente or Ordine without validation.
  • No proper error checking is implemented beyond basic file handling.

Summary

The code is a basic database management program using file I/O, structures, and unions. It updates Cliente or Ordine records stored in text files and outputs updated information in a user-friendly format.

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