'ReCalc.CPP'

( Error Stop ~V2t ~ setprecision(places) )

! Add lt < 'Tags' (after #include) to Compile. !

 
  
  
  
//----------------------  Auto Trig ~ 36 Memories  ---------------------------------

#include stdio.h>      //Add ALL lt (less-than) 'Tags' ( NO SHOW! in HTML )
#include stdlib.h>
#include string.h>
#include iostream.h>
#include ctype.h>                          // For toupper()
#include fstream.h>                      // For file I/O
#include iomanip.h >                     // For setw(), setprecision()
#include conio.h>                          // For getch()
#include math.h>                          // for sqrt(x)  'Q' + '^'
//----------------------------------------------------------------------------------

char        tag, chi, ch5, ch6, op, equal;
long double no1, no2, no3, ans, newAns;
long double Adj, Opp, Hyp, Deg, 	netSquares, mem[37], Pi = 3.14159265358979;
const   int TRUE = 1, FALSE = 0, MAX = 55;
int    count, line = 0, start = TRUE, getAns = FALSE, places = 9;
char   units1[MAX], units2[MAX], unitsAns[MAX], comment[MAX*3],
           S1[9], S2[9], S3[9], S4[9], sIN[MAX], units5[MAX], units6[MAX];


void begin();          // Forward refs. prototypes
void reCalc();
void trimUnits();
void divZero();
void noOp();
void autoTrig();
void AdjOpp();
void AdjHyp();
void OppHyp();
void AdjDeg();
void OppDeg();
void HypDeg();
void DegMinSec();
int  char10(char);

ofstream outFile;		  // For file out/in
ifstream inFile;
char fileName[MAX];

int main()
{
	begin();
 	cout    << setprecision(places);     // max. setprecision(18)
 	outFile << setprecision(places);
 	reCalc();
	return 0;
}
void begin()
{
	outFile.open("ReCalc.txt",  ios::app);      // append to file
	
 	cout << "         ****  ReCalc.exe  **** "  << endl;
	cout << "             setprecision(" << places << ")" << endl << endl;

 	cout << " UnitCalc History Script Calculation Program" << endl;
 	cout << "        ! File Edit Numbers ONLY ! " << endl << endl;

 	cout << "     OPEN (Write) File: 'ReCalc.txt'" << endl << endl;

 	cout << "     To ReCalc 'ReCalcDo.txt' ~ Press Return." << endl << endl;
 	cout << "     or ..." << endl << endl;
 	cout << "     Enter 'I' to Input UnitCalc History (Read) File Name." ;
 	cout << endl << endl << " ";
 	char in = getch();
 	if(in == 'I' || in == 'i')
 	{
		cout << endl << " UnitCalc History (Read) File Name: ";
		cin    >> fileName;
		inFile.open(fileName);
		if (inFile.fail())
		{
			cerr << endl << " ! NO file: '" << fileName
			<< "' Check in 'ReCalc.exe' Folder." << endl;
			cout << endl << endl << "      Enter... to Close/Quit.  ";
			getch();
			exit(1);
		}
	 }
	 else
	 {
		inFile.open("ReCalcDo.txt");
		if (inFile.fail())
		{
			cerr << endl << " ! NO file: '" << "ReCalcDo.txt"
			        << "' Check in 'ReCalc.exe' Folder." << endl;
			cout << endl << endl << "      Enter... to Close/Quit.  ";
			getch();
			exit(1);
		}
	 }	
}
void reCalc()
{
	while(! inFile.eof())
	{
		line++;
		if(count++ > MAX*7) break;       // NO RunAway!
		inFile >> tag;
		if(tag == '"')                   // Blank Line
		{
			inFile.ignore(222, '\n');
			continue;        				    // Next Line : goto endLineInOut;
		}
		else if(tag == '>')                // Print Comments
		{
			inFile.getline(comment, MAX*2);
			outFile  << comment << endl;
			continue;
		}
		else if(tag == '^' || tag == '_')	 // '^' Still here ~ Blank Line
		{
			outFile << endl;
			inFile.ignore(222, '\n');
			continue;
		}
		else if(tag == '!')                  // Start New Calculation ~ Blank Line
		{
			outFile << endl;		
			start  = TRUE;
			continue;
		}
		else if(tag == '{')
		{
			autoTrig();
			continue;
		}
		else if(tag == 'I' || tag == 'i')    // InPut mem value ~ unitsNote
		{
			inFile >> chi >> no3 ; 
			if(no3 == 0) inFile.ignore(222, '\n');
			if(! inFile)
			{
			     cout << endl << " . Bogus InPut Format..."  << endl;
			     outFile << endl << " ? Bogus InPut Format ?" << endl << endl;
			     getch();
			     exit(1);
			}
			mem[char10(chi)] = no3;
			if(no3 != 0)
			{
			  inFile >> sIN;
			  if(strlen(sIN) == 1)
			  {
			    if(sIN[0] == 'I' || sIN[0]== 'i' || sIN[0] == '_' || sIN[0] == '!')
			    {
			        cout << endl << " Bogus InPut: NO unitsNote"  << endl;
			        outFile << endl << " Bogus InPut: NO unitsNote" << endl << endl;
			        getch();
			        exit(1);
			    }
			}
			inFile.ignore(222, '\n');
			outFile << " InPut:  " << chi << " = " << no3 << " " << sIN << endl;
			}
		    continue;
		}
		inFile >> no1 >> units1 >> op >> no2 >> units2 >> equal >> ans >> unitsAns;
		if(! inFile)
		{
			cout << endl << " . BOGUS! UnitCalc History Format... Edit Line: "
			        << line << " (or) " << (line-1) << endl;
			outFile << endl << " ? Format Error ?" << endl << endl;
			cout << endl << endl << "      Enter... to Close/Quit.";
			getch();
			exit(1);
		}
		if(units1[0] == '@')         // ReCall Memories (1~Z) for units1~2 
		{
			chi = toupper(units1[1]);
			no1 = mem[char10(chi)];
		}
		if(units2[0] == '@')
		{
			chi = toupper(units2[1]);
			no2 = mem[char10(chi)];
		}
		if(start)
		{
			start = FALSE;
			switch (op)
			{
				case '^':
					newAns = pow(no1, no2);
					break;
				case '*':
					newAns = no1 * no2;
					break;
				case '/':
					if(no2 != 0) newAns = no1 / no2 ;
						else divZero();
						break;
				case '-':
					newAns = no1 - no2;
					break;
				case '+':
					newAns = no1 + no2;
					break;
				default : noOp();
					break;
			}
		}
		else
		{
			no1 = newAns;
			switch (op)
			{
				case '^':
					newAns = pow(no1, no2);
					break;
				case '*':
					newAns = no1 * no2;
					break;
				case '/':
					if (no2 != 0) newAns = no1 / no2 ;
						else divZero();
						break;
				case '-':
					newAns = no1 - no2;
					break;
				case '+':
					newAns = no1 + no2;
					break;
				default : noOp();
					break;
			}
		}                 // end of start
		if(unitsAns[0] == '@')         // Save mem '1~Z'
		{
			chi = toupper(unitsAns[1]);
			mem[char10(chi)] = newAns;
		}
		trimUnits();
		outFile << " " << no1 << " " << units1 << " " << op << " "
				    << no2 << " " << units2  << " =  " << newAns << " "
				    << unitsAns << endl;  // endLineInOut:
	}                    // end of while
	inFile.close();
	cout << endl << " Last Answer =  " << newAns << " " << unitsAns << endl;
	cout << endl << " Done. " << line;
	if(line > MAX*5) cout << " MAX. LINES ! ";
		else cout << " Lines... ";
	cout << "  Output to 'ReCalc.txt'" << endl;
	cout << endl << endl << "      Enter... to Close/Quit. ";
	getch();
}
int char10(char ch)
{
	if(ch >= '0' && ch <= '9') return ch - '0';
	else if(toupper(ch) >= 'A' && toupper(ch) <= 'Z') return toupper(ch) - '7';
	else cout << endl << endl << " Bogus Char Mem : " << ch << endl << endl;
	getch();
	exit(1);
}
void autoTrig()
{
	inFile >> S1 >> S2 >> S3 >> S4 >> ch5 >> units5 >> ch6 >> units6;
	if(! inFile)
	{
		cout << endl << " . Bogus AutoTrig Format..."  << endl;
		outFile << endl << " ? Bogus AutoTrig Format ?" << endl;
		getch();
		exit(1);
	}
	inFile.ignore(MAX, '\n');
	Adj = mem[char10(S1[0])];
	Opp = mem[char10(S2[0])];
	Hyp = mem[char10(S3[0])];
	Deg = mem[char10(S4[0])];


	if(Adj != 0 && Hyp != 0) AdjHyp();
	else if(Adj != 0 && Opp != 0) AdjOpp();
	else if(Adj != 0 && Deg != 0) AdjDeg();
	else if(Opp != 0 && Hyp != 0) OppHyp();
	else if(Opp != 0 && Deg != 0) OppDeg();
	else if(Hyp != 0 && Deg != 0) HypDeg();
	else
	{ 
                  cout << endl << " Bogus Trig " << endl;
	  getch();
	  exit(1);
	}
	outFile << " AutoTrig Values:  { " << Adj << "j  " << Opp << "o  "
	             << Hyp << "h  " << Deg << "g " << "}" << endl;
	if(ch5 != '0')
	{
		if(units5[0] == '~')
		{
		  outFile << "     " << mem[char10(ch5)];
		  DegMinSec();
		  outFile << units5 << endl;
		}
		else
		{
		  outFile << "     " << mem[char10(ch5)] << " " << units5 << endl;
		}
	}
	if(ch6 != '0')
	{
		if(units6[0] == '~')
		{
		  outFile << "     " << mem[char10(ch6)];
		  DegMinSec();
		  outFile << units6 << endl;
		}
		else
		{
		  outFile << "     " << mem[char10(ch6)] << " " << units6 << endl;
		}
	}
}
void DegMinSec()
{
	int min, deg;
	long double deg2, sec;

	deg = Deg;
	deg2 = (Deg-deg)*60;
	min = deg2;
	sec = (deg2-min)*60;

	outFile << "   [ " << deg << " Deg " << min << "\' " << sec << "\" ] " ;
}
void AdjOpp()
{
	netSquares = Adj*Adj + Opp*Opp;
	Hyp = mem[char10(S3[0])] = sqrt(netSquares);
	Deg = mem[char10(S4[0])] = atan(Opp/Adj)*180/Pi;   
}

void AdjHyp()
{
	if(Hyp <= Adj)
	{
		cout << " Bogus! AutoTrig: Hyp>Adj"  << endl << endl;
		outFile << endl << " Bogus! AutoTrig: Hyp>Adj"  << endl;
		getch();
		exit(1);
	}
	netSquares = Hyp*Hyp - Adj*Adj;
	Opp = mem[char10(S2[0])] = sqrt(netSquares);
	Deg = mem[char10(S4[0])] = acos(Adj/Hyp)*180/Pi;   
}

void OppHyp()
{
	if(Hyp <= Opp)
	{
		cout << " Bogus! AutoTrig: Hyp>Opp"  << endl << endl;
		outFile << endl << " Bogus! AutoTrig: Hyp>Opp"  << endl;
		getch();
		exit(1);
	}
	netSquares = Hyp*Hyp - Opp*Opp;
	Adj = mem[char10(S1[0])]   = sqrt(netSquares);
	Deg = mem[char10(S4[0])] = asin(Opp/Hyp)*180/Pi;
}
void AdjDeg()
{
	Hyp = mem[char10(S3[0])]  = Adj / cos(Deg*Pi/180);
	Opp = mem[char10(S2[0])]  = tan(Deg*Pi/180) * Adj;
}

void OppDeg()
{
	Adj = mem[char10(S1[0])]  = Opp / tan(Deg*Pi/180);
	Hyp = mem[char10(S3[0])]  = Opp / sin(Deg*Pi/180);
}

void HypDeg()
{
	Adj = mem[char10(S1[0])]  = Hyp * cos(Deg*Pi/180);
	Opp = mem[char10(S2[0])]  = Hyp * sin(Deg*Pi/180);
}

void trimUnits()
{
		int i;
		if(units1[0] == '@')
		{
			for(i=0; i < strlen(units1); i++)
			{
				units1[i] = units1[i+3];
			}
		}
		if(units2[0] == '@')
		{
			for(i=0; i < strlen(units2); i++)
			{
				units2[i] = units2[i+3];
			}
		}
		if(unitsAns[0] == '@')
		{
			for(i=0; i < strlen(unitsAns); i++)
			{
				unitsAns[i] = unitsAns[i+3];
			}
		}
}
void divZero()
{
    cout << endl << endl
			  << " Error: /0 !  Edit Line: " << line;
 	 outFile << endl << " ? /0 Error ?" << endl << endl;
   cout << "     Enter... to Close/Quit. ";
	 getch();
   exit(1);
}
void noOp()
{
    cout << endl << " NO */+-^ !  Edit Line: " << line;
    outFile << endl << " ? NO */+-^ Error ?"  << endl << endl;
    cout << "   Enter... to Close/Quit.  ";
    getch();
    exit(1);
}