Delphi & Pascal (česká wiki)
/* ========================================================================== */ /* */ /* CCastic.cpp */ /* */ /* Description */ /* Trida vypocita rapitity castice pt nad souborem podle vzorce: */ /* ____________________ */ /* V m2 + (px)2 + (py)2 + pz */ /* y = 1/2*ln(----------------------------) */ /* ____________________ */ /* V m2 + (px)2 + (py)2 - pz */ /* */ /* Vypocita faktor c pro 2 castice: */ /* _______________________________ _______________________________ */ /* c =(V m2 + (px1)2 + (py1)2 + (pz1)2 * V m2 + (px2)2 + (py2)2 + (pz2)2 - (px1 * px2 + py1*py2 + pz1*pz2))*1/m2 */ /* _____ */ /* y12 = ln(c+ Vc2-1) */ /* */ /* ========================================================================== */ #include <conio.h> #include <string.h> #include <io.h> #include "ccastic.h" // konstruktor CCastic::CCastic(char* meno, float _m) { m = _m; memset(&cs, 0x00, sizeof(cs)); lsize = 1; por = 0; N = 1; rz = '|'; // znuluj kursory for(int i=0; i<POC_CURSORS; i++) { filepos[i] = 0; memset(&cs_cursor[i], 0x00, sizeof(cs)); } // hledana castice hcast = _m; // otevre soubor f = fopen(meno, "rb"); GetLineSize(); cs.por = 0; // MoveNext(); } // destruktor CCastic::~CCastic() { fclose(f); } // nastav parametr m void CCastic::SetM(float _m) { m = _m; } // vypocita rapitity castice // ____________________ // V m2 + (px)2 + (py)2 + pz // y = 1/2*ln(----------------------------) // ____________________ // V m2 + (px)2 + (py)2 - pz // float CCastic::CalcRapidity() { float mv; // medzivypocet mv = pow(m,2) + pow(cs.px,2) + pow(cs.py,2) + pow(cs.pz,2); // vysledek cs.y = 1.0/2 * log( (sqrt(mv) + cs.pz) / (sqrt(mv) - cs.pz)); return cs.y; } // vypocita faktor c pro 2 castice // _______________________________ _______________________________ // c =(V m2 + (px1)2 + (py1)2 + (pz1)2 * V m2 + (px2)2 + (py2)2 + (pz2)2 - (px1 * px2 + py1*py2 + pz1*pz2))*1/m2 // _____ // y12 = ln(c+ Vc2-1) // float CCastic::CalcFaktorC(castic cs2) { float c1,c2,c3,c; float y12; c1 = sqrt( pow(m,2) + pow(cs.px,2) + pow(cs.py,2) + pow(cs.pz,2)); c2 = sqrt( pow(m,2) + pow(cs2.px,2) + pow(cs2.py,2) + pow(cs2.pz,2)); c3 = cs.px * cs2.px + cs.py * cs2.py + cs.pz * cs2.pz; c = (c1*c2-c3) / pow(m,2); y12 = log(c + sqrt(pow(c,2) - 1)); return y12; } // vypocita Pt // __________ // Pt = V px2 + py2 // float CCastic::CalcPt() { float Pt; Pt = sqrt( pow(cs.px,2) + pow(cs.py,2)); return Pt; } // vrat hodnotu aktualne castice castic CCastic::GetCastice() { return cs; } // hodnota cursora castic CCastic::GetCasticeCursor(int i) { return cs_cursor[i]; } // aka je velikost radku void CCastic::GetLineSize() { fseek(f,0L, SEEK_SET); fscanf(f,"%d%d%d%g%g%g", &cs.event, &cs.poradi, &cs.cislo, &cs.px, &cs.py, &cs.pz); fgetpos(f, &lsize); lsize++; // aj \n fseek(f,0L, SEEK_SET); } // presun na prvni radek void CCastic::MoveFirst() { fseek(f,0L, SEEK_SET); cs.por = 0; MoveNext(); } // precte dalsi radek void CCastic::MoveNext() { if( IsEOF()) return; do { // precti fscanf(f,"%d%d%d%g%g%g\n", &cs.event, &cs.poradi, &cs.cislo, &cs.px, &cs.py, &cs.pz); cs.por++; // zobraz ruzici aby videl ze neco dela, iba kazdych 10 aby nezdrzoval // if(( cs.por % 10 ) == 0) // ViewRuzica(cs.por); } while(( cs.cislo != hcast ) && !IsEOF()); // vypocti rapitidy // CalcRapidity(); } // precte predosly rad void CCastic::MovePrev() { fpos_t pos; // aktualni pozice fgetpos(f, &pos); do { pos -= 2*lsize; fseek(f, pos, SEEK_SET); fscanf(f,"%d%d%d%f%f%f\n", &cs.event, &cs.poradi, &cs.cislo, &cs.px, &cs.py, &cs.pz); cs.por--; } while(( abs(cs.cislo) != hcast ) && ( pos > 0 )); // vypocti rapitidy CalcRapidity(); } // presun na posledni void CCastic::MoveLast() { long size; fseek(f, 0L, SEEK_END); fgetpos(f, &size); cs.por = size/lsize; fseek(f, (size-lsize), SEEK_SET); fscanf(f,"%d%d%d%f%f%f\n", &cs.event, &cs.poradi, &cs.cislo, &cs.px, &cs.py, &cs.pz); // posledny nesuhlasi - hladaj predosly if( abs(cs.cislo) != hcast ) MovePrev(); } // je konec souboru int CCastic::IsEOF() { return feof(f); } // zmaze aktualni castici void CCastic::ClearCursor() { fpos_t pos; int i; // ulozime poziciu fgetpos(f, &pos); fclose(f); // otvorime a zapiseme nuly f = fopen(TMP_FILE, "r+b"); i = 1; while( cs_cursor[i].por != 0 ) { // fseek(f, (cs_cursor[i].por-1) * lsize, SEEK_SET); // zmazanu zapis cs_cursor[i].cislo = 0; fprintf(f,"%8d%7d%9d%12.8f%12.8f%12.8f\n", cs_cursor[i].event, cs_cursor[i].poradi, cs_cursor[i].cislo , cs_cursor[i].px, cs_cursor[i].py, cs_cursor[i].pz); // cele zmaz memset(&cs_cursor[i], 0x00, sizeof(cs_cursor[i])); i++; } fclose(f); // otovrime znova na pozicii f = fopen(TMP_FILE, "rb"); fsetpos(f, &pos); } // zapamatuje si pozici kurzora void CCastic::GetCursor(int i) { // prekrocen max. pocet cursoru if( i>= POC_CURSORS ) return; fgetpos(f, &filepos[i]); cs_cursor[i] = cs; } // obnovi pozici kurzora void CCastic::SetCursor(int i) { // prekrocen max. pocet cursoru if( i>= POC_CURSORS ) return; if( filepos != 0 ) { fsetpos(f, &filepos[i]); cs = cs_cursor[i]; } } // kresli ruzicu aby bylo videt ze neco dela void CCastic::ViewRuzica(long cislo) { int x,y; x = wherex(); y = wherey(); if( cislo != 0 ) printf("%8ld %c", cislo, rz); else printf(" %c", rz); gotoxy(x,y); // posune na dalsi znak switch( rz ) { case '|' : rz = '/'; break; case '/' : rz = '-'; break; case '-' : rz = '\\'; break; case '\\': rz = '|'; break; } } // spocte N void CCastic::CountN() { MoveLast(); N = cs.por / cs.event; } // vrati N int CCastic::GetN() { return N; }