// C++ program to create calculator using
// switch statement
#include <iostream>
#include<unistd.h>
#include "nd_sdk.h"
#include<string>
#include<map>
using namespace std;

static void __attribute__((constructor)) calc_constructor(void) {
	nd_init2((char*)"cppAgent", (char*)"calculator",NULL,NULL);
}

ndBTHandle bthandle = NULL;
int ii=0;
void Displaysum(float num1, float num2) {
	nd_method_entry(bthandle,(char*)"pkg1.cls1.Displaysum",0);
	cout << "sum " << num1 + num2<<endl;
	if(ii == 5) {
		sleep(10);
		ii=0;
	}
	else
		sleep(1);
	ii++;
	nd_method_exit(bthandle,(char*)"pkg1.cls1.Displaysum",0,0,0,NULL);
}
void Displaydiff(float num1, float num2) {
	nd_method_entry(bthandle,(char*)"pkg1.cls1.Displaydiff",0);
	cout << "diff "<<num1 - num2<<endl;
	sleep(1);
	nd_method_exit(bthandle,(char*)"pkg1.cls1.Displaydiff",0,0,0,NULL);
}
void Displayproduct(float num1, float num2) {
	nd_method_entry(bthandle,(char*)"pkg1.cls1.Displayproduct",0);
	cout << "prod " << num1 * num2<<endl;
	sleep(1);
	nd_method_exit(bthandle,(char*)"pkg1.cls1.Displayproduct",0,0,0,NULL);
}
void Displaydiv(float num1, float num2) {
	nd_method_entry(bthandle,(char*)"pkg1.cls1.Displaydiv",0);
	cout << "div " << num1 / num2<<endl;
	sleep(1);
	nd_method_exit(bthandle,(char*)"pkg1.cls1.Displaydiv",0,0,0,NULL);
}
// Main program
int main()
{
	map<string,int> testmap;
	testmap["1"]=1;
	string x="2";
	testmap[x]=2;
	string y;
	map<string, int>::iterator thread_itr = testmap.find(y);
	if(thread_itr != testmap.end())
		fprintf(stderr, "found in map\n");
	testmap[y]=3;
	fprintf(stderr, "not found in map\n");
	int num_operations = 5;
	char *unique_bt_id_c = (char*)"1";
	float num1 = 50, num2 = 20;
	for(int i = 0;i < 2; i++) {
		bthandle = nd_bt_begin((char*)"arthmetic_operation",NULL,NULL,NULL,NULL,NULL,0);
		nd_bt_store(bthandle, unique_bt_id_c);

		for(int j = 0; j < num_operations; j++) {
			// Switch statement begins
			switch (j) {
				
				// If user enter +
				case 0:
				case 4:
				case 8:
					Displaysum(num1, num2);
					sleep(1);
					break;
				
				// If user enter -
				case 1:
				case 5:
				case 9:
					Displaydiff(num1, num2);
					sleep(1);
					break;
				
				// If user enter *
				case 2:
				case 6:
				case 10:
					Displayproduct(num1, num2);
					sleep(1);
					break;
				
				// If user enter /
				case 3:
				case 7:
				case 11:
					Displaydiv(num1,num2);
					sleep(1);
					break;
				
				// If the operator is other than +, -, * or /,
				// error message will display
				default:
					cout << "Error! operator is not correct";
					break;
			} // switch statement ends
		}
		nd_bt_end(bthandle,0,0,0,0,NULL);

		num_operations++;
		num1++;
		num2++;
		if(num_operations > 10)
			num_operations = 5;
		sleep(1);
	}

	return 0;
}

