Prova scritta 10/02/2025 #include #include using namespace std; struct Transazione { char id[11]; char sorgente[21]; char destinatario[21]; double valore; bool esito; Transazione* pun; }; struct SmartContract { Transazione* attesa; Transazione* blockchain; int quanti_attesa; }; void Inizializza(SmartContract& s) { s.attesa = NULL; s.blockchain = NULL; s.quanti_attesa = 0; } void AggiungiInAttesa(SmartContract& s, char* id, char* src, char* dst, double quanti) { if(strlen(id) == 0 || strlen(id) > 10 || strlen(src) == 0 || strlen(src) > 20 || strlen(dst) == 0 || strlen(dst) > 20 || quanti < 0.0) { cout << "Errore" << endl; return; } Transazione* q,* p; for (q = s.attesa; q != NULL; q = q->pun) { if (strcmp(q->id, id) == 0) { // id giĆ  presente return; } p = q; } Transazione* r = new Transazione; r->valore = quanti; strcpy(r->sorgente, src); strcpy(r->destinatario, dst); strcpy(r->id, id); r->esito = false; r->pun = NULL; if (s.attesa == NULL) { s.attesa = r; } else { p->pun = r; } s.quanti_attesa++; } void Stampa(SmartContract s) { cout << "[" << s.quanti_attesa << "]"; Transazione* q; for (q = s.blockchain; q != NULL; q = q->pun) { cout << " <" << q->id << "-" << q->valore << ">"; } cout << endl; } bool ConfermaPagamento(SmartContract& s, char* id) { if (strlen(id) == 0 || strlen(id) > 10) { return false; } Transazione* q; for (q = s.attesa; q != NULL; q = q->pun) { if (strcmp(q->id, id) == 0 && !q->esito) { q->esito = true; return true; } } return false; } bool SpostaInBlockchain(SmartContract& s) { Transazione* q, *p; for (q = s.attesa; q != NULL && !q->esito; q = q->pun) { p = q; } if (q == NULL) { return false; } Transazione* q_block, *p_block; for (q_block = s.blockchain; q_block != NULL; q_block = q_block->pun) { p_block = q_block; } if (q == s.attesa) { //Tolgo il primo in lista d'attesa s.attesa = q->pun; q->pun = NULL; } else { //tolgo in mezzo alla lista d'attesa, o in fondo p->pun = q->pun; q->pun = NULL; } if (s.blockchain == NULL) { //Inserisco il primo elemento della blockchain s.blockchain = q; } else { //Inserisco in fondo p_block->pun = q; } s.quanti_attesa--; return true; } double SoldiSpesi(SmartContract& s, char* src) { if (strlen(src) == 0 || strlen(src) > 20) { return -1; } Transazione* q; double totale = 0.0; for (q = s.blockchain; q != NULL; q = q->pun) { if (strcmp(q->sorgente, src) == 0) { totale += q->valore; } } return totale; } void ModificaPagamento(SmartContract& s, char* src) { if (strlen(src) == 0 || strlen(src) > 20) { return; } Transazione* q; for (q = s.attesa; q != NULL; q = q->pun) { if (strcmp(q->sorgente, src) == 0 && !q->esito) { q->valore = q->valore * 2; } } }