Hilfe Problem mit Programmcode - bitte mal anschauen
-
Nun gut das mit den push_back hab ich jetzt verstanden, bei der unübersichtlichkeit hab ich das net gesehen .... mom ich überarbeite eben noch mal den source...
//Edit:
ist jetzt kompilierbar... mehr auch nicht. Ich muss ganz ehrlich sagen das der Code übelst ist. -.- Ich hab aber keine Lust so nen Schwall code so zu überarbeiten das es brauchbar wird
MfG
#include "stdafx.h" #include <cmath> #include <vector> #include <conio.h> #include <cstring> #include <iostream> #include <iomanip> #include <fstream> #include <boost/lexical_cast.hpp> using namespace std; //using namespace std; class Lagerdaten { protected: vector <int> d; vector <float> e, dynC, statC, y, y0; string Datei; public: std::size_t get_d_size() const { return d.size(); } vector <int> D; vector <string> typ; Lagerdaten(string LagerDatei) { Datei=LagerDatei; } virtual ~Lagerdaten(){} void Einlesen() { ifstream Lagerliste(Datei.c_str()); Lagerliste.seekg(ios::beg); std::string tmp; while (!Lagerliste.eof() && !Lagerliste.fail() ) { getline(Lagerliste,tmp,'\t'); d.push_back(boost::lexical_cast<int>(tmp)); getline(Lagerliste,tmp,'\t'); D.push_back(boost::lexical_cast<int>(tmp)); getline(Lagerliste,tmp,'\t'); dynC.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); e.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); y.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); statC.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); y0.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp); typ.push_back(tmp); } } }; class dynBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int *z) { double p; if ((Fa/Fr)<=e[*z]) { if (axial==1) p=Fr; else p=Fr+1.12*y[*z]*Fa; } else { if (axial==1) p=0,4*Fr+y[*z]*Fa; else p=0.67*Fr+1.68*y[*z]*Fa; } return p; } public: dynBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string const & LAGERDATEI) : Lagerdaten(LAGERDATEI) { FR=Fr; FA=Fa; LL=L; N=n; AXIAL=axial; } int SucheLagerDyn(int y) { Einlesen(); double CTRAG; int IndexOpt; CTRAG=pow((L*n*60/1000000),(3/10))*p(&y); if (axial==2) CTRAG=CTRAG/1.715; if (dynC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } }; class statBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z) { double p; if ((Fa/Fr)<=(1/(2*y0[z]))) { if (axial==1) p=Fr; else p= 0.5 * Fr + y0[z] * Fa; } else { p=Fr+2*y0[z]*Fa; } return p; } public: statBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string const & LAGERDATEI) : Lagerdaten(LAGERDATEI) { FR=Fr; FA=Fa; LL=L; N=n; AXIAL=axial; } int SucheLagerStat(int y) { Einlesen(); double CTRAG; int IndexOpt; CTRAG=pow((L*n*60/1000000),(3/10))* p(y); if (axial==2) CTRAG=CTRAG/1.715; if (statC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } }; int main() { double Fa, Fr; int axial, L, d, n, optLager; string LagerDatei = "lager.txt"; cout << "Lagerbestimmungsprogramm am Bsp. FAG Kegelrollenlager\n\n" << endl; cout << "Geben Sie die konstruktiven Merkmale an:" << endl; cout << "\n\nAxialkraft in kN: "; cin >> Fa; Fa=fabs(Fa); cout << "\nRadialkraft in kN: "; //cin << Fr; cin >> Fr; Fr=fabs(Fr); cout << "\n(1)einseitige oder (2)zweiseitige axiale Belastung?: "; do cin >> axial; while(axial==1 || axial==2); cout << "\nGewünschte Lebensdauer: "; cin >> L; cout << "\nWellendurchmesser: "; cin >> d; cout << "\nDrehzahl in min^-1: "; cin >> n; if(n<=10) { statBeanspruchung statBea(Fr, Fa, L, n, axial, LagerDatei); /* for(unsigned x =0; x<= statBea.get_d_size(); x++) { if(statBea[x]==d) <--- ??? ) optLager = statBea.SucheLagerStat(&x); } */ cout << "\n\nDas entsprechende Lager entspricht FAG" << statBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << statBea.D[optLager] << endl; } else { dynBeanspruchung dynBea(Fr, Fa, L, n, axial, LagerDatei); /* for(unsigned x=0; x<=dynBea.get_d_size(); x++) { if(dynBea.d[x]==d) <---- ???? optLager = dynBea.SucheLagerDyn(&x); } */ cout << "\n\nDas entsprechende Lager entspricht FAG" << dynBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << dynBea.D[optLager] << endl; } getch(); return 0; }
-
evilissimo schrieb:
ist jetzt kompilierbar... mehr auch nicht. Ich muss ganz ehrlich sagen das der Code übelst ist. -.- Ich hab aber keine Lust so nen Schwall code so zu überarbeiten das es brauchbar wird
Warum ist der Code übelst, und warum bezeichnest du ihn als Schwall code.
Was mache ich falsch, wenn du sowas sagen musst? Passt dir die Form nicht oder wie? Würd mich echt mal interessieren?Was soll das Ganze mit "lexical_cast"?
-
linuxfan132 schrieb:
Was soll das Ganze mit "lexical_cast"?
das kommt aus so'ner zusatz library für c++, ist das gleich wie atoi(tmp.c_str()) bzw. atof(tmp.c_str())
-
Bin zwar kein Experte aber Public: schreibt man z.B. vor Protectet.
Vielleicht meinte er sowas mit schlechtem Code.
-
linuxfan132 schrieb:
evilissimo schrieb:
ist jetzt kompilierbar... mehr auch nicht. Ich muss ganz ehrlich sagen das der Code übelst ist. -.- Ich hab aber keine Lust so nen Schwall code so zu überarbeiten das es brauchbar wird
Warum ist der Code übelst, und warum bezeichnest du ihn als Schwall code.
Schwall = Sehr Viel
Was mache ich falsch, wenn du sowas sagen musst? Passt dir die Form nicht oder wie? Würd mich echt mal interessieren?
Du solltest dich einfach mal etwas mit C++ beschäftigen, ich hab den Code nicht verbessert da ich heute morgen dazu auch gar keine Lust hatte, ich hab einfach net schlafen können. Das war alles. Du verwendest z.b. Pointer wo es keinen Sinn macht, dann dereferenzierst du sie nicht mal zum benutzen. Du willst auf eine äusserst seltsame weise elemente einem Container hinzufügen, usw. Da sind so viele Punkte....
Was soll das Ganze mit "lexical_cast"?
lexical_cast convertiert die Strings in Zahlen.
-
Hast du denn überhaupt probiert dich schlafen zu legen?
-
Ja wie gesagt, mir fehlt eben die Routine. Wie kann ich denn sonst die Daten aus der Datei in den vector einlesen? Mit den Pointern wollt ich doch nur den Speicher schonen! Lehr mich doch mal, scheinst ja echt Ahnung zu haben, von dem was du machst. Ich bin Maschinenbaustudent, kein Informatiker. Sag mir doch mal, wie man den Code verbessern kann, sodass das dann am Ende auch funktioniert.
PS: der Vergleich unten in der for-Scheleife soll heißen, dass er nur die Vectorelemente durchgehen soll, wo d gleich dem d in dem vector ist.
Ich hoffe mal, du nimmst dir noch ein wenig Zeit für mich!
<-- fänd ich das
-
linuxfan132 schrieb:
Ja wie gesagt, mir fehlt eben die Routine. Wie kann ich denn sonst die Daten aus der Datei in den vector einlesen? Mit den Pointern wollt ich doch nur den Speicher schonen! Lehr mich doch mal, scheinst ja echt Ahnung zu haben, von dem was du machst. Ich bin Maschinenbaustudent, kein Informatiker. Sag mir doch mal, wie man den Code verbessern kann, sodass das dann am Ende auch funktioniert.
PS: der Vergleich unten in der for-Scheleife soll heißen, dass er nur die Vectorelemente durchgehen soll, wo d gleich dem d in dem vector ist.
Ich hoffe mal, du nimmst dir noch ein wenig Zeit für mich!
<-- fänd ich daswieviel bist du denn bereit zu zahlen?
-
#include "stdafx.h" #include <cmath> #include <vector> #include <conio.h> #include <cstring> #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <stdexcept> using namespace std; //using namespace std; namespace my { template < typename Out , typename In > Out lexical_cast( In data) { Out tmp; std::stringstream sstr; sstr << data; if(!(sstr >> tmp)|| !sstr.good() || sstr.eof()) throw std::bad_cast(); return tmp; } } class Lagerdaten { public: explicit Lagerdaten(string LagerDatei):Datei(LagerDatei){} virtual ~Lagerdaten(){} void Einlesen(); std::size_t get_d_size() const { return d.size(); } int get_D(size_t idx) const; std::string const & get_typ(size_t idx) const; int get_d( size_t idx ) const { return d[idx]; } protected: std::vector< int > d; std::vector <float> e, dynC, statC, y, y0; std::string Datei; vector <int> D; vector <string> typ; }; template< typename Type > void MyInsert(std::ifstream & ifs , std::vector<Type> & vec ) { std::string tmp; getline(ifs , tmp , '\t' ); vec.push_back(my::lexical_cast<Type>(tmp)); } int Lagerdaten::get_D(size_t idx) const { return D[idx]; } std::string const & Lagerdaten::get_typ(size_t idx) const { return typ[idx]; } void Lagerdaten::Einlesen() { ifstream Lagerliste(Datei.c_str()); Lagerliste.seekg(ios::beg); std::string tmp; while (!Lagerliste.eof() && !Lagerliste.fail() ) { MyInsert(Lagerliste, d); MyInsert(Lagerliste, D); MyInsert(Lagerliste, dynC); MyInsert(Lagerliste, e); MyInsert(Lagerliste, y); MyInsert(Lagerliste, statC); MyInsert(Lagerliste, y0); getline(Lagerliste,tmp); typ.push_back(tmp); } } class dynBeanspruchung : public Lagerdaten { public: dynBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string const & LAGERDATEI) : Lagerdaten(LAGERDATEI), Fr(FR), Fa(FA), L(LL), n(N), axial(AXIAL) {} int SucheLagerDyn(int y); private: double p(int z); double Fr; double Fa; int L; int n; int axial; }; int dynBeanspruchung::SucheLagerDyn(int y) { Einlesen(); double CTRAG; int IndexOpt; CTRAG= pow( ( L * n * 60 / 1000000 ) , ( 3 / 10 ) ) * p(y) ; if (axial==2) CTRAG=CTRAG/1.715; if (dynC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } double dynBeanspruchung::p(int z) { double p; if ( (Fa/Fr)<=e[z] ) { if (axial==1) p = Fr; else p = Fr + 1.12 * y[z] * Fa; } else { if (axial==1) p=0,4*Fr+y[z]*Fa; else p= 0.67 * Fr + 1.68 * y[z] * Fa; } return p; } class statBeanspruchung : public Lagerdaten { public: statBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string const & LAGERDATEI) : Lagerdaten(LAGERDATEI), Fr(FR), Fa(FA), L(LL), n(N), axial(AXIAL) {} int SucheLagerStat(int y); private: double p(int z); double Fr; double Fa; int L; int n; int axial; }; int statBeanspruchung::SucheLagerStat(int y) { Einlesen(); double CTRAG; int IndexOpt; CTRAG=pow((L*n*60/1000000),(3/10))* p(y); if (axial==2) CTRAG=CTRAG/1.715; if (statC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } double statBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=(1/(2*y0[z]))) { if (axial==1) p=Fr; else p= 0.5 * Fr + y0[z] * Fa; } else { p = Fr + 2 * y0[z] * Fa; } return p; } int main() { double Fa, Fr; int axial, L, d, n, optLager; string LagerDatei = "lager.txt"; cout << "Lagerbestimmungsprogramm am Bsp. FAG Kegelrollenlager\n\n" << endl; cout << "Geben Sie die konstruktiven Merkmale an:" << endl; cout << "\n\nAxialkraft in kN: "; cin >> Fa; Fa=fabs(Fa); cout << "\nRadialkraft in kN: "; //cin << Fr; cin >> Fr; Fr=fabs(Fr); cout << "\n(1)einseitige oder (2)zweiseitige axiale Belastung?: "; do { cin >> axial; if(axial == 1 || axial == 2 ) break; } while(1); cout << "\nGewünschte Lebensdauer: "; cin >> L; cout << "\nWellendurchmesser: "; cin >> d; cout << "\nDrehzahl in min^-1: "; cin >> n; if(n<=10) { statBeanspruchung statBea(Fr, Fa, L, n, axial, LagerDatei); for(unsigned x =0; x<= statBea.get_d_size(); x++) if( statBea.get_d(x) == d ) optLager = statBea.SucheLagerStat(x); cout << "\n\nDas entsprechende Lager entspricht FAG" << statBea.get_typ(optLager) << " und hat eine Einbaudurchmesser D von " << statBea.get_D(optLager) << endl; } else { dynBeanspruchung dynBea(Fr, Fa, L, n, axial, LagerDatei); for(unsigned x=0; x<=dynBea.get_d_size(); x++) if(dynBea.get_d(x)==d) optLager = dynBea.SucheLagerDyn(x); cout << "\n\nDas entsprechende Lager entspricht FAG" << dynBea.get_typ(optLager) << " und hat eine Einbaudurchmesser D von " << dynBea.get_D(optLager) << endl; } getch(); return 0; }Es ist immer noch nicht das grüne vom Ei, aber ich hab keine Ahnung was du da berechnest. Das ist auch der Grund warum ich deins nur noch mal überarbeitet habe.
Ich wüsste nur gern wie du auf den Gedanken kommst ich sei Informatik-Student.
Ich bin weder Student noch habe ich Abitur oder einen IT-Beruf erlernt. Ich hab mir alles auch nur selbst bei gebracht....
MfG
-
evilissimo schrieb:
Es ist immer noch nicht das grüne vom Ei

-
Michael E. schrieb:
evilissimo schrieb:
Es ist immer noch nicht das grüne vom Ei

lol
hab ich gar net gecheckt *g*
-
So ich hab mir den Code mit eurer Hilfe nochmal verbessert. Ist soweit auch auführber, nur das er mir ein acccess volation ausspuckt. Er meint, wenn ich folgende Eingaben mache:
Fr=1
axial=1
Fr=1
d=25
n=3000
L=20000, dass optLager=-8xxxxxxxxx ist.
// Lagersoftware.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <cmath> #include <vector> #include <conio.h> #include <string> #include <iostream> #include <iomanip> #include <fstream> #include <boost/lexical_cast.hpp> using namespace std; class Lagerdaten { protected: vector <float> e, dynC, statC, y, y0; string Datei; public: vector <int> d; vector <int> D; vector <string> typ; Lagerdaten(string LagerDatei) { Datei=LagerDatei; } void Einlesen(); }; void Lagerdaten::Einlesen() { ifstream Lagerliste(Datei.c_str()); Lagerliste.seekg(ios::beg); std::string tmp; while (!Lagerliste.eof() && !Lagerliste.fail() ) { getline(Lagerliste,tmp,'\t'); d.push_back(boost::lexical_cast<int>(tmp)); getline(Lagerliste,tmp,'\t'); D.push_back(boost::lexical_cast<int>(tmp)); getline(Lagerliste,tmp,'\t'); dynC.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); e.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); y.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); statC.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp,'\t'); y0.push_back(boost::lexical_cast<float>(tmp)); getline(Lagerliste,tmp); typ.push_back(tmp); } } class dynBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: dynBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { FR=Fr; FA=Fa; LL=L; N=n; AXIAL=axial; } int SucheLagerDyn(int y); }; double dynBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=e[z]) { if (axial==1) p=Fr; else p=Fr+1.12*y[z]*Fa; } else { if (axial==1) p=0,4*Fr+y[z]*Fa; else p=0.67*Fr+1.68*y[z]*Fa; } return p; } int dynBeanspruchung::SucheLagerDyn(int y) { Einlesen(); double CTRAG; int IndexOpt=1; CTRAG=pow((L*n*60/1000000),(3/10))*p(y); if (axial==2) CTRAG=CTRAG/1.715; if (dynC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } class statBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: statBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { FR=Fr; FA=Fa; LL=L; N=n; AXIAL=axial; } int SucheLagerStat(int y); }; double statBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=(1/(2*y0[z]))) { if (axial==1) p=Fr; else p=0.5*Fr+y0[z]*Fa; } else { p=Fr+2*y0[z]*Fa; } return p; } int statBeanspruchung::SucheLagerStat(int y) { Einlesen(); double CTRAG; int IndexOpt=1; CTRAG=pow((L*n*60/1000000),(3/10))*p(y); if (axial==2) CTRAG=CTRAG/1.715; if (statC[y]<=CTRAG) IndexOpt=y; return IndexOpt; } int main() { double Fa, Fr; int axial, L, durchmesser, n, optLager; string LagerDatei = "lager.txt"; cout << "Lagerbestimmungsprogramm am Bsp. FAG Kegelrollenlager\n\n" << endl; cout << "Geben Sie die konstruktiven Merkmale an:" << endl; cout << "\n\nAxialkraft in kN: "; cin >> Fa; Fa=fabs(Fa); cout << "\n(1)einseitige oder (2)zweiseitige axiale Belastung?: "; do cin >> axial; while(!axial==1 || !axial==2); cout << "\nRadialkraft in kN: "; cin >> Fr; Fr=fabs(Fr); cout << "\nWellendurchmesser: "; cin >> durchmesser; cout << "\nDrehzahl in min^-1: "; cin >> n; cout << "\nGewünschte Lebensdauer: "; cin >> L; if(n<=10) { class statBeanspruchung statBea(Fr, Fa, L, n, axial, LagerDatei); for(int x=1; x<=statBea.d.size(); x++) { if(statBea.d[x]==durchmesser) optLager = statBea.SucheLagerStat(x); } cout << "\n\nDas entsprechende Lager entspricht FAG" << statBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << statBea.D[optLager] << endl; } else { class dynBeanspruchung dynBea(Fr, Fa, L, n, axial, LagerDatei); for(int x=1; x<=dynBea.d.size(); x++) { if(dynBea.d[x]==durchmesser) optLager = dynBea.SucheLagerDyn(x); } cout << "\n\nDas entsprechende Lager entspricht FAG" << dynBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << dynBea.D[optLager] << endl; } getch(); return 0; }Hier die Textdatei lager.txt!
15 35 15 0,35 1,73 14,3 0,95 30202A 15 42 23,2 0,29 2,11 20,8 1,16 30302A 17 40 19,3 0,35 1,74 19 0,96 30203A 17 40 29 0,31 1,92 30 1,06 32203A 17 47 28 0,29 2,11 25 1,16 30303A 17 47 36,5 0,29 2,11 36,5 1,16 32303A 20 42 24 0,37 1,6 29 0,88 32004X 20 47 27,5 0,35 1,74 27,5 0,96 30204A 20 52 34,5 0,3 2 33,5 1,1 30304A 20 52 31 0,73 0,82 30,5 0,45 31304 20 52 46,5 0,3 2 48 1,1 32304A 25 47 26,5 0,43 1,39 34 0,77 32005X 25 52 32,5 0,37 1,6 35,5 0,88 30205A 25 52 40,5 0,36 1,67 45 0,92 32205A 25 52 49 0,35 1,71 58,5 0,94 33205 25 62 47,5 0,3 2 46,5 1,1 30305A 25 62 38 0,83 0,73 39 0,4 31305A 25 62 63 0,3 2 65,5 1,1 32305A 28 52 34 0,43 1,39 40,5 0,77 320/28XWoran kann das liegen.
-
Was mir suspekt vorkommt ist
for(int x=1; x<=dynBea.d.size(); x++) { if(dynBea.d[x]==durchmesser) optLager = dynBea.SucheLagerDyn(x); }es sollte vielmehr
for(int x=0; x<dynBea.d.size(); x++) { if(dynBea.d[x]==durchmesser) optLager = dynBea.SucheLagerDyn(x); }heissen da bei einem vektor die indizes von 0 bis size() -1 gültig sind
Das selbe gilt natürlich auch bei der statischen beanspruchung.
Kurt
-
Weiß den keine, was hier der elementare Fehler ist?!?!?!
#include "stdafx.h" #include <cmath> #include <vector> #include <conio.h> #include <string> #include <iostream> #include <iomanip> #include <fstream> using namespace std; class Lagerdaten { protected: vector <int> d; vector <float> e, dynC, statC, y, y0; string Datei; public: vector <int> D; vector <string> typ; Lagerdaten(string LagerDatei) { Datei=LagerDatei; } void Einlesen(); }; void Lagerdaten::Einlesen() { int dtmp, Dtmp; float dynCtmp, etmp, ytmp, statCtmp, y0tmp; string typtmp; ifstream liste((char*) &Datei,ios::binary); liste.seekg(ios::beg); do { liste.getline((char*) &dtmp,sizeof(dtmp),'\t'); d.push_back(dtmp); liste.getline((char*) &Dtmp,sizeof(Dtmp),'\t'); D.push_back(Dtmp); liste.getline((char*) &dynCtmp,sizeof(dynCtmp),'\t'); dynC.push_back(dynCtmp); liste.getline((char*) &etmp,sizeof(etmp),'\t'); e.push_back(etmp); liste.getline((char*) &ytmp,sizeof(ytmp),'\t'); y.push_back(ytmp); liste.getline((char*) &statCtmp,sizeof(statCtmp),'\t'); statC.push_back(statCtmp); liste.getline((char*) &y0tmp,sizeof(y0tmp),'\t'); y0.push_back(y0tmp); liste.getline((char*) &typtmp,sizeof(typtmp)); typ.push_back(typtmp); } while (!liste.eof() && !liste.fail()); } class dynBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: dynBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { Fr=FR; Fa=FA; L=LL; n=N; axial=AXIAL; } int SucheLagerDyn(int durchmesser); }; double dynBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=e[z]) { if (axial==1) p=Fr; else p=Fr+1.12*y[z]*Fa; } else { if (axial==1) p=0.4*Fr+y[z]*Fa; else p=0.67*Fr+1.68*y[z]*Fa; } return p; } int dynBeanspruchung::SucheLagerDyn(int durchmesser) { Einlesen(); double CTRAG; int IndexOpt=-1; for(unsigned short int x=0; x<=d.size(); x++) { if(d[x]==durchmesser) { CTRAG=pow((L*n*60/1000000),(3/10))*p(x); if (axial==2) CTRAG=CTRAG/1.715; if (dynC[x]<=CTRAG) IndexOpt=x; } } return IndexOpt; } class statBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: statBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { FR=Fr; FA=Fa; LL=L; N=n; AXIAL=axial; } int SucheLagerStat(int y); }; double statBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=(1/(2*y0[z]))) { if (axial==1) p=Fr; else p=0.5*Fr+y0[z]*Fa; } else { p=Fr+2*y0[z]*Fa; } return p; } int statBeanspruchung::SucheLagerStat(int durchmesser) { Einlesen(); double CTRAG; int IndexOpt=-1; for(unsigned short int x=0; x<=d.size(); x++) { if(d[x]==durchmesser) { CTRAG=pow((L*n*60/1000000),(3/10))*p(x); if (axial==2) CTRAG=CTRAG/1.715; if (statC[x]<=CTRAG) IndexOpt=x; } } return IndexOpt; } int main() { double Fa, Fr; int axial, L, durchmesser, n; int optLager; string LagerDatei = "lager.txt"; cout << "Lagerbestimmungsprogramm am Bsp. FAG Kegelrollenlager\n\n" << endl; cout << "Geben Sie die konstruktiven Merkmale an:" << endl; cout << "\n\nAxialkraft in kN: "; cin >> Fa; Fa=fabs(Fa); cout << "\n(1)einseitige oder (2)zweiseitige axiale Belastung?: "; cin >> axial; cout << "\nRadialkraft in kN: "; cin >> Fr; Fr=fabs(Fr); cout << "\nWellendurchmesser: "; cin >> durchmesser; cout << "\nDrehzahl in min^-1: "; cin >> n; cout << "\nGewünschte Lebensdauer: "; cin >> L; if(n<=10) { class statBeanspruchung statBea(Fr, Fa, L, n, axial, LagerDatei); optLager = statBea.SucheLagerStat(durchmesser); if(optLager>=0) cout << "\n\nDas entsprechende Lager entspricht FAG" << statBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << statBea.D[optLager] << endl; else cout << "Fehler!" << endl; } else { class dynBeanspruchung dynBea(Fr, Fa, L, n, axial, LagerDatei); optLager = dynBea.SucheLagerDyn(durchmesser); if(optLager>=0) cout << "\n\nDas entsprechende Lager entspricht FAG" << dynBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << dynBea.D[optLager] << endl; else cout << "Fehler!" << endl; } getch(); return 0; }
-
Die funktion Lagerdaten::Einlesen() ist total falsch. ifstream::getline() liest einen string und den kannst du nicht einfach in ein integer oder float casten.
Die Version mit boost::lexical_cast war schon richtig.
Kurt
-
Selbst dann funktioniert es nicht. Probiert es doch mal bitte aus, die lager.txt hab ich doch oben schon geposted. Ich bin echt verzweifelt!!!!!

-
Habe ein paar fehler gefunden.
lager.txt hat ein falsches format. die tabs als trennzeichen zwischen den werten sind verschwunden ( möglicherweise durchs forum ). ausserdem sind die kommas im file beistriche. ( zumindest mein compiler will punkte ).15 35 15 0.35 1.73 14.3 0.95 30202A 15 42 23.2 0.29 2.11 20.8 1.16 30302A 17 40 19.3 0.35 1.74 19 0.96 30203A 17 40 29 0.31 1.92 30 1.06 32203A 17 47 28 0.29 2.11 25 1.16 30303A 17 47 36.5 0.29 2.11 36.5 1.16 32303A 20 42 24 0.37 1.6 29 0.88 32004X 20 47 27.5 0.35 1.74 27.5 0.96 30204A 20 52 34.5 0.3 2 33.5 1.1 30304A 20 52 31 0.73 0.82 30.5 0.45 31304 20 52 46.5 0.3 2 48 1.1 32304A 25 47 26.5 0.43 1.39 34 0.77 32005X 25 52 32.5 0.37 1.6 35.5 0.88 30205A 25 52 40.5 0.36 1.67 45 0.92 32205A 25 52 49 0.35 1.71 58.5 0.94 33205 25 62 47.5 0.3 2 46.5 1.1 30305A 25 62 38 0.83 0.73 39 0.4 31305A 25 62 63 0.3 2 65.5 1.1 32305A 28 52 34 0.43 1.39 40.5 0.77 320/28Xich habe noch ein paar logikfehler gefunden. sind in code als kommentar gekennzeichnet. Ich verstehe nicht wirklich was du da rechnest und wie diese liste zu lesen ist daher sihh dir meine änderungen noch mal an. Ausserdem habe ich ein paar debug-outputs eingefügt, das sollte bei der fehlersuche hilfreich sein.
#include <cmath> #include <vector> #include <string> #include <iostream> #include <iomanip> #include <fstream> using namespace std; class Lagerdaten { protected: vector <int> d; vector <float> e, dynC, statC, y, y0; string Datei; public: vector <int> D; vector <string> typ; Lagerdaten(string LagerDatei) { Datei=LagerDatei; } void Einlesen(); }; void Lagerdaten::Einlesen() { char typtmp[200]; ifstream liste(Datei.c_str()); // cast geht nicht liste.seekg(ios::beg); // warum binär ?? do { liste.getline( typtmp,sizeof(typtmp),'\t'); d.push_back(atoi(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); D.push_back(atoi(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); dynC.push_back(atof(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); e.push_back(atof(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); y.push_back(atof(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); statC.push_back(atof(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\t'); y0.push_back(atof(typtmp)); liste.getline( typtmp,sizeof(typtmp),'\n'); // hier bis end of line typ.push_back(typtmp); } while (!liste.eof() && !liste.fail()); // for ( int i= 0; i < d.size(); i++ ) { // cout << d[i] << "\t" << D[i] << "\t" << dynC[i] << "\t" << e[i] << "\t" << y[i] << "\t" << statC[i]; // cout << "\t" << y0[i] << "\t" << typ[i] << endl; // } } class dynBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: dynBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { Fr=FR; Fa=FA; L=LL; n=N; axial=AXIAL; cout << "dynBeanspruchung::dynBeanspruchung( Fr=" << Fr << " Fa=" << Fa << " L="<< L << " n=" << n << " axial=" << axial << ")" << endl; } int SucheLagerDyn(int durchmesser); }; double dynBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=e[z]) { if (axial==1) p=Fr; else p=Fr+1.12*y[z]*Fa; } else { if (axial==1) p=0.4*Fr+y[z]*Fa; else p=0.67*Fr+1.68*y[z]*Fa; } cout << "double dynBeanspruchung::p( z = " << z << " ) returns " << p << endl; return p; } int dynBeanspruchung::SucheLagerDyn(int durchmesser) { Einlesen(); cout << "dynBeanspruchung::SucheLagerDyn( durchmesser = " << durchmesser << " )" << endl; double CTRAG; int IndexOpt=-1; for(unsigned short int x=0; x < d.size(); x++) { // index 0 bis size -1 if(d[x]==durchmesser) { CTRAG=pow((double)(L*n*60/1000000),(3/10))*p(x); if (axial==2) CTRAG=CTRAG/1.715; cout << "CTRAG= " << CTRAG << " dynC[x]= " << dynC[x] << endl; // if (dynC[x]<=CTRAG) { if (dynC[x]>=CTRAG) { //gefunden wenn dynC > CTRAG ?? IndexOpt=x; cout << "returning " << typ[x] << endl; break; // ??? } } } return IndexOpt; } class statBeanspruchung : public Lagerdaten { double Fr, Fa; int L, n, axial; double p(int z); public: statBeanspruchung(double FR, double FA, int LL, int N, int AXIAL, string LAGERDATEI) : Lagerdaten(LAGERDATEI) { Fr=FR; Fa=FA; L=LL; n=N; axial=AXIAL; cout << "statBeanspruchung::statBeanspruchung( Fr=" << Fr << " Fa=" << Fa << " L=" << L << " n=" << n << " axial=" << axial << " )" << endl; } int SucheLagerStat(int y); }; double statBeanspruchung::p(int z) { double p; if ((Fa/Fr)<=(1/(2*y0[z]))) { if (axial==1) p=Fr; else p=0.5*Fr+y0[z]*Fa; } else { p=Fr+2*y0[z]*Fa; } cout << "double statBeanspruchung::p( z = " << z << " ) returns " << p << endl; return p; } int statBeanspruchung::SucheLagerStat(int durchmesser) { Einlesen(); cout << "statBeanspruchung::SucheLagerStat( durchmesser = " << durchmesser << " )" << endl; double CTRAG; int IndexOpt=-1; for(unsigned short int x=0; x < d.size(); x++) { // index 0 bis size -1 if(d[x]==durchmesser) { CTRAG=pow(( double )(L*n*60/1000000),(3/10))*p(x); if (axial==2) CTRAG=CTRAG/1.715; cout << "CTRAG= " << CTRAG << " dynC[x]= " << dynC[x] << endl; // if (statC[x]<=CTRAG) { if (dynC[x]>=CTRAG) { //gefunden wenn dynC > CTRAG ?? IndexOpt=x; cout << "returning " << typ[x] << endl; break; // don't know ?? } } } return IndexOpt; } int main() { double Fa, Fr; int axial, L, durchmesser, n; int optLager; string LagerDatei = "lager.txt"; cout << "Lagerbestimmungsprogramm am Bsp. FAG Kegelrollenlager\n\n" << endl; cout << "Geben Sie die konstruktiven Merkmale an:" << endl; cout << "\n\nAxialkraft in kN: "; cin >> Fa; Fa=fabs(Fa); cout << "\n(1)einseitige oder (2)zweiseitige axiale Belastung?: "; cin >> axial; cout << "\nRadialkraft in kN: "; cin >> Fr; Fr=fabs(Fr); cout << "\nWellendurchmesser: "; cin >> durchmesser; cout << "\nDrehzahl in min^-1: "; cin >> n; cout << "\nGewünschte Lebensdauer: "; cin >> L; if(n<=10) { class statBeanspruchung statBea(Fr, Fa, L, n, axial, LagerDatei); optLager = statBea.SucheLagerStat(durchmesser); if(optLager>=0) cout << "\n\nDas entsprechende Lager entspricht FAG" << statBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << statBea.D[optLager] << endl; else cout << "Fehler!" << endl; } else { class dynBeanspruchung dynBea(Fr, Fa, L, n, axial, LagerDatei); optLager = dynBea.SucheLagerDyn(durchmesser); if(optLager>=0) cout << "\n\nDas entsprechende Lager entspricht FAG" << dynBea.typ[optLager] << " und hat eine Einbaudurchmesser D von " << dynBea.D[optLager] << endl; else cout << "Fehler!" << endl; } return 0; }viel spass Kurt
-
sehr viele unsigned signed mischungen
-
nicht gut

-
fürs verschönern fühl ich mich nicht zuständig.

Kurt