#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <limits.h>
#include "option.h"

static struct timeval _tstart, _tend;
static struct timezone _tstruct;

void   avg_tstart  (void);
void   avg_tend    (void);
double avg_treturn (void);

void avg_tstart(void) {
  gettimeofday(&_tstart, &_tstruct);
}

void avg_tend(void) {
  gettimeofday(&_tend,&_tstruct);
}

double avg_treturn(void) {
  double t1,t2;
  t1=(double)_tstart.tv_sec + (double)_tstart.tv_usec/(1000*1000);
  t2=(double)_tend.tv_sec + (double)_tend.tv_usec/(1000*1000);
  return(t2-t1);
}




void check_if_null(void *x,char *s,char *t)
{
  if (NULL == x) {
     printf("%s::Null pointer (%s)\n",s,t);
     abort();
  }
}

int main(int argc, char **argv)
{
 double K, S,r,u,d,sigma,dx,dividend;
 int    N,Nj;
 double T;
 double result;
 double rst, rc;
 double t1,t2;

 if (argc !=2 ) {
  fprintf(stderr,"%s horizon datafile-from-stdinput\n",argv[0]);
  return(1);
 }
 scanf("%lf",&K);
 scanf("%lf",&T);
 scanf("%lf",&S);
 scanf("%lf",&r);
 scanf("%d",&N);
 scanf("%lf",&sigma);
 scanf("%lf",&dx);
 scanf("%lf",&dividend);
 scanf("%d",&Nj);
 N= atoi(argv[1]);
 Nj=N;

 dx= sigma*sqrt(3.0*T/N);
/*
*/
 fprintf(stdout, "K=%f T=%f S=%f r=%f N=%d, Nj=%d sigma=%f dx=%f\
                  dividend=%f \n",
                  K,T,S,r,N,Nj,sigma,dx,dividend); 
 fflush(stdout);
 rst=rc=0.0;
 avg_tstart();
/*
 result= exfdm(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,EUROPEANCALL,Nj);
*/
 avg_tend();
 fprintf(stdout,"EuropeanCallOption= %f (S= %f)\n",rc,rst);
 fprintf(stdout,"ECallOptionRunTime= %f seconds\n",
         avg_treturn()                   );
 fflush(stdout);

 rst=rc=0.0;
 avg_tstart();
/*
 result= exfdm(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,EUROPEANPUT,Nj);
*/
 avg_tend();
 fprintf(stdout,"EuropeanPutOption= %f (S= %f)\n",rc,rst);
 fprintf(stdout,"EPutOptionRunTime= %f seconds\n",
         avg_treturn()                   );
 fflush(stdout);

 rst=rc=0.0;
 avg_tstart();
/*
 result= exfdm(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,AMERICANCALL,Nj);
*/
 avg_tend();
 fprintf(stdout,"AmericanCallOption= %f (S= %f)\n",rc,rst);
 fprintf(stdout,"ACallOptionRunTime= %f seconds\n",
         avg_treturn()                   );
 fflush(stdout);

 rst=rc=0.0;
 avg_tstart();
/*
*/
 result= exfdm(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,AMERICANPUT,Nj);
 avg_tend();
 fprintf(stdout,"AmericanPutOption= %18.8f (S= %18.8f)\n",rc,rst);
 fprintf(stdout,"APutOptionRunTime= %f seconds\n",
         avg_treturn()                   );
 fflush(stdout);


}
