Naloga:
Napišite program, ki prebere rezultate analize barve oči. V datoteki eyes.txt so podatki o spolu osebe in barvi oči. Program na ugotovi, koliko je osebkov (moških in žensk skupaj) z določeno barvo oči in naj rezultate zapiše v datoteko. Z gnuplotom narišite histogram. Ugotovite še, če imajo moški in ženske različne barve oči.
#include <iostream> #include <cmath> #include <fstream> #include <iomanip> using namespace std; int main () { int i,j; int brown=0,blue=0,hazel=0,green=0,brownf=0,bluef=0,hazelf=0,greenf=0; int m,f; string x,y; ofstream mf; mf.open ("hist2.txt"); ifstream eyes; eyes.open ("eyes.txt"); while (!eyes.eof()){ eyes >> j >> x >> y; if (!eyes.eof()) { i=i+1; if ((x=="m")&&(y=="brown")) brown=brown+1; if ((x=="m")&&(y=="blue")) blue=blue+1; if ((x=="m")&&(y=="hazel")) hazel=hazel+1; if ((x=="m")&&(y=="green")) green=green+1; if ((x=="f")&&(y=="brown")) brownf=brownf+1; if ((x=="f")&&(y=="blue")) bluef=bluef+1; if ((x=="f")&&(y=="hazel")) hazelf=hazelf+1; if ((x=="f")&&(y=="green")) greenf=greenf+1; } } m=brown+blue+green+hazel; f=brownf+bluef+greenf+hazelf; mf << 1 << " "<< brown+brownf<< " "<< brown<< " "<< brownf<< " "<< m<< " "<< f<< " brown"<< endl; mf << 2 << " "<< blue+bluef<< " "<< blue<< " "<< bluef<< " "<< m<< " "<< f<< " blue"<< endl; mf << 3 << " "<< hazel+hazelf<< " "<< hazel<< " "<< hazelf<< " "<< m<< " "<< f<< " hazel"<< endl; mf << 4 << " "<< green+greenf<< " "<< green<< " "<< greenf<< " "<< m<< " "<< f<< " green"<< endl; mf.close(); eyes.close(); cout << "histogram je narejen."; }
Tekst sedaj shranimo kot datoteko oci.cpp in prevedemo.
Tu je sedaj še print screen postopka prevajanja, postopka risanja z gnuplotom in narisana grafa v pdf datoteki. V kolikor vam kakšna podrobnost ni jasna, mi sporočite. Histogram je narisan v naslednji PDF datoteki.
Ter podatki za histogram v hist2.txt
Nazaj na osnovno stran za predmet Uporaba numeričnih metod v kemiji.