domenica 12 febbraio 2017

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);

Nessun commento :

Posta un commento