/****   Copyright (c) 2002  Alexandros V. Gerbessiotis
 ****
 ****   Permission to use, copy, modify, and distribute this software,
 ****   and to incorporate it, in whole or in part, into other software,
 ****   is hereby granted without fee, provided that
 ****     (1) the above copyright notice and this permission notice appear in
 ****         all copies of the source code, and the above copyright notice
 ****         appear in clearly visible form on all supporting documentation
 ****         and distribution media, and
 ****     (2) any redistribution of the software, in original
 ****         or modified form, be without fee and subject to
 ****         these same conditions.
 ****   No guarantee, whatsoever, is offered that the code works as
 ****   advertised or is absent of any, even damaging,
 ****   side-effects. Use this code at your own personal risk.
 ****/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include "option.h"
extern clock_t clock();

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;
 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);
 N= atoi(argv[1]);


 dx= sigma*sqrt(3.0*T/N);
/*
*/
 fprintf(stdout, "K=%f T=%f S=%f r=%f N=%d, sigma=%f dx=%f\
                  dividend=%f \n",
                  K,T,S,r,N,sigma,dx,dividend); 
 fflush(stdout);
 rst=rc=0.0;
 t1=clock();
/*
*/
 result=option(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,EUROPEANCALL);
 t2=clock();
 fprintf(stdout,"EuropeanCallOption= %14.9f (S= %f)\n",rc,rst);
 fprintf(stdout,"ECallOptionRunTime= %f seconds\n",
         (t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 rst=rc=0.0;
 t1=clock();
/*
*/
 result=option(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,EUROPEANPUT);
 t2=clock();
 fprintf(stdout,"EuropeanPutOption= %14.9f (S= %f)\n",rc,rst);
 fprintf(stdout,"EPutOptionRunTime= %f seconds\n",
         (t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 rst=rc=0.0;
 t1=clock();
/*
*/
 result=option(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,AMERICANCALL);
 t2=clock();
 fprintf(stdout,"AmericanCallOption= %14.9f (S= %f)\n",rc,rst);
 fprintf(stdout,"ACallOptionRunTime= %f seconds\n",
         (t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);

 rst=rc=0.0;
 t1=clock();
/*
*/
 result=option(K,T,S,r,N,sigma,dx,dividend,&rst,&rc,AMERICANPUT);
 t2=clock();
 fprintf(stdout,"AmericanPutOption= %14.9f (S= %f)\n",rc,rst);
 fprintf(stdout,"APutOptionRunTime= %f seconds\n",
         (t2-t1)/((double)CLOCKS_PER_SEC));
 fflush(stdout);


}
