domenica 12 febbraio 2017

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";
}
}

Nessun commento :

Posta un commento