Probleme mit der berechnung in Valarry + funktion
-
Hallo habe ein Programm geschrieben das eine Fläche berechnen soll.
Dies geht so weit aber ich möchte gerne die berechnung von delta X und das Result (res) in einer Funktion schreiben.
Könnt ihr mir vieleicht helfen.
Danke und gruss Turbohawk#include <iostream> #include <fstream> #include <valarray> #include <cmath> typedef double Tv; using namespace std; //##################----fnc ermitteln der Feldweite----############################ template<class T> inline T& fnc_fw(T& fw){ char *ch=0; //wird für Typecast benötigt char s; char str[2]; fw=0; //Feldweite auf null setzen ifstream fin("test.dat"); if (!fin){cout << "Eingabedatei nicht gefunden!";exit(1);} for(int e=0;!fin.eof();e++) { str[0]=s; str[1]=0; ch=&str[0]; fin >> ch; fw++;//Stream an ch }; fin.close(); cout << "Feldweite von x,y in fnc_fw: "<< fw/2 << endl; fw=fw/2; // Dividiert durch 2 (Koordinatenpaare) return(fw); } //##################----fnc Werte von Datei einlesen----############################ template<class T> inline T& fnc_fread(T& x, T& y){ char *ch=0; //wird für Typecast benötigt char s; char str[2]; ifstream in("test.dat"); if (!in){cout << "Eingabedatei nicht gefunden!";exit(1);} for(int i=0;!in.eof();i++) { str[0]=s; str[1]=0; ch=&str[0]; in >> ch; //Stream an ch x[i]= atof(ch); //Typ char in double; in >> ch; //Stream an ch y[i] = atof(ch);//Typ char in double; //cout << ch; };//end for in.close();//schliessen der Datei return(x,y); } //#######################------MAIN-----------############################################### void main() { int fw=0,i=0; fnc_fw(fw);//fnc ermittelt Feldweite size_t fw1=fw;// !!!!! fw GIBT PROBLEM wenn nicht initialisiert. valarray <Tv> x(0.0,fw),y(x); Tv dx; //Datentyp: double dx Tv res; fnc_fread(x,y);//fnc Werte von Datei einlesen cout << "Feldweite in main: " << fw << endl; cout << endl <<"x.Feldweite: " << x.size() << endl; for (i=0;i<x.size();++i) cout << "x[" <<i<< "]: " << x[i] << " #### y[" <<i<< "]: " << y[i]<< endl; //fnc_dx(x,x.size());//fnc zur berechnung von Delta X //fnc_res(dx,y,x.size());//fnc zur berechnung von Delta X //----------Berechnung dx noch in Funktionen schreiben!!!!! --------------------- dx=((x[x.size()-1]-x[0]) / (x.size()-1)); res = dx *(y.sum()-(y[0]-y[y.size()-1])/2.0); cout << endl <<"Feldweite: " << x.size() << endl; cout << endl <<"Delta-X: " << dx << endl; cout << endl <<"Gesamtflaeche: " << res << endl; } //end Main#############################################################################################p.s: die Datei Test.dat hat folgendes aussehen:
0.0 1.0 2.0 1.1 2.124 1.2 3.3 1.3 3.51u.s.w
nur werte die einlesen werden.
-
und was hat das mit der MFC zu tun ?????