#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

const char *ztincrements[256] = {"000", "001", "002", "003", "005", "006", "007", "008", "010", "011", "012", "013", "015", "016", "017", "018", "020", "021", "022",
				"023", "025", "026", "027", "028", "030", "031", "032", "033", "035", "036", "037", "038", "040", "041", "042", "043", "045", "046",
				"047", "048", "050", "051", "052", "053", "055", "056", "057", "058", "060", "061", "062", "063", "065", "066", "067", "068", "070",
				"071", "072", "073", "075", "076", "077", "078", "080", "081", "082", "083", "085", "086", "087", "088", "090", "091", "092", "093",
				"095", "096", "097", "098", "100", "101", "102", "103", "105", "106", "107", "108", "110", "111", "112", "113", "115", "116", "117",
				"118", "120", "121", "122", "123", "125", "126", "127", "128", "130", "131", "132", "133", "135", "136", "137", "138", "140", "141",
				"142", "143", "145", "146", "147", "148", "150", "151", "152", "153", "155", "156", "157", "158", "160", "161", "162", "163", "165",
				"166", "167", "168", "170", "171", "172", "173", "175", "176", "177", "178", "180", "181", "182", "183", "185", "186", "187", "188",
				"190", "191", "192", "193", "195", "196", "197", "198", "200", "201", "202", "203", "205", "206", "207", "208", "210", "211", "212",
				"213", "215", "216", "217", "218", "220", "221", "222", "223", "225", "226", "227", "228", "230", "231", "232", "233", "235", "236",
				"237", "238", "240", "241", "242", "243", "245", "246", "247", "248", "250", "251", "252", "253", "255", "256", "257", "258", "260",
				"261", "262", "263", "265", "266", "267", "268", "270", "271", "272", "273", "275", "276", "277", "278", "280", "281", "282", "283",
				"285", "286", "287", "288", "290", "291", "292", "293", "295", "296", "297", "298", "300", "301", "302", "303", "305", "306", "307",
				"308", "310", "311", "312", "313", "315", "316", "317", "318"};

//2000 per point per contract
//one point represents 1% of Face value at maturity of $200,000
const double usdpersub = 7.8125;
const double usdperpoint = 2000;
const double rndtrpcomm_percontract = 5.74;
/*
int increments_from(int sub_entry, int sub_exit){
	int diff = 0;
	int entry, exit;

	while(


	if(sub_exit > sub_entry){
		while(
	}
}
*/

// DECIMAL to ZT increments

//One-eighth of one thirty-second (1/32) of one point ($7.8125, rounded to the nearest cent per contract), including intermonth spreads.



// ./zt contracts entry multiple
// output: -1x 0 1x 2x 3x 4x 5x 6x 7x 8x 9x 10x
// ./zt 39 107.241 2500


int main(int argc, char **argv){
/*
	int num1 = 0;
	int num2 = 0;
	float tmp = 0;
	float input = atof(argv[1]);
	int dig_a;
	int dig_b;
	int output;
	float input_orig = input;
	input -= (int)input/1;
	while(input>lookup[num1])
		num1++;
	if(input == 1)
		num1 = 1;
	printf("%f = %s \n", input_orig, ztincrements[num1]);
*/
	int contracts   = atoi(argv[1]);


	int point_entry; //= atoi(argv[2]);
	char sub_entry[4];   //= atoi(argv[3]);

	//int point_exit;  //= atoi(argv[4]);
	//char sub_exit[4];    //= atoi(argv[5]);

	int points_diff = 0;
	double usd_change = 0;
	char entry_c[8];
	//char exit_c[8];
	char temp1[4];
	char temp2[4];
	int count1=0;
	int count2=0;
	int sub_diff=0;
	double usd_tmp1 = 0;
	double multiple = atof(argv[3]);
/*
argv1: 39 argv2: 107241 107233 argv3: (null)
printf("argv1: %s argv2: %s argv3: %s\n", argv[1], argv[2], argv[3]);

argv2: 107241 107233

107241 107233
*/

	memset ((void*)entry_c, '\0', sizeof(entry_c));
	//memset ((void*)exit_c, '\0', sizeof(exit_c));

	(void)strncpy(entry_c, (const char *)argv[2], 7);
	//(void)strncpy(exit_c, (const char *)argv[3], 7);

//printf("here1\n");
	memset ((void*)temp1, '\0', sizeof(temp1));
	memset ((void*)temp2, '\0', sizeof(temp2));
	//100'103 first 3 chars=point   ' separator  next 3 chars=sub
	while(count2<3){
		temp1[count2] = entry_c[count2];
		//temp2[count2] = exit_c[count2];
		count2++;
	}
	//skip '
	count2++;
	point_entry = atoi(temp1);
	//point_exit = atoi(temp2);

	memset((void*)sub_entry, '\0', sizeof(sub_entry));
	//memset((void*)sub_exit, '\0', sizeof(sub_exit));
	while(count2<7){
		sub_entry[count1] = entry_c[count2];
		//sub_exit[count1] = exit_c[count2];
		count1++;
		count2++;
	}
	


	//points_diff = point_entry>point_exit ? point_entry-point_exit : point_exit-point_entry;

//	if(point_exit>point_entry){
		points_diff = point_exit-point_entry;
		usd_change = points_diff*usdperpoint*contracts;
//	}else{
//		points_diff = point_exit-point_entry;
//		usd_change = points_diff*usdperpoint*contracts;
//	}
//printf("here2\n");
	//ztincrements[256]
	count1=0;
	count2=0;
	while(strcmp((const char *)sub_entry, (const char *)ztincrements[count1]) != 0){
		count1++;
		//count2++;
	}
	//count2=0;
	while(strcmp((const char *)sub_exit, (const char *)ztincrements[count2]) != 0){
		count2++;
	}
//printf("here3\n");
	sub_diff = count2-count1;
	//sub_diff = increments_from(sub_entry, sub_exit);


	//sub_diff*usdpersub needs to be rounded to the nearest cent
	usd_tmp1 = roundf(sub_diff*usdpersub * 100) / 100;

	usd_change += usd_tmp1*contracts;


	printf("points diff: %d\n", points_diff);
	printf("sub diff: %d\n", sub_diff);
	printf("usd_change: %lf\n", usd_change);

	return 0;
}
