Calculator.cpp

 #include "stdafx.h"
#include "calc.h"

typedef Math cal;

void error(int ErrorCode = 0) {
if (ErrorCode == 0)
exit(0);
if (ErrorCode == 1) {
cout << "Error 001: Memory faliure due to trying to store invalid data\n           Program will now close or fail depanding on type of memory leak\n";
system("pause");
exit(1);
}
//TODO: If more error codes are made, add error notification and management codes here
}
bool NotCommand(STRING Svalue) {//Made in different function for better understanding
//Divided for better understanding
if (Svalue == "Multi" || Svalue == "multi" || Svalue ==  "MULTI" ||
Svalue == "Save" || Svalue == "save" || Svalue ==  "SAVE" ||
Svalue == "Delete" || Svalue == "delete" || Svalue == "DELETE" ||
Svalue == "Show" || Svalue == "show" || Svalue == "SHOW" ||
Svalue == "Replace" || Svalue == "replace" || Svalue == "REPLACE" ||
Svalue == "Clear" || Svalue == "clear" || Svalue == "CLEAR" ||
/*TODO: If more commands are made, add them here*/
Svalue == "Exit" || Svalue == "exit" || Svalue == "EXIT")
return False;
return True;
}
bool NotOperator(STRING Operator) {//Made in different function for better understanding
//Divided for better understanding
if (Operator == "=" ||
Operator == "+=" || Operator == "-=" ||
Operator == "$" || Operator == "$$" ||
Operator == "+" || Operator == "-" || Operator == "*" || Operator == "/" ||
Operator == "==" || Operator == "!=" || Operator == ">" || Operator == "<" || Operator == ">=" || Operator == "<=" ||
Operator == "%" ||
Operator == "^" ||
Operator == "->"
/*TODO: If more operator features are made, add them here*/)
return False;
return True;
}
int calc() {
cal x;
STRING Number = new char[17];
STRING Operator = new char[2];
STRING Number2 = new char[17];
double Answer = 0.0;
int CurLocation = 0;
double Digit = 0.0;
double Digit2 = 0.0;
cout << setprecision(18);
cout << fixed;
CALCULATE:
cin >> Number;
if (FindLength(Number) > 17)//If modifing this, modify the dynamic allocation of this variable
error(1);
cout << endl;
for (int i = 0; Number[i] != 0; ++i) {
if (Number[i] < 48 || Number[i] > 57) {
if (Number == "Multi" || Number == "multi" || Number == "MULTI") {
cout << "What operation would you like to do?\n";
bool Operation = False;
STRING chStringMulti = new char[10];
MULTI:
cin >> chStringMulti;
if (FindLength(chStringMulti) > 10)
error(1);
int Input = 1;
if (chStringMulti == "Add" || chStringMulti == "add" || chStringMulti == "ADD") {
cout << "Begin operation:\n";
Answer = 0;
STRING chStringMultiNumber = new char[17];
MULTIADD:
cin >> chStringMultiNumber;
if (FindLength(chStringMultiNumber) > 17)
error(1);
if (NumbersOnly(chStringMultiNumber)) {
Answer += StringToInt(chStringMultiNumber);
++Input;
Operation = True;
goto MULTIADD;
}
else if (chStringMultiNumber == ";") {
if (Operation == False) {
cout << "No data was initialized\nInitialize data\n";
goto MULTIADD;
}
cout << Answer << endl;
goto CALCULATE;
}
else {
for (int j = 0; j <= 10; ++j) {
if (chStringMultiNumber == x.GetStorageName(j)) {
Answer += x.GetData(j);
++Input;
Operation = True;
goto MULTIADD;
}
if (x.GetData(j) == 0 || j == 10) {
cout << "Enter valid input from the ";
if (Input == 1)
cout << "1st";
else if (Input == 2)
cout << "2nd";
else if (Input == 3)
cout << "3rd";
else
cout << Input << "th";
cout << " Input\n";
goto MULTIADD;
}
}
}
}
if (chStringMulti == "Multiply" || chStringMulti == "multiply" || chStringMulti == "MULTIPLY" || chStringMulti == "Multi" || chStringMulti == "multi" || chStringMulti == "MULTI") {
cout << "Begin operation:\n";
Answer = 1;
STRING chStringMultiNumber = new char[17];
MULTIMULTI:
cin >> chStringMultiNumber;
if (FindLength(chStringMultiNumber) > 17)
error(1);
if (NumbersOnly(chStringMultiNumber)) {
Answer *= StringToInt(chStringMultiNumber);
++Input;
Operation = True;
goto MULTIMULTI;
}
else if (chStringMultiNumber == ";") {
if (Operation == False) {
cout << "No data was initialized\nInitialize data\n";
goto MULTIMULTI;
}
cout << Answer << endl;
goto CALCULATE;
}
else {
for (int j = 0; j <= 10; ++j) {
if (chStringMultiNumber == x.GetStorageName(j)) {
Answer *= x.GetData(j);
++Input;
Operation = True;
goto MULTIMULTI;
}
if (x.GetData(j) == 0 || j == 10) {
cout << "Enter valid input from the ";
if (Input == 1)
cout << "1st";
else if (Input == 2)
cout << "2nd";
else if (Input == 3)
cout << "3rd";
else
cout << Input << "th";
cout << " input\n";
goto MULTIMULTI;
}
}
}
}
else {
cout << "Enter valid input\n";
goto MULTI;
}
}
if (Number == "Save" || Number == "save" || Number == "SAVE") {
if (Answer != 0) {
cout << "What do you want to save the data as?\n";
STRING chStringNameOfData = new char[10];//If modifing this, modify the error handle for this variable
SAVE:
cin >> chStringNameOfData;
if (FindLength(chStringNameOfData) > 10)
error(1);
if (!NotCommand(chStringNameOfData)) {
cout << "A data can not have a name consisting only of a command\n";
goto SAVE;
}
if (!NotOperator(chStringNameOfData)) {
cout << "A data can not have a name consisting only of a operator symbol\n";
goto SAVE;
}
if (chStringNameOfData == "Answer" || chStringNameOfData == "answer" || chStringNameOfData == "ANSWER") {
cout << "Enter valid input\n";
goto SAVE;
}
for (int ii = 0; ii <= 10; ++ii) {
if (chStringNameOfData == x.GetStorageName(ii)) {
cout << "A data with this ID has already been initialized\nEnter a unique ID\n";
goto SAVE;
}
}
if (CurLocation > 10 && !NumbersOnly(chStringNameOfData)) {
cout << "There is no more data available\nWould you like to replace a data (Y/N)?";
STRING Choice = new char[4];
REPLACEYN:
cin >> Choice;
if (FindLength(Choice) > 3)
error(1);
if (Choice == "Yes" || Choice == "yes" || Choice == "YES" || Choice == "Y" || Choice == "y") {
cout << "Choose the location of the data to replace:\n";
x.DisplayData();
STRING Choice2 = new char[2];
REPLACE:
cin >> Choice2;
if (FindLength(Choice2) > 2)
error(1);
if (StringToInt(Choice2) > 0 && StringToInt(Choice2) <= 11) {
if (x.GetData(static_cast<int>(StringToInt(Choice2))) != 0) {
x.Save((static_cast<int>(StringToInt(Choice2))) - 1, Answer, chStringNameOfData);
cout << "Data replaced\n";
}
else {
cout << "Enter valid inout\n";
goto REPLACE;
}
goto CALCULATE;
}
else {
cout << "Enter valid input\n";
goto REPLACE;
}
}
else if (Choice == "No" || Choice == "no" || Choice == "NO" || Choice == "N" || Choice == "n") {
cout << "Save cancelled\n";
goto CALCULATE;
}
else {
cout << "Enter valid input\n";
goto REPLACEYN;
}
}
else if (CurLocation <= 10 && !NumbersOnly(chStringNameOfData)) {
x.Save(CurLocation, Answer, chStringNameOfData);
++CurLocation;
Answer = 0;
cout << "Data saved\n";
goto CALCULATE;
}
else {
cout << "Enter valid input\n";
goto SAVE;
}
}
else {
cout << "A data has not been intialized\nPlease perform an operation before saving\n";
goto CALCULATE;
}
}
else if (Number == "Delete" || Number == "delete" || Number == "DELETE") {
if (CurLocation == 0) {
cout << "There is no data available for deleting\n";
goto CALCULATE;
}
else {
cout << "Choose the location of the data to delete:\n";
x.DisplayData();
STRING Choice2 = new char[2];
DELETE:
cin >> Choice2;
if (FindLength(Choice2) > 2)
error(1);
if (StringToInt(Choice2) > 0 && StringToInt(Choice2) <= 11) {
if (x.GetData(static_cast<int>(StringToInt(Choice2)) - 1) != 0) {
x.Delete(static_cast<int>(StringToInt(Choice2)) - 1);
Answer = 0;
--CurLocation;
cout << "Data deleted\n";
}
else {
cout << "Enter valid input\n";
goto DELETE;
}
goto CALCULATE;
}
else {
cout << "Enter valid input\n";
goto DELETE;
}
}
}
else if (Number == "Show" || Number == "show" || Number == "SHOW") {
if (x.GetData(0) == 0) {
cout << "There is no data to display\n";
goto CALCULATE;
}
cout << "Current data:\n";
x.DisplayData();
goto CALCULATE;
}
else if (Number == "Replace" || Number == "replace" || Number == "REPLACE") {
cout << "Choose the location of the data to replace:\n";
x.DisplayData();
STRING Choice2 = new char[2];
FREPLACE:
cin >> Choice2;
if (FindLength(Choice2) > 2)
error(1);
if (StringToInt(Choice2) > 0 && StringToInt(Choice2) <= 11) {
if (x.GetData(static_cast<int>(StringToInt(Choice2)) - 1) == 0) {
cout << "The location has not been initialized\nPlease choose a location with intialized data\n";
goto FREPLACE;
}
cout << "What do you want to set the data as?\n";
STRING chStringNumber = new char[17];//If modifing this, modify the error handle for this variable
NUMBER:
cin >> chStringNumber;
if (FindLength(chStringNumber) > 17)
error(1);
if (!NumbersOnly(chStringNumber)) {
cout << "Enter valid input\n";
goto NUMBER;
}
cout << "What do you want to name it as?\n";
STRING chStringNameOfData = new char[10];
DATA:
cin >> chStringNameOfData;
if (FindLength(chStringNameOfData) > 10)
error(1);
if (!NotCommand(chStringNameOfData)) {
cout << "A data can not have a name of a command\n";
goto DATA;
}
if (!NotOperator(chStringNameOfData)) {
cout << "A data can not have a name consisting only of a operator symbol\n";
goto DATA;
}
if (chStringNameOfData == "Answer" || chStringNameOfData == "answer" || chStringNameOfData == "ANSWER") {
cout << "Enter valid input\n";
goto DATA;
}
for (int j = 0; j <= 10; ++j) {
if (chStringNameOfData == x.GetStorageName(j)) {
cout << "A data with this ID has already been initialized\nEnter a unique ID\n";
goto DATA;
}
}
if (!NumbersOnly(chStringNameOfData)) {
x.Save(static_cast<int>(StringToInt(Choice2)) - 1, StringToInt(chStringNumber), chStringNameOfData);
Answer = 0;
cout << "Data replaced\n";
goto CALCULATE;
}
}
else {
cout << "Enter valid input\n";
goto FREPLACE;
}
}
if (Number == "Clear" || Number == "clear" || Number == "CLEAR") {
cout << "Please wait . . .\n";
Answer = 0;
system("cls");
goto CALCULATE;
}
//TODO: Add other commands here (modify Math in calc.h if needed)
else if (Number == "Exit" || Number == "exit" || Number == "EXIT")
return null;
else {
if (Number == "Answer" || Number == "answer" || Number == "ANSWER")
break;
for (int j = 0; j <= 10; ++j) {
if (Number == x.GetStorageName(j)) {
Digit = x.GetData(j);
break;
}
if (x.GetData(j) == 0  || j == 10) {
cout << "Enter valid input\n";
goto CALCULATE;
}
}
break;
}
}
else {
if (Number[i + 1] == 0) {
Digit = StringToInt(Number);
break;
}
}
}
if (Number == "Answer" || Number == "answer" || Number == "ANSWER") {
if (Answer == 0) {
cout << "An answer has not been initialized\nPlease perform an operation before refering to answer\n";
goto CALCULATE;
}
Digit = Answer;
}
cin >> Operator;
if (FindLength(Operator) > 2)//If modifing this, modify the dynamic allocation of this variable
error(1);
//Operators grouped in types for easier understanding
if (Operator == "=") {
Answer = Digit;
cout << Answer << endl;
goto CALCULATE;
}
if (Operator == "+=" || Operator == "-=") {
if (Operator == "+=")
Answer = Digit * 2;
if (Operator == "-=")
Answer = Digit - Digit * 2;
cout << Answer << endl;
goto CALCULATE;
}
if (Operator == "$" || Operator == "$$") {
double dSqrt = 0.0;
if (Operator == "$") {
while (dSqrt * dSqrt < Digit)
++dSqrt;
if (dSqrt * dSqrt != Digit) {
--dSqrt;
while (dSqrt * dSqrt < Digit)
dSqrt += 0.000000001;
dSqrt -= 0.000000001;
}
}
if (Operator == "$$") {
while (dSqrt * dSqrt * dSqrt < Digit)
++dSqrt;
if (dSqrt * dSqrt * dSqrt != Digit) {
--dSqrt;
while (dSqrt * dSqrt * dSqrt < Digit)
dSqrt += 0.000000001;
dSqrt -= 0.000000001;
}
}
Answer = dSqrt;
cout << Answer << endl;
goto CALCULATE;
}
//TODO: Add other operator features here that does not require right hand operand
cin >> Number2;
if (FindLength(Number2) > 17)//If modifing this, modify the dynamic allocation of this variable
error(1);
if (!NumbersOnly(Number2)) {
for (int j = 0; j <= 10; ++j) {
if (Number2 == "Answer" || Number2 == "answer" || Number2 == "ANSWER") {
if (Answer == 0) {
cout << "An answer has not been initialized\nPlease perform an operation before refering to answer\n";
goto CALCULATE;
}
Digit2 = Answer;
break;
}
if (Number2 == x.GetStorageName(j)) {
Digit2 = x.GetData(j);
break;
}
if (x.GetData(j) == 0 || j == 10) {
cout << "Enter valid input\n";
goto CALCULATE;
}
}
}
else
Digit2 = StringToInt(Number2);
if (Operator == "+" || Operator == "-" || Operator == "*" || Operator == "/") { //Standard operators
if (Operator == "+")
Answer = Digit + Digit2;
if (Operator == "-")
Answer = Digit - Digit2;
if (Operator == "*")
Answer = Digit * Digit2;
if (Operator == "/")
Answer = Digit / Digit2;
cout << Answer << endl;
goto CALCULATE;
}
if (Operator == "==" || Operator == "!=" || Operator == ">" || Operator == "<" || Operator == ">=" || Operator == "<=") {
cout << "This statement is ";
if (Operator == "==") {
if (Digit == Digit2)
cout << "true\n";
else
cout << "false\n";
}
if (Operator == "!=") {
if (Digit != Digit2)
cout << "true\n";
else
cout << "false\n";
}
if (Operator == ">") {
if (Digit > Digit2)
cout << "true\n";
else
cout << "false\n";
}
if (Operator == "<") {
if (Digit < Digit2)
cout << "true\n";
else
cout << "false\n";
}
if (Operator == ">=") {
if (Digit >= Digit2)
cout << "true\n";
else
cout << "false\n";
}
goto CALCULATE;
if (Operator == "<=") {
if (Digit <= Digit2)
cout << "true\n";
else
cout << "false\n";
}
}
if (Operator == "%") {
Answer = static_cast<int>(Digit) % static_cast<int>(Digit2);
cout << Answer << endl;
goto CALCULATE;
}
if (Operator == "^") {
Answer = 1;
for (int i = 1; i <= Digit2; ++i)
Answer *= Digit;
cout << Answer << endl;
goto CALCULATE;
}
if (Operator == "->") {
if (Digit2 == 0) {
cout << "Enter valid input\n";
goto CALCULATE;
}
for (int j = 0; j <= 10; ++j) {
if (Number == "Answer" || Number == "answer" || Number == "ANSWER") {
Answer = Digit2;
break;
}
if (Number == x.GetStorageName(j)) {
Answer = Digit2;
x.SetData(j, Digit2);
break;
}
if (x.GetData(j) == 0 || j == 10) {
cout << "Enter valid input\n";
goto CALCULATE;
}
}
cout << Answer << endl;
goto CALCULATE;
}
//TODO: Add other operator features here (modify error handle for operator value if needed)
else {
cout << "Enter valid input\n";
goto CALCULATE;
}
}
int main() {
exit(calc());
}

Project Homepage: