29/01/2024, 16:19
#include <iostream>
using namespace std;
enum tipo_numero { PARI, DISPARI };
class ListaNumeri {
friend ostream& operator<<(ostream&, const ListaNumeri&);
class Lista {
struct elem {
int num;
elem* prox;
};
elem *testa;
void copia(const Lista&);
void elimina();
public:
Lista() { testa = NULL; }
Lista(const Lista& src) { copia(src); }
Lista& operator=(const Lista&);
void inserisci(int);
int estrai(int&);
int somma() const;
int operator!() const;
~Lista() { elimina(); }
};
// Crea due sottoliste: una per i numeri pari, una per i numeri dispari.
Lista liste[2];
public:
ListaNumeri() {}
ListaNumeri(const ListaNumeri&);
ListaNumeri& operator=(const ListaNumeri&);
int inserisci(int);
int estrai(tipo_numero, int&);
int operator!() const;
int operator%(tipo_numero) const;
int operator[](tipo_numero) const;
};
//
// Classe Lista
//
void ListaNumeri::Lista::copia(const ListaNumeri::Lista& src)
{
testa = NULL;
if (src.testa != NULL) {
testa = new elem;
testa->num = src.testa->num;
elem *temp = testa, *aux = src.testa;
while (temp != NULL) {
temp->prox = new elem;
temp = temp->prox;
temp->num = aux->num;
aux = aux->prox;
}
temp->prox = NULL;
}
}
void ListaNumeri::Lista::elimina()
{
elem *temp = testa;
while (temp != NULL) {
testa = testa->prox;
delete temp;
temp = testa;
}
}
ListaNumeri::Lista& ListaNumeri::Lista::operator=(const Lista& src)
{
if (this != &src) {
elimina();
copia(src);
}
return *this;
}
// Inserzione in coda.
void ListaNumeri::Lista::inserisci(int n)
{
elem *temp = new elem;
temp->num = n;
temp->prox = NULL;
if (testa == NULL) {
testa = temp;
} else {
elem *ultimo = testa;
while (ultimo->prox != NULL)
ultimo = ultimo->prox;
ultimo->prox = temp;
}
}
// Estrazione in testa.
int ListaNumeri::Lista::estrai(int& num_estratto)
{
if (testa != NULL) {
elem *temp = testa;
testa = testa->prox;
num_estratto = temp->num;
delete temp;
return 1;
}
return 0;
}
// Restituisce il numero di elementi presenti in una lista.
int ListaNumeri::Lista::operator!() const
{
int conta = 0;
for (elem *temp = testa; temp != NULL; temp = temp->prox)
conta++;
return conta;
}
// Restituisce la somma di tutti i valori memorizzati nella lista.
int ListaNumeri::Lista::somma() const
{
int somma = 0;
elem *aux = testa;
while (aux != NULL) {
somma += aux->num;
aux = aux->prox;
}
return somma;
}
//
// Classe ListaNumeri
//
int ListaNumeri::inserisci(int n)
{
#ifndef ZERO_OP
if (n == 0) return 0;
#endif
if (n % 2)
liste[DISPARI].inserisci(n);
else
liste[PARI].inserisci(n);
return 1;
}
inline int ListaNumeri::estrai(tipo_numero tn, int& num_estratto)
{
return liste[tn].estrai(num_estratto);
}
// Restituisce il numero di elementi contenuti in una determinata sottolista.
inline int ListaNumeri::operator%(tipo_numero tn) const
{
return !(liste[tn]);
}
// Restituisce la somma dei valori degli elementi contenuti in una
// delle due sottoliste.
inline int ListaNumeri::operator[](tipo_numero tn) const
{
return liste[tn].somma();
}
// Restituisce il numero totale di elementi contenuti nella lista di numeri.
inline int ListaNumeri::operator!() const
{
return ((!liste[PARI]) + (!liste[DISPARI]));
}
ostream& operator<<(ostream& os, const ListaNumeri& src)
{
os << '<' << src % PARI << ", " << src % DISPARI << '>';
return os;
}
30/01/2024, 11:52
ncant ha scritto:Qualche consiglio/opinione in merito?
ListaNumeri
da te ideata.
30/01/2024, 12:25
Lista() : testa(nullptr) { }
Elem* testa = nullptr;
// BEFORE
elem* temp = new elem;
temp->num = n;
temp->prox = NULL;
// AFTER
elem* temp = new elem{ n, nullptr };
if (!lista) {
// ...
}
liste[n % 2].inserisci(n);
if (condizione) {
// resto della funzione
}
if (!condizione) {
return;
}
// resto della funzione
#pragma once
#include <iostream>
#include <optional>
//
// Classe Lista
//
class Lista {
struct Elem {
int num;
Elem* prox;
};
Elem* testa = nullptr;
void copia(const Lista&);
void elimina();
public:
Lista() = default;
Lista(const Lista& src) { copia(src); }
virtual ~Lista() { elimina(); }
Lista& operator=(const Lista&);
void inserisci(int);
std::optional<int> estrai();
int somma() const;
int lunghezza() const;
};
inline void Lista::copia(const Lista& src)
{
// Niente da fare se copiamo noi stessi
if (this == &src) {
return;
}
// Rimuoviamo il contenuto corrente della lista
elimina();
// Niente da fare se stiamo copiando una lista vuota
if (src.testa == nullptr) {
return;
}
// Crea il nodo di testa
testa = new Elem{ src.testa->num, nullptr };
// Copia il resto della lista
Elem* temp = testa;
Elem* aux = src.testa;
while (temp != nullptr) {
temp->prox = new Elem{ aux->num, nullptr };
temp = temp->prox;
aux = aux->prox;
}
}
inline void Lista::elimina()
{
Elem* temp = testa;
while (temp != nullptr) {
testa = testa->prox;
delete temp;
temp = testa;
}
}
inline Lista& Lista::operator=(const Lista& src)
{
copia(src);
return *this;
}
// Inserzione in coda.
inline void Lista::inserisci(int n)
{
Elem* temp = new Elem{ n, nullptr };
if (testa == nullptr) {
testa = temp;
} else {
Elem* ultimo = testa;
while (ultimo->prox != nullptr) {
ultimo = ultimo->prox;
}
ultimo->prox = temp;
}
}
// Estrazione in testa.
inline std::optional<int> Lista::estrai()
{
if (testa == nullptr) {
return {};
}
Elem* temp = testa;
testa = testa->prox;
int num_estratto = temp->num;
delete temp;
return { num_estratto };
}
// Restituisce il numero di elementi presenti in una lista.
inline int Lista::lunghezza() const
{
int conta = 0;
for (Elem* temp = testa; temp != nullptr; temp = temp->prox) {
conta++;
}
return conta;
}
// Restituisce la somma di tutti i valori memorizzati nella lista.
inline int Lista::somma() const
{
int somma = 0;
Elem* aux = testa;
while (aux != nullptr) {
somma += aux->num;
aux = aux->prox;
}
return somma;
}
//
// Classe ListaNumeri
//
class ListaNumeri {
// Crea due sottoliste: una per i numeri pari, una per i numeri dispari.
Lista liste[2];
public:
enum TipoNumero { PARI, DISPARI };
ListaNumeri() {}
ListaNumeri(const ListaNumeri& src)
: liste{ src.liste[0], src.liste[1] }
{}
ListaNumeri& operator=(const ListaNumeri& src)
{
liste[0] = src.liste[0];
liste[1] = src.liste[1];
return *this;
}
void inserisci(int);
std::optional<int> estrai(TipoNumero);
int lunghezza() const;
int lunghezza(TipoNumero) const;
int somma(TipoNumero) const;
};
void ListaNumeri::inserisci(int n)
{
liste[n % 2].inserisci(n);
}
inline std::optional<int> ListaNumeri::estrai(TipoNumero tn)
{
return liste[tn].estrai();
}
// Restituisce il numero di elementi contenuti in una determinata sottolista.
inline int ListaNumeri::lunghezza(TipoNumero tn) const
{
return liste[tn].lunghezza();
}
// Restituisce la somma dei valori degli elementi contenuti in una
// delle due sottoliste.
inline int ListaNumeri::somma(TipoNumero tn) const
{
return liste[tn].somma();
}
// Restituisce il numero totale di elementi contenuti nella lista di numeri.
inline int ListaNumeri::lunghezza() const
{
return liste[PARI].lunghezza() + liste[DISPARI].lunghezza();
}
std::ostream& operator<<(std::ostream& os, const ListaNumeri& src)
{
os << '<' << src.lunghezza(ListaNumeri::PARI) << ", " << src.lunghezza(ListaNumeri::DISPARI) << '>';
return os;
}
#include <iostream>
#include "listaNumeri.hpp"
int main(int argc, char* argv[])
{
ListaNumeri lista;
lista.inserisci(10);
lista.inserisci(12);
lista.inserisci(15);
lista.inserisci(1);
lista.inserisci(34);
std::cout << "EXPECTED: <3, 2>\n";
std::cout << "GOT: " << lista << "\n";
std::cout << "EXPECTED: 56, 16\n";
std::cout << "EXPECTED: " << lista.somma(ListaNumeri::PARI) << ", " << lista.somma(ListaNumeri::DISPARI) << "\n";
lista.estrai(ListaNumeri::DISPARI);
std::cout << "EXPECTED: <3, 1>\n";
std::cout << "GOT: " << lista << "\n";
std::cout << "EXPECTED: 56, 1\n";
std::cout << "EXPECTED: " << lista.somma(ListaNumeri::PARI) << ", " << lista.somma(ListaNumeri::DISPARI) << "\n";
lista.estrai(ListaNumeri::PARI);
lista.estrai(ListaNumeri::PARI);
lista.inserisci(4);
lista.inserisci(3);
std::cout << "EXPECTED: <2, 2>\n";
std::cout << "GOT: " << lista << "\n";
std::cout << "EXPECTED: 38, 4\n";
std::cout << "EXPECTED: " << lista.somma(ListaNumeri::PARI) << ", " << lista.somma(ListaNumeri::DISPARI) << "\n";
}
30/01/2024, 12:36
05/02/2024, 16:23
06/02/2024, 10:57
Skuola.net News è una testata giornalistica iscritta al Registro degli Operatori della Comunicazione.
Registrazione: n° 20792 del 23/12/2010.
©2000—
Skuola Network s.r.l. Tutti i diritti riservati. — P.I. 10404470014.
Powered by phpBB © phpBB Group - Privacy policy - Cookie privacy
phpBB Mobile / SEO by Artodia.