domenica 12 febbraio 2017

COMPITO 9 ( PICCIALLI , CHIANESE)

COMPITO 9 SCAMBIO ARREY MATRICE
#include<iostream>
#include<fstream>
#include<string>

#define MAX 100

using namespace std;

typedef char cstring[256];

void apriFile(ifstream &file);
void caricaFile(ifstream &file, string **mat, int &righe, int &colonne);
void stampa_lung_uguale(string **mat, int righe, int colonne);
void ordinaMatrice(string **mat, int righe, int colonne);
void stampaMatrice(string **mat, int righe, int colonne);
void scambiaArray(string*, string*, int);

int main(){
ifstream file;
string **mat;
int righe, colonne;

mat = new string*[MAX];
for (int i = 0; i < MAX; i++){
mat[i] = new string[MAX];
}

apriFile(file);
caricaFile(file, mat, righe, colonne);
ordinaMatrice(mat, righe, colonne);
stampa_lung_uguale(mat, righe, colonne);
stampaMatrice(mat, righe, colonne);
system("PAUSE");

return 0;
}

void apriFile(ifstream &file){
string percorso;

cout << "Inserire il percorso del file: ";
do{
cin >> percorso;
file.open(percorso, ios::in);
if (!file)
cout << "Errore nell'apertura del file, inserire un nuovo percorso: ";
} while (!file);

return;
}

void caricaFile(ifstream &file, string **mat, int &righe, int &colonne){
cstring temp;
char c;
int riemp = 0;
int row = 0, col = 0;

while (!file.eof()){
file.get(c);
if (c == ' '){
temp[riemp] = '\0';
mat[row][col] = temp;
col++;
riemp = 0;
}
else if (c == '\n'){
temp[riemp] = '\0';
mat[row][col] = temp;
col=0;
row++;
riemp = 0;
}
else{
temp[riemp] = c;
riemp++;
}
colonne = col + 1;
righe = row + 1;
}

return;
}

void stampa_lung_uguale(string **mat, int righe, int colonne){
int max = 0;
for (int i = 0; i < righe; i++){
for (int j = 0; j < colonne; j++){
if (mat[i][j].length()>max)
max = mat[i][j].length();
}
}

cout << "Le stringhe di lunghezza massima ("<<max<<") sono in posizione:\n";

for (int i = 0; i < righe; i++){
for (int j = 0; j < colonne; j++){
if (mat[i][j].length() == max)
cout << "[" << i << ", " << j << "]: "<<mat[i][j]<<"\n";
}
}
}

void ordinaMatrice(string **mat, int righe, int colonne){
int somma1, somma2;
int scambi;

do{
scambi = 0;
for (int i = 0; i < righe - 1; i++){
somma1 = 0;
somma2 = 0;
for (int j = 0; j < colonne; j++){
somma1 = mat[i][j].length();
somma2 = mat[i+1][j].length();
}
if (somma1 > somma2){
scambiaArray(mat[i], mat[i + 1], colonne);
scambi++;
}
}
} while (scambi > 0);

}

void stampaMatrice(string **mat, int righe, int colonne){
for (int i = 0; i < righe; i++){
for (int j = 0; j < colonne; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}

void scambiaArray(string* vet1, string* vet2, int lung){
string temp;

for (int i = 0; i < lung; i++){
temp = vet1[i];
vet1[i] = vet2[i];
vet2[i] = temp;
}

}

COMPITO 8 (PICCIALLI , CHIANESE)

COMPITO 8
#include<iostream>
#include<fstream>
#include<string>

#define MAX 100

using namespace std;

void apriFile(ifstream &file);
void caricaFile(ifstream &file, int vet[], int &riemp);
void richiediSequenza(int *&sequenza, int &lung);
int verificaPresenza(int sequenza[], int fileVet[], int lung, int riemp);

int main(){
ifstream mioFile;
int *sequenza;
int fileVet[MAX];
int riemp, lung, trovato;

apriFile(mioFile);
caricaFile(mioFile, fileVet, riemp);
richiediSequenza(sequenza, lung);
trovato = verificaPresenza(sequenza, fileVet, lung, riemp);

if (trovato == 0)
cout << "Sequenza non trovata nel file!\n";
else
cout << "Sequenza trovata " << trovato << " volte nel file!\n";

system("PAUSE");

return 0;
}

void apriFile(ifstream &file){
string percorso;

cout << "Inserire il percorso del file: ";
do{
cin >> percorso;
file.open(percorso, ios::in);
if (!file)
cout << "Errore nell'apertura del file, inserire un nuovo percorso: ";
} while (!file);

return;
}

void caricaFile(ifstream &file, int vet[], int &riemp){
riemp = 0;

while (!file.eof()){
file>>vet[riemp];
riemp++;
}
return;
}

void richiediSequenza(int *&sequenza, int &lung){
cout << "Inserire la lunghezza della sequenza: ";
do{
cin >> lung;
if (lung < 2)
cout << "Valore non valido, inserire un numero maggiore di 1: ";
} while (lung < 2);

sequenza = new int[lung];

for (int i = 0; i < lung; i++){
cout << "Inserire l'elemento alla posizione " << i << ": ";
cin>>sequenza[i];
}
}

int verificaPresenza(int sequenza[], int fileVet[], int lung, int riemp){
int indice = 0;
bool ricerca = false;
int ricorrenze = 0;
int i = 0;
int primo = 0;
while(i<riemp){
if (fileVet[i] != sequenza[indice] && ricerca){
ricerca = false;
indice = 0;
}
if (fileVet[i] == sequenza[indice] && !ricerca){
ricerca = true;
indice++;
primo = i;
}
else if (fileVet[i] == sequenza[indice] && ricerca){
indice++;
if (indice == lung){
ricorrenze++;
ricerca = false;
indice = 0;
i = primo;
}
}
i++;
}

return ricorrenze;
}

COMPITO 7(PICCIALLI,CHIANESE)

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

bool apri_file(ifstream &file, string percorso);
bool apri_file(ofstream &file, string percorso);
void processa_file(ifstream &f1, ofstream &f2, ofstream &f3);

int main(){
ifstream file1;
ofstream file2, file3;
string f1, f2, f3;
f1 = "C:\\fondamenti\\compito7\\f1.txt";
f2 = "C:\\fondamenti\\compito7\\f2.txt";
f3 = "C:\\fondamenti\\compito7\\f3.txt";

while (!apri_file(file1, f1)){
cout << "Inserire un nuovo percorso: ";
cin >> f1;
}

while (!apri_file(file2, f2)){
cout << "Inserire un nuovo percorso: ";
cin >> f2;
}

while (!apri_file(file3, f3)){
cout << "Inserire un nuovo percorso: ";
cin >> f3;
}


processa_file(file1, file2, file3);

file1.close();
file2.close();
file3.close();

system("PAUSE");
return 0;
}

bool apri_file(ifstream &file, string percorso){
file.open(percorso, ios::in);
if (!file){
cout << "Errore nell'apertura del file!\n";
return false;
}
return true;
}

bool apri_file(ofstream &file, string percorso){
file.open(percorso, ios::out);
if (!file){
cout << "Errore nell'apertura del file!\n";
return false;
}
return true;
}


void processa_file(ifstream &f1, ofstream &f2, ofstream &f3){
int temp;
int zeroCount=0, maxPari, minDispari;
bool primoPari = true, primoDispari = true;
while (!f1.eof()){
f1 >> temp;
if (temp == 0){
zeroCount++;
}
   else if (temp % 2 == 1){
f2 << temp<<" ";
if (primoDispari){
minDispari = temp;
primoDispari = false;
}
else{
if (temp < minDispari)
minDispari = temp;
}
}
else{
f3 << temp<<" ";
if (primoPari){
maxPari = temp;
primoPari = false;
}
else{
if (temp > maxPari)
maxPari = temp;
}
}
}

cout << "Minimo: " << minDispari<<endl;

cout << "Massimo: " << maxPari<<endl;
cout << "Zeri: " << zeroCount << endl;
}

COMPITO 6( PICCIALLI , CHIANESE)

COMPITO 6 ORDINA MATRICE

MAIN.CPP:
#include<string>
#include<iostream>
#include"header.h"

using namespace std;

int main(){
string percorso;
int size;
int **mat;
bool errore;

cout << "Inserire percorso file: ";
cin >> percorso;

do{
//size = determina_size(percorso);
errore = carica_file(percorso, mat, size);
if (errore){
cout << "Inserire nuovo percorso: ";
cin >> percorso;
}
} while (errore);

/*mat = new int*[size];
for (int i = 0; i < size; i++)
mat[i] = new int[size];

carica_file(percorso, mat, size);*/

ordina_matrice(mat, size);

stampa_matrice(mat, size);

system("PAUSE");

return 0;
}
*********************************************************************************
HEADER.CPP:
#include"header.h"

using namespace std;

int determina_size(string percorso){
int temp;
ifstream mioFile;
int size = 0;

mioFile.open(percorso, ios::in);
if (!mioFile){
cout << "Errore nell'apertura del file!\n";
return -1;
}
while (!mioFile.eof()){
mioFile >> temp;
size++;
}
mioFile.close();

size = sqrt(size);

return size;
}

bool carica_file(string percorso, int **&mat, int &size){
ifstream mioFile;
int temp;

mioFile.open(percorso, ios::in);

if (!mioFile){
cout << "Errore nell'apertura file!\n";
return true;
}
size = 0;
while (!mioFile.eof()){
mioFile >> temp;
size++;
}

size = sqrt(size);

mat = new int*[size];
for (int i = 0; i < size; i++)
mat[i] = new int[size];

mioFile.clear();
mioFile.seekg(0, ios_base::beg);

for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
mioFile >> mat[i][j];
}
}
mioFile.close();
return false;
}

int calcola_somma(int vet[], int size){
int somma = 0;
for (int i = 0; i < size; i++)
somma += vet[i];
return somma;
}

void scambia_righe(int **mat, int i, int size){
int *temp;
temp = new int[size];

for (int j = 0; j < size; j++){
temp[j] = mat[i][j];
}

for (int j = 0; j < size; j++){
mat[i][j] = mat[i+1][j];
}

for (int j = 0; j < size; j++){
mat[i+1][j] = temp[j];
}
}

void ordina_matrice(int **mat, int size){
int *temp;
int somma = 0;
int scambi;
temp = new int[size];

do{
scambi = 0;
for (int i = 0; i < size - 1; i++){
if (calcola_somma(mat[i], size)>calcola_somma(mat[i + 1], size)){
scambia_righe(mat, i, size);
scambi++;
}
}
} while (scambi > 0);
}

void stampa_matrice(int **mat, int size){
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
*********************************************************************************
HEADER.H:
#include<iostream>
#include<fstream>
#include<string>
#include<math.h>
#include<string>

using namespace std;

int determina_size(string percorso);

bool carica_file(string percorso, int **&mat, int &size);

void ordina_matrice(int **mat, int size);

void stampa_matrice(int **mat, int size);

int calcola_somma(int vet[], int size);

void scambia_righe(int **mat, int i, int size);

COMPITO 5 (PICCIALLI, CHIAMNESE)

COMPITO 5 ORDINARE ELENCO

#include<iostream>
#include<string>
#include<fstream>

#define MAX 100

using namespace std;

struct persona{
string nome;
int numero;
};

bool carica_elenco(string percorso, persona lista[], int &riemp);
bool verifica_ordine(persona lista[], int riemp);
void ordine(persona lista[], int riemp);
void elimina_doppioni(persona lista[], int &riemp);
void stampa_elenco(persona lista[], int riemp);

int main(){
string percorso;
persona lista[MAX];
int riemp = 0;
bool errore;

cout << "Inserire il percorso del file: ";
cin >> percorso;

do{
errore = carica_elenco(percorso, lista, riemp);
if (errore){
cout << "Inserire un nuovo percorso: ";
cin >> percorso;
}
} while (errore);

if (!verifica_ordine(lista, riemp))
ordine(lista, riemp);

elimina_doppioni(lista, riemp);

stampa_elenco(lista, riemp);

system("PAUSE");

return 0;
}

bool carica_elenco(string percorso, persona lista[], int &riemp){
ifstream mioFile;

riemp = 0;

mioFile.open(percorso, ios::in);
if (!mioFile){
cout << "Errore nell'apertura del file!\n";
return true;
}

while (!mioFile.eof()){
mioFile >> lista[riemp].nome;
mioFile >> lista[riemp].numero;
riemp++;
}

mioFile.close();
return false;
}

bool verifica_ordine(persona lista[], int riemp){
bool ordinato = true;
int i = 0;

while (i < riemp-1 && ordinato){
if (lista[i].nome.compare(lista[i + 1].nome) > 0){
ordinato = false;
}
i++;
}
return ordinato;
}

void ordine(persona lista[], int riemp){
int scambi;
persona temp;

do{
scambi = 0;
for (int i = 0; i < riemp - 1; i++){
if (lista[i].nome.compare(lista[i + 1].nome) > 0){
temp = lista[i];
lista[i] = lista[i + 1];
lista[i + 1] = temp;
scambi++;
}
}
} while (scambi > 0);
}

void elimina_doppioni(persona lista[], int &riemp){
for (int i = 0; i < riemp - 1; i++){
for (int j = i + 1; j < riemp; j++){
if (lista[i].nome == lista[j].nome){
for (int k = j; k < riemp - 1; k++){
lista[k] = lista[k + 1];
}
riemp--;
j--;
}
}
}
return;
}

void stampa_elenco(persona lista[], int riemp){
for (int i = 0; i < riemp; i++){
cout << lista[i].nome << endl << lista[i].numero << endl;
}
}

COMPITO 4 (CHIANESE ,PICCIALLI)

COMPITO 4  MEDIA STIPENDI

#include<iostream>
#include<fstream>
#include<string>

#define MAX 100

using namespace std;

struct impiegato{
string nome;
string cognome;
int stipendio;
};

bool carica_file(string percorso, impiegato lista[], int &riemp);
int media_stipendi(impiegato lista[], int riemp);
void stampa_max(impiegato lista[], int riemp);
void stampa_oltre_media(impiegato lista[], int riemp, int media);

int main(){
string percorso;
impiegato lista[MAX];
int riemp = 0;
bool errore;
int media;

cout << "Inserire percorso file: ";
cin >> percorso;

do{
errore = carica_file(percorso, lista, riemp);
if (errore){
cout << "Inserire un nuovo percorso: ";
cin >> percorso;
}
} while (errore);

stampa_max(lista, riemp);

media = media_stipendi(lista, riemp);

stampa_oltre_media(lista, riemp, media);

system("PAUSE");

return 0;
}

bool carica_file(string percorso, impiegato lista[], int &riemp){
ifstream mioFile;
riemp = 0;
mioFile.open(percorso, ios::in);
if (!mioFile){
cout << "Errore nell'apertura del file!\n";
return true;
}

while (!mioFile.eof()){
mioFile >> lista[riemp].nome;
mioFile >> lista[riemp].cognome;
mioFile >> lista[riemp].stipendio;
riemp++;
}

mioFile.close();
return false;
}

int media_stipendi(impiegato lista[], int riemp){
int somma = 0;
if (riemp == 0)
return 0;
for (int i = 0; i < riemp; i++){
somma += lista[i].stipendio;
}
somma /= riemp;

cout << "Lo stipendio medio e': " << somma<<endl;

return somma;
}

void stampa_max(impiegato lista[], int riemp){
int max, imax=0;
max = lista[0].stipendio;
for (int i = 1; i < riemp; i++){
if (lista[i].stipendio > max){
max = lista[i].stipendio;
imax = i;
}
}
cout << "L'impiegato con lo stipendio maggiore e' " << lista[imax].nome << " " << lista[imax].cognome << ".\n";
return;
}
void stampa_oltre_media(impiegato lista[], int riemp, int media){
bool primo = true;
cout << "Gli impiegati che guadagnano piu' della media sono:\n";
for (int i = 0; i < riemp; i++){
if (lista[i].stipendio > media){
if (!primo){
cout << ", ";
}
cout << lista[i].nome << " " << lista[i].cognome;
if (primo)
primo = false;
}
}
cout << ".\n";
}

COMPITO 3 (PICCIALLI , CHIANESE)

COMPITO 3  MASCHIO FEMMINA

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

#define MAX 100

using namespace std;

int esiste(string nome, string vet[], int riemp);
bool is_male(string nome);
bool carica_file(string percorso, string maschi[], int &maschiRiemp, string femmine[], int &femmineRiemp);
void output(string maschi[], int maschiRiemp, string femmine[], int femmineRiemp);

int main(){
string percorso, maschi[MAX], femmine[MAX];
int maschiRiemp=0, femmineRiemp = 0;
bool err;

percorso = "C:\\fondamenti\\compito3\\fil.txt";

//cout << "Inserire il percorso: ";

do{
//cin >> percorso;
err = carica_file(percorso, maschi, maschiRiemp, femmine, femmineRiemp);
if (err){
cout << "Inserire un nuovo percorso: ";
cin >> percorso;
}
} while (err);

output(maschi, maschiRiemp, femmine, femmineRiemp);

system("PAUSE");

return 0;
}

int esiste(string nome, string vet[], int riemp){
int i = 0;
bool trovato = false;
while (i < riemp && !trovato){
if (nome.compare(vet[i]) == 0)
trovato = true;
else
i++;
}

if (trovato)
return i;
else
return -1;
}

bool is_male(string nome){
if (nome.back() == 'o')
return true;
else
return false;
}

bool carica_file(string percorso, string maschi[], int &maschiRiemp, string femmine[], int &femmineRiemp){
ifstream mioFile;
string temp;

mioFile.open(percorso, ios::in);
if (!mioFile){
cout << "Errore nell'apertura del file!\n";
return true;
}

while (!mioFile.eof()){
mioFile >> temp;
if (is_male(temp)){
if (esiste(temp, maschi, maschiRiemp)==-1){
maschi[maschiRiemp++] = temp;
}
}
else{
if (esiste(temp, femmine, femmineRiemp)==-1){
femmine[femmineRiemp++] = temp;
}
}
}
mioFile.close();
return false;
}

void output(string maschi[], int maschiRiemp, string femmine[], int femmineRiemp){
cout << "Il file contiene " << maschiRiemp << " nomi maschili diversi (";
for (int i = 0; i < maschiRiemp; i++){
cout << maschi[i];
if (i != maschiRiemp - 1)
cout << ", ";
}
cout << ") e " << femmineRiemp << " nomi femminili diversi (";
for (int i = 0; i < femmineRiemp; i++){
cout << femmine[i];
if (i != femmineRiemp - 1)
cout << ", ";
else
cout << ").\n";
}
}

COMPITO 2 (PICCIALLI, CIANESE)

COMPITO 2 PALINDROMI

#include<iostream>
#include<fstream>
#include<string>

#define MAX_SIZE 100

using namespace std;

struct coppie{
int numero;
int occorrenze;
};

bool is_palindromo(int n);
void carica_numeri(string percorso, coppie lista[], int &riemp);
int is_presente(int n, coppie lista[], int riemp);
void ordina_lista(coppie lista[], int riemp);
void stampa_lista(coppie lista[], int riemp);

int main(){
coppie lista[MAX_SIZE];
string percorso_file;
int riemp = 0;

percorso_file = "C:\\fondamenti\\compito2\\file.txt";

carica_numeri(percorso_file, lista, riemp);
if (riemp == -1){
system("PAUSE");
return -1;
}

ordina_lista(lista, riemp);

stampa_lista(lista, riemp);

system("PAUSE");
return 0;
}

bool is_palindromo(int n){
int a[20], riemp=0;
bool palindromo = true;
int i;
do{
a[riemp] = n % 10;
riemp++;
n /= 10;
} while (n != 0);

i = 0;
while (i < riemp/2 && palindromo){
if (a[i] != a[riemp - i - 1])
palindromo = false;
i++;
}
return palindromo;
}

void carica_numeri(string percorso, coppie lista[], int &riemp){
int n, index;
ifstream mioFile;
mioFile.open(percorso, ios::in);
if (!mioFile){
cout << "Errore nell'apertura!\n";
riemp = -1;
return;
}
while (!mioFile.eof()){
mioFile >> n;
if (is_palindromo(n)){
index = is_presente(n, lista, riemp);
if (index != -1)
lista[index].occorrenze++;
else{
lista[riemp].numero = n;
lista[riemp].occorrenze = 1;
riemp++;
}
}
}
mioFile.close();
}

int is_presente(int n, coppie lista[], int riemp){
bool presente = false;
int i = 0;
int posizione;
while (!presente && i < riemp){
if (lista[i].numero == n){
presente = true;
posizione = i;
}
i++;
}
if (!presente)
posizione = -1;
return posizione;
}

void ordina_lista(coppie lista[], int riemp){
int sw;
coppie temp;
do{
sw = 0;
for (int i = 0; i < riemp - 1; i++){
if (lista[i].occorrenze < lista[i + 1].occorrenze){
temp = lista[i];
lista[i] = lista[i + 1];
lista[i + 1] = temp;
sw++;
}
}
} while (sw > 0);
}

void stampa_lista(coppie lista[], int riemp){
for (int i = 0; i < riemp; i++){
cout << lista[i].numero << " compare " << lista[i].occorrenze << " volte.\n";
}
}

COMPITO 1 FONDAMENTI (PICCIALLI, CHIANESE)

MAIN :
#include<iostream>
#include<string>
#include "header.h"

using namespace std;

int main(){
int **mat1, **mat2;
int n1=0, n2=0;
string file1, file2;
file1 = "C:\\fondamenti\\compito1\\mat1.txt";
file2 = "C:\\fondamenti\\compito1\\mat2.txt";
carica_matrice(file1, mat1, n1);
if (n1 == -1)
return -1;
carica_matrice(file2, mat2, n2);
if (n2 == -1)
return -1;

cout << "mat1:\n";
stampa_matrice_quadrata(mat1, n1);

cout << "\nmat2:\n";
stampa_matrice_quadrata(mat2, n2);
cout << endl;

confronta(mat1, n1, mat2, n2);

system("PAUSE");

for (int i = 0; i < n1; i++)
delete[] mat1[i];
delete[] mat1;

for (int i = 0; i < n2; i++)
delete[] mat2[i];
delete[] mat2;

return 0;
}
*********************************************************************************
HEADER.CPP:
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void carica_matrice(string file, int** &mat, int &n){
ifstream mioFile;
mioFile.open(file, ios::in);

if (!mioFile){
cout << "Errore nell'apertura del file!\n";
n = -1;
return;
}

mioFile >> n;

mat = new int*[n];
for (int i = 0; i < n; i++)
mat[i] = new int[n];

for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
mioFile >> mat[i][j];
}

mioFile.close();
return;
}

void stampa_matrice_quadrata(int** mat, int n){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
return;
}

void confronta(int** mat1, int n1, int** mat2, int n2){
if (n1 != n2){
cout << "Le matrici hanno dimensioni diverse, non possono avere righe o colonne uguali\n";
return;
}

//Confronto righe
int trovato;
bool uguale;
int j;
for (int k = 0; k < n1; k++){
trovato = 0;
for (int i = 0; i < n1; i++){
uguale = true;
j = 0;
while (j < n1 && uguale){
if (mat1[k][j] != mat2[i][j])
uguale = false;
j++;
}
if (uguale){
trovato++;
if (trovato == 1)
cout << "La riga " << k << " di mat1 e' uguale alla riga " << i;
else
cout << " e alla riga " << i;
}
}
if (trovato != 0)
cout << " di mat2.\n";
}

//Confronto colonne
for (int k = 0; k < n1; k++){
trovato = 0;
for (int i = 0; i < n1; i++){
uguale = true;
j = 0;
while (j < n1 && uguale){
if (mat1[j][k] != mat2[j][i])
uguale = false;
j++;
}
if (uguale){
trovato++;
if (trovato == 1)
cout << "La colonna " << k << " di mat1 e' uguale alla colonna " << i;
else
cout << " e alla colonna " << i;
}
}
if (trovato != 0)
cout << " di mat2.\n";
}

return;
}
*********************************************************************************
HEADER.H:
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void carica_matrice(string file, int** &mat, int &n);
void stampa_matrice_quadrata(int** mat, int n);
void confronta(int** mat1, int n1, int** mat2, int n2);