All pastes #188356 Raw Edit

Someone

public cpp v1 · immutable
·published 2006-10-02 00:52 UTC
rendered paste body
//Paragwgh tyxaiwn arithmwn kanonikhs katanomhs me Monte-Carlo methodo.//To aoristo oloklhrwma ths f(x):Gauss den yparxei kai efarmozw monte-carlo.#include<iostream>#include<fstream>#include<cstdlib>#include<cmath>#include<time.h>#include<string>#define EXEI_ELLHNIKO_EXCELusing namespace std;int main(){     const int a=0,b=1000;  double x,y,f,m,s;  string outputname;  char* buffer = 0;  int i=0,j;  cerr<<"\n PARGWGH TYXAIWN ARITHMWN KANONIKHS KATANOMHS"<<endl<<endl;  cerr<<"   Dwse thesh koryfhs              : "; cin>>m; while (cin.peek() != '\n') cin.ignore(); cin.ignore();  cerr<<"   Dwse diaspora                   : "; cin>>s; while (cin.peek() != '\n') cin.ignore(); cin.ignore();  cerr<<"   Plhthos tyxaiwn arithmwn        : "; cin>>j; while (cin.peek() != '\n') cin.ignore(); cin.ignore();  buffer = new char[2000];  cerr<<"   Dwse arxeio e3odou (enter=o8onh): "; cin.getline(buffer, 1999);  outputname = buffer;  delete[] buffer; buffer = 0;  cerr<<endl;  // Proetoimasia arxeiou e3odou.  ofstream file;  if (outputname != "")  {	  if (outputname.rfind(".csv") == -1)		  outputname += ".csv";	  file.open(outputname.c_str());	  if (!file)	  {		  cerr << "LA8OS: De mporw na anoi3w auto to arxeio (" << outputname << ")." << endl;		  exit(-1);	  }  }  srand((unsigned int)time(0));  buffer = new char[2000];  while(i<j){  //tha paraksw tyxaious omoiomorfhs katanomhs sto (m-4s,m+4s)     x=(float)(rand()%(b-a)+a)/1000.0;//x :tyxaio omoiomorfhs katanomhs sto (0,1)    x=(x-0.5)*8*s+m;//to (0,1) metaferetai sto (-0.5,0.5) kai diasteletai kata                     // 8 diaspores ,meta metaferetai gyro apo thn mesh timh m.  //Grafw thn f(x):Gauss synarthsei twn m,s      f=(pow(2.718281828,-(pow((x-m)/s,2))/2)) / (s*(sqrt(2*3.141592654)));  //Zari... y tyxaio omoiomorfhs katanomhs sto (o , f max)    y=(float)(rand()%(b-a)+a)/1000.0;  //y sto (0,1)    y=y/(s*(sqrt(2*3.1415926)));       //y sto (0 , f max)      if(f>=y)	  {		  sprintf(buffer, "%f", x);#ifdef EXEI_ELLHNIKO_EXCEL		  // Alla3e thn upodiastolh apo "." se ",".		  char* p = buffer; while(*p) {if (*p == '.') *p = ','; p++;}#endif		  if (file) file << buffer << endl;		  else cout << buffer << endl;	  }	  i++;  }  delete[] buffer;  // Kleinei to arxeio.  if (file)	  file.close();return 0;}