Aggiunti esempi del professore

This commit is contained in:
Alessandro Ferro 2021-03-21 00:41:19 +01:00 committed by GitHub
parent be83aad44b
commit 7688efdf80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 735 additions and 0 deletions

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,48 @@
#include <iostream>
using namespace std;
int main() {
try {
ulong* ulptr = new ulong; // Uninitialized unsigned long
// ulong* ulptr = new ulong(5); // new ulong{5} // Unsigned long initialized to 5
cout << (*ulptr)++ << endl;
cout << *ulptr << endl;
delete ulptr; // ulptr = nullptr;
// delete ulptr; // Double free cannot be catched!
} catch (bad_alloc exc) {
cout << "Quite rare exception!" << endl;
}
try {
const ulong arraysize = 3; // 10000000000
ulong* ulptr = new ulong[arraysize]; // Uninitialized array of three unsigned longs
// ulong* ulptr = new ulong[arraysize]{}; // array of three unsigned longs initialized to the default value (0)
// ulong* ulptr = new ulong[arraysize]{5, 6, 7}; // array of three unsigned longs initialized to 5, 6, and 7.
cout << ulptr[0]++ << endl;
cout << ulptr[1] << endl;
cout << ulptr[2]-- << endl << endl;
cout << ulptr[0] << endl;
cout << ulptr[1] << endl;
cout << ulptr[2] << endl;
delete[] ulptr; // ulptr = nullptr;
// delete[] ulptr; // Double free cannot be catched!
} catch (bad_alloc exc) {
cout << "Quite rare exception!" << endl;
}
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,131 @@
#include <cmath>
#include <iostream>
using namespace std;
int f() { return 0; };
string g() { return string("This is a very long long long long long long string..."); }
string h() { return string("Another quite long long long long long long string..."); }
int main() {
// Slide 1
bool bvar = true; // false
cout << "Boolean variable: " << bvar << "; " << !bvar << endl;
char cvar1 = 'A'; // 126
// char cvar1 = 126;
unsigned char cvar2 = 'Z'; // 126
cout << "Char variable: " << cvar1 << "; " << endl;
cout << "Unsigned char variable: " << cvar2 << "; " << endl;
// short svar1 = 'A';
short svar1 = -120; // 'A'
unsigned short svar2 = -120; // 'A'
cout << "Short variable: " << svar1 << "; " << endl;
cout << "Unsigned short variable: " << svar2 << "; " << endl;
// Other basic types: [unsiogned] int, long (long int), long long (long long
// int), float, double, long double. Synonyms for some compilers (e.g., gnu):
// uint for unsigned int and ulong for unsigned long.
uint uivar1 = pow(2, 32) - 120;
uint uivar2 = -120;
ulong ulvar1 = pow(2, 32) - 120;
ulong ulvar2 = -120;
cout << "Unsigned int variable: " << uivar1 << " == " << uivar2 << "; " << endl;
cout << "Unsigned long variable: " << ulvar1 << " != " << ulvar2 << "; " << endl;
float vecvar[3] = { 0.1, 2, 5.7 };
// double vecvar[3] = { 0.1, 2, 5.7 };
cout << "Double vector variable: ";
for (uint i = 0; i < 3; i++) { cout << vecvar[i] << ' '; };
cout << endl;
long matvar[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
cout << "Long matrix variable: " << endl;
for (uint i = 0; i < 3; i++) { for (uint j = 0; j < 2; j++) { cout << matvar[i][j] << ' '; }; cout << endl; };
const double pi = 3.141592;
// pi = 3.141592;
cout << "Double constant: " << pi << "; " << endl;
// // Slide 2
//
// char cvar = 'a';
// char* cpvar = &cvar;
// cout << "Char variable and its pointer: '" << cvar << "' == '" << *cpvar << "'; " << endl;
// cvar++;
// cout << "Char variable and its pointer: '" << cvar << "' == '" << *cpvar << "'; " << endl;
//
// int ivar = 'a';
// int* ipvar = &ivar;
// cout << "Int variable and its pointer: " << ivar << " == " << *ipvar << " != " << ipvar << "; " << endl;
// ivar++;
// cout << "Int variable and its pointer: " << ivar << " == " << *ipvar << " != " << ipvar << "; " << endl;
//
// const char ccon = 'a';
// const char* cpcon = &ccon;
// // char* cpcon = &ccon;
// // const char* const cpcon = &ccon;
// cout << "Char constant and its pointer: '" << ccon << "' == '" << *cpcon << "'; " << endl;
// // ccon++;
// cpcon++;
// cout << "Char constant and its pointer: '" << ccon << "' (possibly)!= '" << *cpcon << "'; " << endl;
// cpcon--;
// cout << "Char constant and its pointer: '" << ccon << "' == '" << *cpcon << "'; " << endl;
//
// char cnew = 'z';
// void* vptr = &cnew;
// cout << "Char constant and its pointer: '" << cvar << "' == '" << *cpvar << "'; " << endl;
// // cvar = *vptr;
// // cvar = *((char*) vptr); // C-like cast
// cvar = *(static_cast<char*>(vptr)); // C++ static cast
// cout << "Char constant and its pointer: '" << cvar << "' == '" << *cpvar << "'; " << endl;
// // cout << *vptr << ";" << endl;
//
// uint uivar = 25;
// cout << "Unsigned integer variable: " << uivar << "; " << endl;
// uivar = *((uint*) vptr); // C-like cast
// // uivar = *(static_cast<uint*>(vptr)); // C++ static cast
// // uivar = *((uint*) cpcon); // C-like cast
// // uivar = *(static_cast<uint*>(cpcon)); // C++ static cast
// cout << "Unsigned integer variable: " << uivar << "; " << endl;
// // Slide 3
//
// int ivarx = 0;
// int& irvarx = ivarx;
// cout << "Int variable and its reference: " << ivarx << " == " << irvarx << "; " << endl;
// irvarx++;
// cout << "Int variable and its reference: " << ivarx << " == " << irvarx << "; " << endl;
//
// //int& ivary = 0;
// const int& ivary = 0;
//
// int ivarz = f();
// // int& ivarz = f();
// // Slide 4
//
// string stringa1 = g(); // Copia del valore.
// // string& stringa1 = g(); // Riferimento lvalue a un rvalue (valore temporaneo). Errato!
// // string&& stringa1 = g(); // Riferimento rvalue.
// // string stringa1 = std::move(g()); // Spostamento del valore.
//
// cout << "1) " << stringa1 << endl;
//
// // string&& stringa2 = h();
// //
// // cout << "2) " << stringa2 << endl;
// //
// // stringa1 = std::move(stringa2);
// //
// // cout << "3) " << stringa1 << endl;
// // cout << "4) " << stringa2 << endl;
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,90 @@
#include <iostream>
// #include <stdexcept> // Optional when iostream is included!
using namespace std;
int main() {
cout << "Hello everyone... I am rising some exception!" << endl;
try {
// throw logic_error("Some logic error.");
// throw length_error("A lenght error occurred!");
// throw out_of_range("An out-of-range access to some structure occurred!");
// throw runtime_error("Some runtime error.");
// throw overflow_error("An overflow occurred!");
// throw underflow_error("An underflow occurred!");
// throw bad_alloc(); // Just an example, do not throw thsi exception.
// throw 25;
} catch (length_error& exc) {
cout << "Length error: " << exc.what() << endl;
// throw;
} catch (logic_error& exc) {
cout << "Logic error: " << exc.what() << endl;
} catch (overflow_error& exc) {
cout << "Overflow error: " << exc.what() << endl;
} catch (runtime_error& exc) {
cout << "Runtime error: " << exc.what() << endl;
} catch (bad_alloc& exc) {
cout << "Bad allocation: " << exc.what() << endl;
} catch (exception& exc) {
cout << "All unmanaged standard exceptions reach this point!" << exc.what() << endl;
}
catch (...) {
cout << "All unmanaged non-standard exceptions reach this point!" << endl;
}
// try {
//
// try {
//
// throw logic_error("Some logic error.");
//
// } catch (logic_error& exc) {
//
// cout << "I am unable to manage this exception: " << exc.what() << endl;
//
// throw;
//
// }
//
// } catch (...) {
//
// cout << "All unmanaged exceptions reach this point!" << endl;
//
// }
// try {
//
// throw 5;
//
// } catch (int num) {
//
// cout << "Exception number: " << num;
//
// }
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,130 @@
#include <iostream>
using namespace std;
void quicksort(int*, uint);
void quicksort(int*, uint, uint);
uint partition(int*, uint, uint);
enum class ComparisonType { LessThan, Equal, GreaterThen};
// // Function pointer a la C
// typedef ComparisonType (*CompareFunction) (int, int);
//
// // // Function pointer a la C++
// // #include <functional>
// // typedef function<ComparisonType(int, int)> CompareFunction;
//
// void quicksort(int*, uint, CompareFunction);
// void quicksort(int*, uint, uint, CompareFunction);
// uint partition(int*, uint, uint, CompareFunction);
//
// ComparisonType OrdA(int a, int b) {
// if (a < b) {
// return ComparisonType::LessThan;
// } else if (a > b) {
// return ComparisonType::GreaterThen;
// }
// return ComparisonType::Equal;
// }
//
// ComparisonType OrdB(int a, int b) {
// if (a < b) {
// return ComparisonType::GreaterThen;
// } else if (a > b) {
// return ComparisonType::LessThan;
// }
// return ComparisonType::Equal;
// }
//
// ComparisonType OrdC(int a, int b) {
// if (a % 2 != b % 2) {
// return ((a % 2 == 0) ? ComparisonType::LessThan : ComparisonType::GreaterThen);
// } else if (a < b) {
// return ComparisonType::LessThan;
// } else if (a > b) {
// return ComparisonType::GreaterThen;
// }
// return ComparisonType::Equal;
// }
int main() {
int A[11] = {5, 7, 6, 8, 4, 9, 3, 10, 2, 0, 1};
quicksort(A, 11);
// quicksort(A, 11, OrdA);
// // quicksort(A, 11, OrdB);
// // // quicksort(A, 11, OrdC);
for (uint i = 0; i < 11; i++) { cout << A[i] << ' '; }; cout << endl;
return 0;
}
void quicksort(int* A, uint size) {
quicksort(A, 0, size - 1);
}
void quicksort(int* A, uint p, uint r) {
if (p < r) {
uint q = partition(A, p, r);
quicksort(A, p, q);
quicksort(A, q + 1, r);
}
}
uint partition(int* A, uint p, uint r) {
int x = A[p];
int i = p - 1;
int j = r + 1;
do {
do { j--; } while ( x < A[j] );
do { i++; } while ( A[i] < x );
if (i < j) { swap(A[i], A[j]); } // "swap" is standard-library function
} while (i < j);
return j;
}
// void quicksort(int* A, uint size, CompareFunction cmp) {
// quicksort(A, 0, size - 1, cmp);
// }
//
// void quicksort(int* A, uint p, uint r, CompareFunction cmp) {
// if (p < r) {
// uint q = partition(A, p, r, cmp);
// quicksort(A, p, q, cmp);
// quicksort(A, q + 1, r, cmp);
// }
// }
//
// uint partition(int* A, uint p, uint r, CompareFunction cmp) {
//
// int x = A[p];
// int i = p - 1;
// int j = r + 1;
//
// do {
//
// do { j--; } while ( cmp(x, A[j]) == ComparisonType::LessThan );
//
// do { i++; } while ( cmp(x, A[i]) == ComparisonType::GreaterThen );
//
// if (i < j) { swap(A[i], A[j]); } // "swap" is standard-library function
//
// } while (i < j);
//
// return j;
//
// }

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,91 @@
#include <iostream>
using namespace std;
struct Studente {
ulong Id = 0;
string Matricola = "N86000000";
string Cognome = "";
string Nome = "";
// friend ostream& operator<<(ostream& outstr, const Studente& stu);
// friend istream& operator>>(istream& instr, Studente& stu);
// private:
// ulong SecureNum = 0x25242310;
};
ostream& operator<<(ostream& outstr, const Studente& stu) {
outstr << "Id: " << stu.Id << "; Matricola: " << stu.Matricola //
<< "; Cognome: " << stu.Cognome << "; Nome: " << stu.Nome;
// outstr << "; Numero di sicurezza: " << stu.SecureNum;
return outstr;
}
istream& operator>>(istream& instr, Studente& stu) {
instr >> stu.Id >> stu.Matricola >> stu.Cognome >> stu.Nome;
// instr >> stu.SecureNum;
return instr;
}
enum class Color { Giallo, Verde, Bianco, Rosso };
ostream& operator<<(ostream& outstr, const Color& clr) {
switch(clr) {
case Color::Verde:
outstr << "Green";
break;
case Color::Bianco:
outstr << "White";
break;
case Color::Rosso:
outstr << "Red";
break;
default:
outstr << "Color{ " << int(clr) << " }";
}
return outstr;
}
int main() {
Studente stu1{1, "N86000001", "Alan", "Turing"};
cout << "Id: " << stu1.Id << endl;
cout << "Matricola: " << stu1.Matricola << endl;
cout << "Cognome: " << stu1.Cognome << endl;
cout << "Nome: " << stu1.Nome << endl;
Studente stu2{2, "N86000002", "Gödel", "Kurt"};
cout << "Id: " << stu2.Id << endl;
cout << "Matricola: " << stu2.Matricola << endl;
cout << "Cognome: " << stu2.Cognome << endl;
cout << "Nome: " << stu2.Nome << endl;
// cout << stu1 << endl;
// cout << stu2 << endl;
// Studente stu3;
// cin >> stu3;
// cout << stu3 << endl;
// Studente stu;
// cout << stu << endl;
// cin >> stu;
// cout << stu << endl;
// Color clr = Color::Bianco;
// cout << clr << endl;
// clr = Color::Giallo;
// cout << clr << endl;
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,9 @@
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,26 @@
#include <iostream>
// Random generation a la C
#include <stdlib.h>
#include <time.h>
// // Random generation a la C++
// #include <random>
using namespace std;
int main() {
// Random generation a la C
srand(time(NULL));
for(uint i = 0; i < 15; i++) { cout << rand() << " "; }; cout << endl;
// for(uint i = 0; i < 15; i++) { cout << rand() % 25 << " "; }; cout << endl;
// // Random generation a la C++
// default_random_engine gen(random_device{}());
// uniform_int_distribution<uint> dist(7, 35);
// for(uint i = 0; i < 15; i++) { cout << dist(gen) << " "; }; cout << endl;
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,49 @@
#include <iostream>
// #include <string> // Optional when iostream is included!
using namespace std;
int main() {
string Stringa1 = "Alan Turing";
// string Stringa1 = {"Alan Turing"};
// string Stringa1{"Alan Turing"};
// string Stringa1("Alan Turing");
string Stringa2 = "Kurt Godel";
cout << Stringa1 << " - " << Stringa2 << endl;
cout << "Lexicographic comparison (String1 < String2): " << (Stringa1 < Stringa2) << endl;
cout << "Lexicographic comparison (String1 <= String2): " << (Stringa1 <= Stringa2) << endl;
cout << "Lexicographic comparison (String1 == String2): " << (Stringa1 == Stringa2) << endl;
cout << "Lexicographic comparison (String1 != String2): " << (Stringa1 != Stringa2) << endl;
cout << "Lexicographic comparison (String1 >= String2): " << (Stringa1 >= Stringa2) << endl;
cout << "Lexicographic comparison (String1 > String2): " << (Stringa1 > Stringa2) << endl;
// cin >> Stringa2;
// // getline (cin, Stringa2);
//
// cout << Stringa1 << " - " << Stringa2 << endl;
// cout << "Lexicographic comparison (String1 < String2): " << (Stringa1 < Stringa2) << endl;
// cout << "Lexicographic comparison (String1 <= String2): " << (Stringa1 <= Stringa2) << endl;
// cout << "Lexicographic comparison (String1 == String2): " << (Stringa1 == Stringa2) << endl;
// cout << "Lexicographic comparison (String1 != String2): " << (Stringa1 != Stringa2) << endl;
// cout << "Lexicographic comparison (String1 >= String2): " << (Stringa1 >= Stringa2) << endl;
// cout << "Lexicographic comparison (String1 > String2): " << (Stringa1 > Stringa2) << endl;
// // Stringa1.clear();
//
// cout << "Size: " << Stringa1.size() << endl;
// cout << "String \"" << Stringa1 << "\" is " << (Stringa1.empty() ? "" : "not ") << "empty" << endl;
// uint i = 3; cout << "Char " << i << " of string" << Stringa1 << " is \'" << Stringa1[i] << "\'"<< endl;
// cout << "String front is \'" << Stringa1.front() << "\'"<< endl;
// cout << "String back is \'" << Stringa1.back() << "\'"<< endl;
//
// cout << (Stringa1 + " $ " + Stringa2) << endl;
//
// i = 1; uint j = 2; cout << "Subtring from " << i << " to " << j << " of \"" << Stringa1 << "\" is \"" << Stringa1.substr(i, j) << "\"" << endl;
return 0;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main test.cpp main.cpp

View File

@ -0,0 +1,17 @@
#include <iostream>
#include "test.hpp"
// #include "testx.hpp"
using namespace std;
// void testx() {}; // cout << "";
int main() {
cout << "Call to a function: #";
test();
// testx();
cout << "# Return from a function call!" << endl;
return 0;
}

View File

@ -0,0 +1,6 @@
#include <iostream>
void test() {
std::cout << "Hello world!";
}

View File

@ -0,0 +1,9 @@
#ifndef TEST_HPP
#define TEST_HPP
void test();
// const int a = 1;
#endif

View File

@ -0,0 +1,7 @@
#ifndef TESTX_HPP
#define TESTX_HPP
void testx() {}; // cout << "";
#endif

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,82 @@
#include <iostream>
using namespace std;
struct Studente {
ulong Id = 0;
string Matricola = "N86000000";
string Cognome = "";
string Nome = "";
// Studente() = default;
// Studente(ulong idx, string mat, string cog, string nom) {
// Id = idx; Matricola = mat; Cognome = cog; Nome = nom;
// // Id++; Matricola += "XYZ";
// }
};
// enum class Colore { Bianco, Grigio, Nero }; // Enumeration a la C++
// enum class Colore1 { Bianco, Rosso };
//
// enum Colore2 { Rosso, Giallo, Verde }; // Enumeration a la C
// enum Colore3 { Marrone, Azzurro };
// // enum Colore3 { Rosso, Marrone }; // Error: duplicated value!
int main() {
// Slide 1
Studente stu;
cout << "Id: " << stu.Id << endl;
cout << "Matricola: " << stu.Matricola << endl;
cout << "Cognome: " << stu.Cognome << endl;
cout << "Nome: " << stu.Nome << endl;
stu.Id = 1;
stu.Matricola = "N86000001";
stu.Cognome = "Turing";
stu.Nome = "Alan";
cout << "Id: " << stu.Id << endl;
cout << "Matricola: " << stu.Matricola << endl;
cout << "Cognome: " << stu.Cognome << endl;
cout << "Nome: " << stu.Nome << endl;
// Studente stu1{2, "N86000002", "Gödel"};
// // Studente stu1 = {2, "N86000002", "Gödel", "Kurt"}; // Equivalente definition
//
// cout << "Id: " << stu1.Id << endl;
// cout << "Matricola: " << stu1.Matricola << endl;
// cout << "Cognome: " << stu1.Cognome << endl;
// cout << "Nome: " << stu1.Nome << endl;
// Studente stu2(3, "N86000003", "Church", "Alonzo");
//
// cout << "Id: " << stu2.Id << endl;
// cout << "Matricola: " << stu2.Matricola << endl;
// cout << "Cognome: " << stu2.Cognome << endl;
// cout << "Nome: " << stu2.Nome << endl;
// // Slide 2
//
// Colore color = Colore::Grigio;
//
// cout << (color < Colore::Nero) << endl;
// cout << (color == Colore::Bianco) << endl;
// // cout << (Colore::Bianco == Colore1::Bianco) << endl;
//
// Colore1 color1 = Colore1::Rosso;
// // Colore1 color1 = Colore::Bianco;
//
// Colore2 color2 = Giallo;
// // Colore2 color2 = Azzurro;
return 0;
}