Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
esercizi lezione 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Nov 9, 2021
1 parent 39a3a5e commit b2fe1ae
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 0 deletions.
52 changes: 52 additions & 0 deletions esercitazioni/bisezione_iterativa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <cmath>
#include <limits>

using namespace std;

const double precision = numeric_limits<double>::epsilon();

// calcola la radice nell'intervallo [a, b], se esiste,
// della funzione f(x) definita a tempo di scrittura
// -1 se non sono presenti radici nell'intervallo
double bisezione(double a, double b, double epsilon);

// definisci sotto
double f(double x);

int main() {
// cout << precision; // DEBUGGING
double a, b;
double res;

cout << "Precisione (minore possibile): " << precision << endl;
do {
cout << "Inserisci a e b: "; cin >> a >> b;
res = bisezione(a, b, precision);
} while (res == -1.0);

cout << "La radice di f(x) nell'intervallo [" << a << ", " << b << "] è: " << res << endl;
}

// cambia il corpo di questa funzione
double f(double x) {
// return exp(x) - x*x - 2.0;
return exp(x);
}

double bisezione(double a, double b, double epsilon) {
if (f(a)*f(b) > 0.0)
return -1;

double c, f_c;
while (fabs(a-b) > epsilon) {
c = (a+b)/2.0; f_c = f(c);
if (f_c >= 0.0-epsilon && f_c <= 0.0+epsilon)
return c;
if (f_c*f(a) < 0.0)
b = c;
else if (f_c*f(b) < 0.0)
a = c;
}
return -1;
}
13 changes: 13 additions & 0 deletions esercitazioni/codice_car2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>

using namespace std;

int main() {
int cod;
cin >> cod;

cod = cod < int(' ') ? ' ' : cod;
cod = cod > int('~') ? '~' : cod;
cout << static_cast<char>(cod) << endl;
return 0;
}
15 changes: 15 additions & 0 deletions esercitazioni/div_reale3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
int a, b;
double a_real;

cout << "[a/b] -> inserisci a: "; cin >> a;
cout << "[a/b] -> inserisci b: "; cin >> b;
a_real = a;
cout << setprecision(15) << a_real/b << endl;
return 0;
}
61 changes: 61 additions & 0 deletions esercitazioni/funzioni_car.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <ctype.h>

using namespace std;

const char MIN_CHAR = ' ';
const char MAX_CHAR = '~';

bool alfanum(char c) ;
bool alfabetico(char c) ;
bool cifra_dec(char c) ;
bool cifra_esadec(char c) ;
bool minuscolo(char c) ;
bool maiuscolo(char c) ;

int main() {
char c;
cin >> c;

cout << "alfanum alfabetico dec hex minusc maiusc" << endl;

cout << alfanum(c) << " ";
cout << alfabetico(c) << " ";
cout << cifra_dec(c) << " ";
cout << cifra_esadec(c) << " ";
cout << minuscolo(c) << " ";
cout << maiuscolo(c) << " ";
cout << endl;

cout << (bool)isalnum(c) << " ";
cout << (bool)isalpha(c) << " ";
cout << (bool)isdigit(c) << " ";
cout << (bool)isxdigit(c) << " ";
cout << (bool)islower(c) << " ";
cout << (bool)isupper(c) << " ";
cout << endl;
}

bool alfanum(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}

bool alfabetico(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

bool cifra_dec(char c) {
return (c >= '0' && c <= '9');
}

bool cifra_esadec(char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}

bool minuscolo(char c) {
return (c >= 'a' && c <= 'z');
}

bool maiuscolo(char c) {
return (c >= 'A' && c <= 'Z');
}
46 changes: 46 additions & 0 deletions esercitazioni/funzioni_reali.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <cmath>

using namespace std;

double val_assoluto(double);
double arrotonda_per_eccesso(double);
double arrotonda_per_difetto(double);

int main() {
double n;
cin >> n;

cout << "fabs ceil floor" << endl;
cout << fabs(n) << " ";
cout << ceil(n) << " ";
cout << floor(n) << " ";
cout << endl;

cout << val_assoluto(n) << " ";
cout << arrotonda_per_eccesso(n) << " ";
cout << arrotonda_per_difetto(n) << " ";
cout << endl;
}

double val_assoluto(double n) {
return n < 0 ? -1*n : n;
}

double arrotonda_per_eccesso(double n) {
if (val_assoluto(n)-static_cast<int>(val_assoluto(n)) == 0)
return n;

return n > 0 ?
static_cast<int>(n)+1
: static_cast<int>(n);
}

double arrotonda_per_difetto(double n) {
if (val_assoluto(n)-static_cast<int>(val_assoluto(n)) == 0)
return n;

return n > 0 ?
static_cast<int>(n)
: static_cast<int>(n)-1;
}
15 changes: 15 additions & 0 deletions esercitazioni/int_reale_int.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
int a;
cin >> a;

float b = float(a);

int c = int(b);

cout << setprecision(20) << a << " " << b << " " << c << endl;
}
24 changes: 24 additions & 0 deletions esercitazioni/numero_n_cifre.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>

using namespace std;

// return random number in range [min, max]
int rand_in_range(int min, int max);

int main() {
int num_cifre;
cin >> num_cifre;

int max_num = pow(10, num_cifre);
// cout << max_num << endl;

srand(time(NULL));
cout << rand_in_range(1, max_num);
}

int rand_in_range(int min, int max) {
return rand()%(max-min) + min;
}
32 changes: 32 additions & 0 deletions esercitazioni/numero_razionale.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>

using namespace std;

int main() {
double num;
cin >> num;

int d = 1; // denominatore
double n = num; // numeratore
while (n-static_cast<int>(n) != 0){
d *= 10;
n *= 10;
}

cout << n << " " << d << endl;

// algoritmo di Euclide
int a = d, b = n;
int mod;
while (b) {
mod = a%b;
a = b;
b = mod;
}

cout << a << endl;

n /= a; d /= a;

cout << num << " = " << n << "/" << d << endl;
}

0 comments on commit b2fe1ae

Please sign in to comment.