// Name:  McCabe, Micheal H.
// Date:  March 18, 2009
// Project:   Simple_Calculator_V5
// Filename:  main.cpp

// File Edit History:
//
// File created on 03/11/2009 as a monolithic c++ program.
// Additional work done on 03/12 and 03/13.  Additional in-class
// coding examples added on 03/16.  Changed function Help_Function to Helpscreen
// based on directions in Assignment 17.  Added function IsDigit and ReadInputFile
// based on in-class assignment 03/17.  More work on 03/18 to add WriteOutputFile.
// 
// On 03/18/2009 program was broken up into individual modules to simplify
// editing and minimize the possible effects of a VIM catastrophe.

#include "calc_headers.h"

// Global variables

float Data[MAX_USER_LIST];

// Main Routine

int main()
{
bool command_loop = true;
char cmd_chr1;
char cmd_chr2;
string temp;
int elements_read=0; //This value is returned by ReadInputFile
int i=0; //Just an index variable I'll use to debug the file i/o
float V1=0;
float V2=0;
float result=0;
int iresult=0;

while (command_loop)
	{
    temp="";
    cout << CMD_PROMPT;
	cin.get(cmd_chr1);
	cin.get(cmd_chr2);
	
	// Decode it
	
	switch (cmd_chr1)
	{
		case 'e':
			enterN1N2(Data);
			break;
		case 'i':
			temp = HELP_i;
			break;
		case 'f':
             if (cmd_chr2 == 'i')
                {
                elements_read=ReadInputFile(START_USER_DATA, Data);
                //This loop just displays the data we read from the file to check
                //for functionality.  Can be discarded when program is working.
                if (elements_read != 0)
                   {cout << elements_read << " items read from file..." << endl;
                   for (i=START_USER_DATA; i<=Data[0]+START_USER_DATA-1; i++)
                       {
                       cout << i << ": " << Data[i] << endl;
                       }
                   }
                   }
             else if (cmd_chr2 == 'o')
                  WriteOutputFile(START_USER_DATA, Data);
                  else temp=NO_COMMAND;
             break;
        case '+':
             V1=Data[1];
             V2=Data[2];
             result=adder(V1,V2);
             cout << "Result:  " << result << endl;
             break;
        case '-':
             V1=Data[1];
             V2=Data[2];
             result=subtractor(V1,V2);
             cout << "Result:  " << result << endl;
             break;
        case '*':
            V1=Data[1];
             V2=Data[2];
             result=multiplier(V1,V2);
             cout << "Result:  " << result << endl;
             break;
        case '/':
             V1=Data[1];
             V2=Data[2];
             result=divider(V1,V2);
             cout << "Result:  " << result << endl;
             break;
        case '^':
             V1=Data[1];
             V2=Data[2];
             result=exponentiator(V1,V2);
             cout << "Result:  " << result << endl;
             break;
        case '!':
             V1=Data[1];
             iresult=factorial((int)V1);
             cout << "Result:  " << iresult << endl;
             break;
        case 'c':
             cout << HELP_c;
             cout << HELP_cL;
             temp="";
             break;
        case 'h':
             helpscreen();
             temp = "";
             break;
        case 'l':
             list_data(Data);
             break;
        case 'q':
             command_loop = false;
             temp = "Bye, Bye!";
             break;
        case 'o':
             temp = HELP_o;
             break;
        case 'a':
             result=mean(Data);
             cout << "Result:  " << result << endl;
             break;
        case 'd':
             temp = HELP_d;
             if (IsDigit(cmd_chr2))
                ;
             break;
        case 'm':
             temp = HELP_m;
             break;
        case 's':
             if (cmd_chr2=='\n')
                {
                result=sum(Data);
                cout << "Result:  " << result << endl;
                }
             else if (cmd_chr2 == 'd')
                     {
                     result=standard_deviation(Data);
                     cout << "Result:  " << result << endl;
                     }
                     else temp="No Command.";
             break;
        case 'p':
             result=list_product(Data);
             cout << "Result:  " << result << endl;
             break;
        case 'M':
             if (cmd_chr2 == '1')
                temp=HELP_M1;
                else if (cmd_chr2 == '2')
                     temp=HELP_M2;
                     else temp=NO_COMMAND;
             break;
        case 'N':
              if (cmd_chr2 == '1')
                temp=HELP_N1;
                else if (cmd_chr2 == '2')
                     temp=HELP_N2;
                     else temp=NO_COMMAND;
             break;
		default:
			temp = "NO_COMMAND \n";
	}		
	cout << temp;
	
	if (cmd_chr2!='\n')
	   	cin.ignore (100, '\n');
	}
}

void clearn()
{
     N1=0;
     N2=0;
     return;
}

void clearl()
{
     int i;
     for (i=0; i<=MAX_USER_LIST; i++)
         Data[i]=0;
     return;
}
