rovan Δημοσ. 7 Ιουνίου 2009 Δημοσ. 7 Ιουνίου 2009 Γεια σε όλους. Θα ήθελα αν μπορεί κάποιος να δώσει τα φώτα του. Έχω φτιάξει το παρακάτω πρόγραμμα το οποίο διαβάζει διάφορες διαστάσεις σε ιντσες και πόδια (πχ 12 ποδια και 3 ιντσες) απο αρχείο και τις αθροιζει, βρισκει τη μεγιστη και μετρα τις λαθος διαστάσεις. Το αρχειο ειναι ενα απλο μορφοποιημενο αρχειο dataDis.dat. Αυτο που θελω να κανω ειναι να διαβαζω τα δεοδομενα απο ενα non-formatted δυαδικο αρχειο. Δωστε μου τα φωτα σας για τις αλλαγες που πρεπει να κανω. Παρακάτω παραθετω τον κωδικα #include <iostream.h> #include <fstream.h> #include <string> class DistanceXptn { public: enum {la9ospodia,la9osinches}; DistanceXptn (std::string onoma, int eidos, int v) { onomame9odou=onoma; kwdla9ous=eidos; la9timi=v; } std::string getOnoma ()const { return onomame9odou; } int getkwdiko ()const { return kwdla9ous; } int getla9timi() const { return la9timi; } private: std::string onomame9odou; int kwdla9ous; int la9timi; }; class Distance{ private: int foot; float inches; public: Distance(int nfoot, float ninches) { if (nfoot<0) throw DistanceXptn("Distance",DistanceXptn::la9ospodia,n foot); if(ninches<0||ninches>12) throw DistanceXptn("Distance",DistanceXptn::la9osinches, ninches); foot=nfoot; inches=ninches; } Distance() { foot=0; inches=0; } void setFoot(int nfoot) { if(nfoot<0) throw DistanceXptn("setFoot",DistanceXptn::la9ospodia,nf oot); else foot=nfoot; } int getFoot() { return foot; } void setInches(float ninches) { if(ninches<=0||ninches>12.0) throw DistanceXptn("setInches",DistanceXptn::la9osinches ,ninches); else inches=ninches; } float getInches() { return inches; } Distance addDist(Distance d1, Distance d2) { d2.foot=d2.foot + d1.foot; d2.inches=d2.inches + d1.inches; if (d2.inches>=12.0) { d2.foot= d2.foot + 1; d2.inches=d2.inches - 12.0; } return d2; } Distance Megisto(Distance d,Distance max) { if (d.foot>max.foot) {max=d;} else if ((d.foot=max.foot) && (d.inches>max.inches)) {max=d;} return max; } void ektyposeAthroisma(Distance d) { cout<<"To synoliko athroisma ton soston apostaseon einai "<<d.foot<<" podia kai "<<d.inches<<" intses."<<endl; } void ektyposeMegisto(Distance max) { cout<<"H megisth apostash einai "<<max.foot<<" podia kai "<<max.inches<<" intses."<<endl; } }; int main() { Distance d,d1,max; int foot,Lathosf=0,Lathosi=0,Lathosd; float inches; ifstream x; x.open("dataDis.dta"); x>>foot; x>>inches; while (!x.eof()) { try { d.setFoot(foot); d.setInches(inches); d.getFoot(); d.getInches(); d1=d1.addDist(d,d1); max=max.Megisto(d,max); } catch(DistanceXptn x) { switch (x.getkwdiko()) { case DistanceXptn::la9ospodia: cout<<"lathos podia-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosf++; break; case DistanceXptn::la9osinches: cout<<"lathos inches stin-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosi++; break; default: cout<<"mi anamenomeni exeresi apo antikimeno tis klasis Distance";endl; } } x>>foot; x>>inches; } Lathosd = Lathosf + Lathosi; d1.ektyposeAthroisma(d1); cout<<"Oi lathos diastaseis einai "<<Lathosd<<" (se podia "<<Lathosf<<" kai se inches "<<Lathosi<<")."<<endl; max.ektyposeMegisto(max); x.close(); int XXX; cout<<"telos programmatos"; cin>>XXX; return 0; } ---------- Το μήνυμα προστέθηκε στις 18:34 ---------- Το προβλημα ειναι στη main την οποια παραθετω παρακατω. Η αναγνωση απο δυαδικο αρχειο δε γινεται οπως απο απλο txt αρχειο int main() { Distance d,d1,max; int foot,Lathosf=0,Lathosi=0,Lathosd; float inches; ifstream x; x.open("dataDis.dta"); x>>foot; x>>inches; while (!x.eof()) { try { d.setFoot(foot); d.setInches(inches); d.getFoot(); d.getInches(); d1=d1.addDist(d,d1); max=max.Megisto(d,max); } catch(DistanceXptn x) { switch (x.getkwdiko()) { case DistanceXptn::la9ospodia: cout<<"lathos podia-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosf++; break; case DistanceXptn::la9osinches: cout<<"lathos inches stin-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosi++; break; default: cout<<"mi anamenomeni exeresi apo antikimeno tis klasis Distance";endl; } } x>>foot; x>>inches; } Lathosd = Lathosf + Lathosi; d1.ektyposeAthroisma(d1); cout<<"Oi lathos diastaseis einai "<<Lathosd<<" (se podia "<<Lathosf<<" kai se inches "<<Lathosi<<")."<<endl; max.ektyposeMegisto(max);
bxenos Δημοσ. 8 Ιουνίου 2009 Δημοσ. 8 Ιουνίου 2009 Στοίχησε το και βάλτο σε [cοde] ... [/cοde] για να είναι ευανάγνωτο. Οταν λες δυαδικά υποθέτω θέλεις binary αρχειο σταθερού record size.
rovan Δημοσ. 8 Ιουνίου 2009 Μέλος Δημοσ. 8 Ιουνίου 2009 [/color] > #include <iostream.h> #include <fstream.h> #include <string> class DistanceXptn { public: enum {la9ospodia,la9osinches}; DistanceXptn (std::string onoma, int eidos, int v) { onomame9odou=onoma; kwdla9ous=eidos; la9timi=v; } std::string getOnoma ()const { return onomame9odou; } int getkwdiko ()const { return kwdla9ous; } int getla9timi() const { return la9timi; } private: std::string onomame9odou; int kwdla9ous; int la9timi; }; class Distance{ private: int foot; float inches; public: Distance(int nfoot, float ninches) { if (nfoot<0) throw DistanceXptn("Distance",DistanceXptn::la9ospodia,n foot); if(ninches<0||ninches>12) throw DistanceXptn("Distance",DistanceXptn::la9osinches, ninches); foot=nfoot; inches=ninches; } Distance() { foot=0; inches=0; } void setFoot(int nfoot) { if(nfoot<0) throw DistanceXptn("setFoot",DistanceXptn::la9ospodia,nf oot); else foot=nfoot; } int getFoot() { return foot; } void setInches(float ninches) { if(ninches<=0||ninches>12.0) throw DistanceXptn("setInches",DistanceXptn::la9osinches ,ninches); else inches=ninches; } float getInches() { return inches; } Distance addDist(Distance d1, Distance d2) { d2.foot=d2.foot + d1.foot; d2.inches=d2.inches + d1.inches; if (d2.inches>=12.0) { d2.foot= d2.foot + 1; d2.inches=d2.inches - 12.0; } return d2; } Distance Megisto(Distance d,Distance max) { if (d.foot>max.foot) {max=d;} else if ((d.foot=max.foot) && (d.inches>max.inches)) {max=d;} return max; } void ektyposeAthroisma(Distance d) { cout<<"To synoliko athroisma ton soston apostaseon einai "<<d.foot<<" podia kai "<<d.inches<<" intses."<<endl; } void ektyposeMegisto(Distance max) { cout<<"H megisth apostash einai "<<max.foot<<" podia kai "<<max.inches<<" intses."<<endl; } }; int main() { Distance d,d1,max; int foot,Lathosf=0,Lathosi=0,Lathosd; float inches; ifstream x; x.open("dataDis.dta"); x>>foot; x>>inches; while (!x.eof()) { try { d.setFoot(foot); d.setInches(inches); d.getFoot(); d.getInches(); d1=d1.addDist(d,d1); max=max.Megisto(d,max); } catch(DistanceXptn x) { switch (x.getkwdiko()) { case DistanceXptn::la9ospodia: cout<<"lathos podia-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosf++; break; case DistanceXptn::la9osinches: cout<<"lathos inches stin-->"<<x.getOnoma()<<":"<<x.getla9timi()<<endl; Lathosi++; break; default: cout<<"mi anamenomeni exeresi apo antikimeno tis klasis Distance";endl; } } x>>foot; x>>inches; } Lathosd = Lathosf + Lathosi; d1.ektyposeAthroisma(d1); cout<<"Oi lathos diastaseis einai "<<Lathosd<<" (se podia "<<Lathosf<<" kai se inches "<<Lathosi<<")."<<endl; max.ektyposeMegisto(max); x.close(); int XXX; cout<<"telos programmatos"; cin>>XXX; return 0; } Ναι θελω να γινει το ιδιο πραγμα με χρηση εισοδου απο binary αρχειο. Το αρχειο αυτο θα εχει διαστασεις σε ποδια και ιντσες. Και μια ακομη ερωτηση: Πως μπορω να δημιουργησω εγω ενα binary αρχειο ετσι ωστε να τεσταρω το προγραμμα που φτιαχνω; ---------- Το μήνυμα προστέθηκε στις 21:36 ---------- Και μια ακομη ερωτηση. Εστω εχω το αρχειο 1.dta που ειναι binary και περιεχει εναν ακεραιο και εναν float. Θελω τα δεδομενα του να τα περασω στο αρχειο 2.txt που ειναι text αρχειο. Πως γινεται αυτο;
bxenos Δημοσ. 9 Ιουνίου 2009 Δημοσ. 9 Ιουνίου 2009 Ναι θελω να γινει το ιδιο πραγμα με χρηση εισοδου απο binary αρχειο. Το αρχειο αυτο θα εχει διαστασεις σε ποδια και ιντσες. δες εδώ http://courses.cs.vt.edu/~cs2604/fall01/binio.html Και μια ακομη ερωτηση: Πως μπορω να δημιουργησω εγω ενα binary αρχειο ετσι ωστε να τεσταρω το προγραμμα που φτιαχνω; φτιάχνεις ενα άλλο προγραμματακι να κατασκευάζει binary αρχείο οπως στην ιστοσελιδα που σου εγραψα. Και μια ακομη ερωτηση. Εστω εχω το αρχειο 1.dta που ειναι binary και περιεχει εναν ακεραιο και εναν float. Θελω τα δεδομενα του να τα περασω στο αρχειο 2.txt που ειναι text αρχειο. Πως γινεται αυτο; ανοίγεις ένα binary αρχειο για διαβασμα ifstream και ένα text για γράψιμο ofstream. ΣΣ. στοίχιση (indentation) http://catb.org/~esr/jargon/html/I/indent-style.html
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.