Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #define MAX 100
- using namespace std;
- struct nomimaschili{
- char nomemaschio[MAX];
- };
- struct nomifemminili{
- char nomefemmina[MAX];
- };
- nomimaschili maschi [MAX];
- nomifemminili femmine [MAX];
- void apertura_file(fstream& file);
- int inserimento_nomi(fstream& file,int totalenomi,int& tot_m,int& tot_f);
- bool esiste_nelle_struct(nomimaschili records[],int size, char nome[]);
- bool esiste_nelle_struct(nomifemminili records[],int size, char nome[]);
- int main()
- {
- int tot_nomi;
- int tot_maschi;
- int tot_femmine;
- fstream myfile;
- apertura_file(myfile);
- tot_nomi = inserimento_nomi(myfile,tot_nomi,tot_maschi,tot_femmine);
- }
- void apertura_file(fstream& file){
- char nomefile[MAX];
- cout<<"Inserisci nome del file: ";
- cin>>nomefile;
- file.open(nomefile,ios::in);
- if(!file){
- cout<<"Errore apertura file";
- exit(1);
- }
- }
- int inserimento_nomi(fstream& file,int totalenomi,int& tot_m,int& tot_f){
- int contatore_nomi;
- int contatore_m = 0;
- int contatore_f = 0;
- int i = 0;
- while(!file.eof()){
- char nome[MAX];
- int lunghezza;
- file>>nome;
- lunghezza = strlen(nome);
- if(nome[lunghezza-1] == 'o' && esiste_nelle_struct(maschi,MAX,nome)){
- strcpy(maschi[i].nomemaschio,nome);
- contatore_m++;
- i++;
- }
- else if(nome[lunghezza-1] == 'a' && esiste_nelle_struct(femmine,MAX,nome)){
- strcpy(femmine[i].nomefemmina,nome);
- contatore_f++;
- i++;
- }
- }
- tot_m = contatore_m;
- tot_f = contatore_f;
- return contatore_nomi;
- }
- bool esiste_nelle_struct(nomimaschili records[],int size, char nome[]){
- for(int i = 0; i < size; i++)
- {
- if(records[i].nomemaschio == nome)
- return 1;
- }
- return 0;
- }
- bool esiste_nelle_struct(nomifemminili records[],int size, char nome[]){
- for(int i = 0; i < size; i++)
- {
- if(records[i].nomefemmina == nome)
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement