Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
vuoi
o PayPal
tutte le volte che vuoi
NOTA:
Gli smart pointer sono utilizzabili con C++ 14 in poi. Pertanto, se il codice
non è compilato con la versione corretta, abbiamo un errore
use of undeclared identifier 'make_unique'
Per compilare il codice con la versione corretta di C++ lanciare da terminale i
seguenti comandi (per Linux e macOS):
cd /path/to/your/code
g++ -std=c++14 NOME_FILE_SORGENTE.cpp -o NOME_FILE_OGGETTO
./NOME_FILE_OGGETTO
In questo caso:
g++ -std=c++14 2022-11-23-2.cpp -o 2022-11-23-2
./2022-11-23-2
*/
std::ifstream inputFile("vehicles.txt"); // NOTA: potrebbe essere necessario
specificare il percorso assoluto
if (!inputFile.is_open()) {
std::cerr << "Error: Could not open the file." << std::endl;
return 1;
}
std::vector< std::unique_ptr<Vehicle> > vehicles; // Usare spazio tra le
parentesi angolate per non confondere con lo stream >>
std::string type, model;
int seats, wheels, loading;
while (inputFile >> type) {
inputFile.ignore(); // Ignore the newline character after type
std::getline(inputFile, model);
if (type == "CAR" || type == "TRUCK") {
inputFile >> seats;
inputFile.ignore(); // Ignore the newline character after seats
}
if (type == "TRUCK") {
inputFile >> wheels >> loading;
inputFile.ignore(); // Ignore the newline character after loading
}
vehicles.push_back(createVehicle(type, model, seats, wheels, loading));
}
inputFile.close();
// Printing the vehicles
for (const auto& vehicle : vehicles) {
std::cout << vehicle->str() << std::endl;
}
return 0;
}
// 2022-11-23-3
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>
class Vehicle {
protected:
std::string model;
public:
Vehicle(const std::string& model) : model(model) {}
virtual ~Vehicle() {}
virtual int getWheels() const = 0;
virtual std::string str() const = 0;
};
class Motorcycle : public Vehicle {
public:
Motorcycle(const std::string& model) : Vehicle(model) {}
int getWheels() const override {
return 2;
}
std::string str() const override {
return "Motorcycle: " + model;
}
};
class Car : public Vehicle {
protected:
int seats;
public:
Car(const std::string& model, int seats) : Vehicle(model), seats(seats) {}
int getSeats() const {
return seats;
}
int getWheels() const override {
return 4; // Assuming all cars have 4 wheels
}
std::string str() const override {
return "Car: " + model;
}
};
class Truck : public Vehicle {
protected:
int wheels;
int loading;
public:
Truck(const std::string& model, int wheels, int loading)
: Vehicle(model), wheels(wheels), loading(loading) {}
int getLoading() const {
return loading;
}
int getWheels() const override {
return wheels;
}
std::string str() const override {
return "Truck: " + model;
}
};
std::unique_ptr<Vehicle> createVehicle(const std::string& type, const std::string&
model, int seats = 0, int wheels = 0, int loading = 0) {
if (type == "MOTORCYCLE") {
return std::make_unique<Motorcycle>(model);
} else if (type == "CAR") {
return std::make_unique<Car>(model, seats);
} else if (type == "TRUCK") {
return std::make_unique<Truck>(model, wheels, loading);
} else {
return nullptr;
}
}
void selectionSort(std::vector< std::unique_ptr<Vehicle> >& vehicles) { // Usare
spazio tra le parentesi angolate per non confondere con lo stream >>
int n = vehicles.size();
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (vehicles[j]->getWheels() < vehicles[minIndex]->getWheels()) {
minIndex = j;
}
}
if (minIndex != i) {
std::swap(vehicles[i], vehicles[minIndex]);
}
}
}
int main() {
/*
NOTA:
Gli smart pointer sono utilizzabili con C++ 14 in poi. Pertanto, se il codice
non è compilato con la versione corretta, abbiamo un errore
use of undeclared identifier 'make_unique'
Per compilare il codice con la versione corretta di C++ lanciare da terminale i
seguenti comandi (per Linux e macOS):
cd /path/to/your/code
g++ -std=c++14 NOME_FILE_SORGENTE.cpp -o NOME_FILE_OGGETTO
./NOME_FILE_OGGETTO
In questo caso:
g++ -std=c++14 2022-11-23-2.cpp -o 2022-11-23-2
./2022-11-23-2
*/
std::ifstream inputFile("vehicles.txt");
if (!inputFile.is_open()) {
std::cerr << "Error: Could not open the file." << std::endl;
return 1;
}
std::vector< std::unique_ptr<Vehicle> > vehicles; // Usare spazio tra le
parentesi angolate per non confondere con lo stream >>
std::string type, model;
int seats, wheels, loading;
while (inputFile >> type) {
inputFile.ignore(); // Ignore the newline character after type
std::getline(inputFile, model);
if (type == "CAR" || type == "TRUCK") {
inputFile >> seats;
inputFile.ignore(); // Ignore the newline character after seats
}
if (type == "TRUCK") {
inputFile >> wheels >> loading;
inputFile.ignore(); // Ignore the newline character after loading
}
vehicles.push_back(createVehicle(type, model, seats, wheels, loading));
}
inputFile.close();
// Sorting the vehicles based on the number of wheels
selectionSort(vehicles);
// Printing the vehicles
for (const auto& vehicle : vehicles) {
std::cout << vehicle->str() << std::endl;
}
return 0;
} Programmazione ad oggetti
Corso di Laurea in Ingegneria Informatica, Elettronica e delle Telecomunicazioni
Prova di programmazione
16 settembre 2022
Al fine di poter gestire dinamicamente un testo, quest’ultimo viene memorizzato in una struttura dinamica
di tipo lista collegata in cui i singoli elementi contengono le parole che formano il testo. Una parola è una
stringa contenenti caratteri di testo e punteggiatura. Nel testo le parole sono separate da spazi.
Esempio di lista concatenata che contiene il testo “Questo è un esempio di testo.”:
Si chiede di:
1) Creare una classe StringList che mantiene una sequenza ordinata di stringhe tramite lista collegata, senza
usare classi della STL. La classe StringList dovrà avere la seguente interfaccia pubblica (costruttore e metodi
pubblici): // costruttore
StringList() //aggiunge una parola in testa
push_front(const string& val) // aggiunge una parola in coda
push_back(const string& val)
// restituisce il numero di elementi presenti
int size() const // restituisce una stringa che concatena tutti le parole, separate da spazi
string str() const
Provare la classe StringList con il seguente programma (test1.cpp):
int main() {
using namespace std;
StringList phrase;
phrase.push_front("ipsum");
phrase.push_back("dolor");
phrase.push_back("sit");
phrase.push_back("amet");
phrase.push_front("Lorem");
cout << phrase.str() << endl;
cout << "size: " << phrase.size() << endl;
che dovrebbe dare la seguente uscita (output):
Lorem ipsum dolor sit amet.
size: 5
2) Aggiungere alla classe StringList il metodo:
void remove(const string& val)
Modificare il programma di test aggiungendo le seguenti righe:
phrase.remove("Lorem");
phrase.remove("dolor");
phrase.remove("sit");
phrase.remove("amet.");
phrase.push_front("Lorem");
cout << phrase.str() << endl;
Che dovrebbe così produrre la seguente uscita:
Lorem ipsum dolor sit amet.
size: 5
Lorem ipsum
3) Scrivere un programma che inserisce in una StringList un testo letto da file (“testo.txt”) e lo stampa su
schermo.
// 2022-09-16-1
#include <iostream>
#include <string>
class StringList {
private:
struct Node {
std::string data;
Node* next;
Node(const std::string& data) : data(data), next(nullptr) {}
};
Node* head;
Node* tail;
int length;
public:
StringList() : head(nullptr), tail(nullptr), length(0) {}
void push_front(const std::string& val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
newNode->next = head;
head = newNode;
}
length++;
}
void push_back(const std::string& val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
length++;
}
int size() const {
return length;
}
std::string str() const {
std::string result;
Node* current = head;
while (current) {
result += current->data;
if (current->next) {
result += " ";
}
current = current->next;
}
return result;
}
~StringList() {
Node* current = head;
while (current) {
Node* temp = current;
current = current->next;
delete temp;
}
}
};
int main() {
using namespace std;
StringList phrase;
phrase.push_front("ipsum");
phrase.push_back("dolor");
phrase.push_back("sit");
phrase.push_back("amet");
phrase.push_front("Lorem");
cout << phrase.str() << endl;
cout << "size: " << phrase.size() << endl;
return 0;
}
// 2022-09-16-2
#include <iostream>
#include <string>
class StringList {
private:
struct Node {
std::string data;
Node* next;
Node(const std::string& data) : data(data), next(nullptr) {}
};
Node* head;
Node* tail;
int length;
public:
StringList() : head(nullptr), tail(nullptr), length(0) {}
void push_front(const std::string& val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
newNode->next = head;
head = newNode;
}
length++;
}
void push_back(const std::string& val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
length++;
}
void remove(const std::string& val) {
Node* current = head;
Node* prev = nullptr;
while (current) {
if (current->data == val) {
if (prev) {
prev->next = current->next;
if (current == tail) {
tail = prev;
}
} else {
head = current->next;
}
Node* temp = current;