/********************************************************** Group Project for COP 4020 Base Two Class with math and logical (bitwise) functions *********************************************************** includes the following class functions AddBin, SubBin, MulBin, DivBin ModBin, PowBin, OrBin, AndBin, XOrBin, NotBin and CompareBin for < <= > >= for Base Two numbers entered as 32-bit binary strings *********************************************************** source code with comments and documentation by Phil Porvaznik (PhilVaz) Andy Thoreson (planaria@gte.net) Tino Cash (cash@eng.usf.edu) Steve Bassi (sbassi@eng.usf.edu) ***********************************************************/ #include #include // for string functions #include // for toupper() #include // for math functions #include // for exit(0) // ***************************************** // // CLASS BaseTwo definition // // ***************************************** class BaseTwo { private: char VariableName[20]; // variable name for reference char BinaryAmount[33]; // binary string 32 bits of 1's 0's protected: // ****************************************** // // Binary to Decimal converter function // // ****************************************** long int BinToDec (char BinaryString[33]) const { // Decimal Amount is a long int for conversion // which allows for 32-bit (4 byte) numbers long int DecimalAmount = 0; // power of 2, begin at first bit long int Pow2 = 1; // go from lowest bit (on right of string) // to highest bit (on left of string) // and compute decimal amount for (int i = (strlen(BinaryString) - 1); i >= 1; i--) { // if bit is 1 add corresponding power of 2 if (BinaryString[i] == '1') DecimalAmount = DecimalAmount + Pow2; // move to next power of 2 bit Pow2 = Pow2 * 2; } // check for sign in highest bit (element 0 in array) // 0 = positive, 1 = negative if (BinaryString[0] == '1') DecimalAmount = -DecimalAmount; cout << "Decimal Amount = " << DecimalAmount << "\n"; return DecimalAmount; } // END BinToDec // ******************************************** // // Decimal to Binary converter function // // ******************************************** void DecToBin (long int DecimalAmount, char BinaryString[]) const { long int Remainder; // remainder after modulus function int BinaryPosition; // position in char array bool Negative = false; // check for negative sign if (DecimalAmount < 0) { Negative = true; // if < 0 then sign is negative DecimalAmount = -DecimalAmount; // work with positive } // make all digits of Binary String zero for (int i=0; i<=31; i++) BinaryString[i] = '0'; // set end of string and begin at high bit position = 31 BinaryString[32] = '\0'; BinaryPosition = 31; while (DecimalAmount >= 1) { Remainder = DecimalAmount % 2; // get remainder of mod 2 DecimalAmount = DecimalAmount / 2; // int divide number by 2 // bit is 1 if we have remainder, otherwise leave 0 if (Remainder == 1) BinaryString[BinaryPosition] = '1'; // move to next lowest bit BinaryPosition--; } // check if negative, if so make lowest bit 1 if (Negative) BinaryString[0] = '1'; } // END DecToBin public: // *************************************** // // Display Variable function // // *************************************** void DisplayVariable () { unsigned int i; cout << VariableName; // line up columns with correct spacing for (i=1; i <= (22 - strlen(VariableName)); i++) cout << " "; cout << BinaryAmount << "\n"; } // END DisplayVariable // *************************************** // // Set Binary Amount to 01 for // increment and decrement // // *************************************** void SetOne () { strcpy (BinaryAmount, "01"); } // END SetOne // *************************************** // // Check Variable Name function // // *************************************** bool CheckForVariable (char VarName[]) { // if var names equal return true // otherwise false if (strcmp(VarName, VariableName) == 0) return true; else return false; } // END CheckForVariable // *************************************** // // Get Variable Name function // // *************************************** void GetVariableName () { char VarName[80]; // for temp variable name bool valid = false; // for whether valid variable name while (!valid) // while NOT valid { cout << "Enter a new variable name: "; cin.getline(VarName,80); // check if variable name < 20 chars and at least 1 char if (strlen(VarName) > 19 || strlen(VarName) < 1) cout << "Sorry, variable names must be less than 20 chars, and at least 1 char\n"; else { // copy the entered variable name into class data strcpy(VariableName, VarName); valid = true; } } } // END GetVariableName // *************************************** // // Get Binary Amount function // // **************************************** void GetBinaryAmount () { char BinaryString[80]; // for binary data 1's 0's bool valid = false; // for whether valid binary number while (!valid) // while NOT valid { // prompt for input of valid 32-bit binary number cout << "Enter up to 32-bit binary number of 1's and 0's\n"; cout << "Highest bit (on the left) is the sign (0=positive, 1=negative)\n"; // get a line of 80 chars cin.getline(BinaryString,80); // however make sure they actually entered less than 33 bits valid = true; if (strlen(BinaryString) > 32 || strlen(BinaryString) < 2) { cout << "Please enter a binary number at least 2 bits but less than 33 bits\n\n"; valid = false; } else { // they entered <= 32 bits so check if valid binary number // can only contain 1's or 0's, set initial to true for (unsigned int i=0; i < strlen(BinaryString); i++) { if (BinaryString[i] != '0' && BinaryString[i] != '1') { cout << "Sorry, that does not appear to be a valid binary number\n\n"; valid = false; break; // break out of FOR loop } } // END FOR } // END ELSE } // END WHILE // they entered valid binary number // so copy into binary amount class data strcpy (BinaryAmount, BinaryString); } // END GetBinaryAmount // ************************************* // // Add Binary function // // ************************************* void AddBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); // perform add, save result as long double temp = long double (Decimal1) + long double (Decimal2); cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on addition"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } // END AddBin // ************************************* // // Subtract Binary function // // ************************************* void SubBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); // perform subtract, save result as long double temp = long double (Decimal1) - long double (Decimal2); cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on subtraction"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } // END SubBin // ************************************* // // Multiply Binary function // // ************************************* void MulBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); // perform multiply, save result as long double temp = long double (Decimal1) * long double (Decimal2); cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on multiply"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } // END MulBin // ******************************************* // // Divide (integer division) Binary function // // ******************************************* void DivBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); // special: check if division by zero error if (Decimal2 == 0) strcpy(BinaryResult, "ERROR: division by zero attempted"); else { // perform int division, save result as long double temp = Decimal1 / Decimal2; cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on division"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } } // END DivBin // ***************************** // // Modulus Binary function // // ***************************** void ModBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); // perform modulus, save result as long double temp = Decimal1 % Decimal2; cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on modulus"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } // END ModBin // ******************************** // // Power Binary function // // accepts only non-negative powers // // ******************************** void PowBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings long int Decimal1, Decimal2, DecimalResult; // decimal conversion long double temp; // for overflow check // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); if (Decimal2 < 0) strcpy (BinaryResult, "ERROR: negative power attempted"); else { // perform raise to power, save result as long double temp = pow(Decimal1, Decimal2); cout.precision(50); cout << "Decimal Result = " << temp << "\n"; // check if overflow if (temp > 2147483647 || temp < -2147483647) strcpy(BinaryResult, "OVERFLOW on raise to power"); else { DecimalResult = long int (temp); // convert to binary DecToBin(DecimalResult, BinaryResult); } } } // END PowBin // ****************************** // // Bitwise OR Binary function // // ****************************** void OrBin (const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings unsigned int pos1, pos2; // bit position for string1, string2 char c1, c2; // temp char for string1, string2 // make all digits of binary result zero for (int i=0; i<=31; i++) BinaryResult[i] = '0'; // set end of string BinaryResult[32] = '\0'; // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); i = 31; // start at low bit (rightmost) of binary result // start at low bit (rightmost) of input strings pos1 = strlen(BinaryString1) - 1; pos2 = strlen(BinaryString2) - 1; // loop through all 31 bits of binary result string while (i > 0) { if (pos1 > 0) c1 = BinaryString1[pos1]; // get the bit value else c1 = '0'; // if reached first bit, fill with 0 if (pos2 > 0) c2 = BinaryString2[pos2]; // get the bit value else c2 = '0'; // if reached first bit, fill with 0 // perform bitwise OR, either c1 or c2, make 1 // otherwise leave 0 if (c1 == '1' || c2 == '1') BinaryResult[i] = '1'; i--; // decrement result bit position if (pos1 > 0) pos1--; // decrement string1 bit position if (pos2 > 0) pos2--; // decrement string2 bit position } // END WHILE // check for sign bit (position 0) c1 = BinaryString1[0]; c2 = BinaryString2[0]; if (c1 == '1' || c2 == '1') BinaryResult[0] = '1'; } // END OrBin // *************************************** // // Bitwise Exclusive OR Binary function // // *************************************** void XOrBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings unsigned int pos1, pos2; // bit position for string1, string2 char c1, c2; // temp char for string1, string2 // make all digits of binary result zero for (int i=0; i<=31; i++) BinaryResult[i] = '0'; // set end of string BinaryResult[32] = '\0'; // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); i = 31; // start at low bit (rightmost) of binary result // start at low bit (rightmost) of input strings pos1 = strlen(BinaryString1) - 1; pos2 = strlen(BinaryString2) - 1; // loop through all 31 bits of binary result string while (i > 0) { if (pos1 > 0) c1 = BinaryString1[pos1]; // get the bit value else c1 = '0'; // if reached first bit, fill with 0 if (pos2 > 0) c2 = BinaryString2[pos2]; // get the bit value else c2 = '0'; // if reached first bit, fill with 0 // perform bitwise XOR, either c1 or c2, make 1 // if both 0 or both 1 leave 0 if ((c1 == '0' && c2 == '1') || (c1 == '1' && c2 == '0')) BinaryResult[i] = '1'; i--; // decrement result bit position if (pos1 > 0) pos1--; // decrement string1 bit position if (pos2 > 0) pos2--; // decrement string2 bit position } // END WHILE // check for sign bit (position 0) c1 = BinaryString1[0]; c2 = BinaryString2[0]; if ((c1 == '0' && c2 == '1') || (c1 == '1' && c2 == '0')) BinaryResult[0] = '1'; } // END XOrBin // ********************************* // // Bitwise AND Binary function // // ********************************* void AndBin(const BaseTwo &BinaryInput, char BinaryResult[]) const { char BinaryString1[33], BinaryString2[33]; // input strings unsigned int pos1, pos2; // bit position for string1, string2 char c1, c2; // temp char for string1, string2 // make all digits of binary result zero for (int i=0; i<=31; i++) BinaryResult[i] = '0'; // set end of string BinaryResult[32] = '\0'; // save two input binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); i = 31; // start at low bit (rightmost) of binary result // start at low bit (rightmost) of input strings pos1 = strlen(BinaryString1) - 1; pos2 = strlen(BinaryString2) - 1; // loop through all 31 bits of binary result string while (i > 0) { if (pos1 > 0) c1 = BinaryString1[pos1]; // get the bit value else c1 = '0'; // if reached first bit, fill with 0 if (pos2 > 0) c2 = BinaryString2[pos2]; // get the bit value else c2 = '0'; // if reached first bit, fill with 0 // perform bitwise AND, both c1 and c2, make 1 // otherwise leave '0' if (c1 == '1' && c2 == '1') BinaryResult[i] = '1'; i--; // decrement result bit position if (pos1 > 0) pos1--; // decrement string1 bit position if (pos2 > 0) pos2--; // decrement string2 bit position } // END WHILE // check for sign bit (position 0) c1 = BinaryString1[0]; c2 = BinaryString2[0]; if (c1 == '1' && c2 == '1') BinaryResult[0] = '1'; } // END AndBin // ***************************************** // // Bitwise NOT Binary function definition // // ***************************************** void NotBin(char BinaryResult[]) const { char BinaryString1[33]; // input string1 unsigned int pos1; // bit position for string1 char c1; // temp char for string1 // make all digits of binary result zero for (int i=0; i<=31; i++) BinaryResult[i] = '0'; // set end of string BinaryResult[32] = '\0'; // save input binary string strcpy(BinaryString1, BinaryAmount); i = 31; // start at low bit (rightmost) of binary result // start at low bit (rightmost) of input strings pos1 = strlen(BinaryString1) - 1; // loop through all 31 bits of binary result string while (i > 0) { if (pos1 > 0) c1 = BinaryString1[pos1]; // get the bit value else c1 = '0'; // if reached first bit, fill with 0 // perform bitwise NOT, opposite value of c1 if (c1 == '0') BinaryResult[i] = '1'; else BinaryResult[i] = '0'; i--; // decrement result bit position if (pos1 > 0) pos1--; // decrement string1 bit position } // END WHILE // check for sign bit (position 0) c1 = BinaryString1[0]; if (c1 == '0') BinaryResult[0] = '1'; else BinaryResult[0] = '0'; } // END NotBin // ************************************* // // Compare Binary Numbers function // // ************************************* int CompareBin(const BaseTwo &BinaryInput) const { long int Decimal1, Decimal2; char BinaryString1[33], BinaryString2[33]; // save two binary strings strcpy(BinaryString1, BinaryAmount); strcpy(BinaryString2, BinaryInput.BinaryAmount); // convert them to decimal Decimal1 = BinToDec(BinaryString1); Decimal2 = BinToDec(BinaryString2); int CompareResult = 0; // if equal = 0 if (Decimal1 < Decimal2) CompareResult = -1; // if 1 < 2 = -1 if (Decimal1 > Decimal2) CompareResult = 1; // if 1 > 2 = +1 return CompareResult; } // END CompareBin }; // END Class BaseTwo definition // // GetAVariable function prototype // bool GetAVariable (char Prompt[50], BaseTwo BinaryArray[99], int NumOfVars, int &i); // // MAIN starts here // void main() { BaseTwo BinaryArray[99]; // allow up to 99 variables BaseTwo TempOne; // TempOne set to "01" TempOne.SetOne(); // for increment/decrement int NumOfVars = 0; // number of variables entered bool Found, Found2; // bool for variable name found int i,j; // int i,j for loops int Result; // for comparison function char BinaryString[80]; // temp binary amount char menu[80], c; // char for menu selection // *************************************************** // // menu infinite loop, quits with exit(0) when done // // *************************************************** while (1) { cout << "\nMake a selection: (type ? for menu options) "; cin.getline(menu,80); c = toupper(menu[0]); // get first char, make UPPERCASE, ignore rest switch (c) { case 'A' : // // Enter a new variable and amount // { if (NumOfVars < 99) { // increment number of variables // get both variable name and binary amount NumOfVars++; BinaryArray[NumOfVars].GetVariableName(); BinaryArray[NumOfVars].GetBinaryAmount(); } else cout << "Sorry, the variable list is full\n"; break; // break out of SWITCH statement } case 'B' : // // List all variables and amounts // { if (NumOfVars < 1) cout << "Sorry, the variable list is empty\n"; else { // we have at least one variable // display headings, variables and amounts cout << "\nVariable Binary Amount\n"; cout << "======== =============\n\n"; for (i=1; i<=NumOfVars; i++) BinaryArray[i].DisplayVariable(); } break; // break out of SWITCH statement } case 'C' : // // Change an existing amount // { cout << "CHANGE AN EXISTING BINARY AMOUNT\n"; Found = GetAVariable ("Enter the variable name to change", BinaryArray, NumOfVars, i); if (Found) // if variable exists, get new binary amount BinaryArray[i].GetBinaryAmount(); else cout << "Sorry, variable name not found\n"; break; // break out of SWITCH statement } case 'D' : // // Clear/Delete variables list // { cout << "Are you sure you want to delete ALL variables? (y/n) "; cin.getline(menu,80); c = menu[0]; if (c == 'y') // if user confirms clear list { NumOfVars = 0; cout << "Variables list deleted!\n"; } break; // break out of SWITCH statement } case 'E' : // // Add two variables // { cout << "ADD TWO VARIABLES -- Format: Var1 + Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for addition", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for addition", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, add them using AddBin BinaryArray[i].AddBin(BinaryArray[j], BinaryString); cout << "The result of the addition = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'F' : // // Subtract two variables // { cout << "SUBTRACT TWO VARIABLES -- Format: Var1 - Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for subtraction", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for subtraction", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, subtract them using SubBin BinaryArray[i].SubBin(BinaryArray[j], BinaryString); cout << "The result of the subtraction = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'G' : // // Multiply two variables // { cout << "MULTIPLY TWO VARIABLES -- Format: Var1 * Var 2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for multiplication", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for multiplication", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, multiply them using MulBin BinaryArray[i].MulBin(BinaryArray[j], BinaryString); cout << "The result of the multiplication = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'H' : // // Divide (integer division) two variables // { cout << "(Integer) DIVIDE TWO VARIABLES -- Format: Var1 / Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for division", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for division", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, divide them using DivBin BinaryArray[i].DivBin(BinaryArray[j], BinaryString); cout << "The result of the (integer) division = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'I' : // // Increment a variable // { cout << "INCREMENT A VARIABLE\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter the variable name to increment", BinaryArray, NumOfVars, i); if (Found) { // variable found, increment using TempOne "01" with AddBin BinaryArray[i].AddBin(TempOne, BinaryString); cout << "The result of the increment = " << BinaryString << "\n"; } else cout << "Sorry, the variable name is not found\n"; break; // break out of SWITCH statement } case 'J' : // // Decrement a variable // { cout << "DECREMENT A VARIABLE\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter the variable name to decrement", BinaryArray, NumOfVars, i); if (Found) { // variable found, decrement using TempOne "01" with SubBin BinaryArray[i].SubBin(TempOne, BinaryString); cout << "The result of the decrement = " << BinaryString << "\n"; } else cout << "Sorry, the variable name is not found\n"; break; // break out of SWITCH statement } case 'K' : // // Modulus function // { cout << "MODULUS (REMAINDER) WITH TWO VARIABLES -- Format: Var1 MOD Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for modulus", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for modulus", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, find modulus (remainder) using ModBin BinaryArray[i].ModBin(BinaryArray[j], BinaryString); cout << "The result of modulus = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'L' : // // Raise to Power function // { cout << "RAISE TO POWER WITH TWO VARIABLES -- Format: Var1 RAISED TO Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for raise to power", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for raise to power", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, raise to power using PowBin BinaryArray[i].PowBin(BinaryArray[j], BinaryString); cout << "The result of raise to power = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'M' : // // Binary OR // { cout << "BITWISE BINARY (OR) ON TWO VARIABLES -- Format: Var1 OR Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for bitwise OR", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for bitwise OR", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, perform bitwise OR with OrBin BinaryArray[i].OrBin(BinaryArray[j], BinaryString); cout << "The result of bitwise OR = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'N' : // // Binary AND // { cout << "BITWISE BINARY (AND) ON TWO VARIABLES -- Format: Var1 AND Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for bitwise AND", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for bitwise AND", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, perform bitwise AND with AndBin BinaryArray[i].AndBin(BinaryArray[j], BinaryString); cout << "The result of bitwise AND = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'O' : // // Binary XOR // { cout << "BITWISE BINARY (EXCLUSIVE OR) ON TWO VARIABLES -- Format: Var1 XOR Var2\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for bitwise XOR", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for bitwise XOR", BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, perform bitwise XOR with XOrBin BinaryArray[i].XOrBin(BinaryArray[j], BinaryString); cout << "The result of bitwise XOR = " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'P' : // // Binary NOT // { cout << "BITWISE BINARY (NOT) ON A VARIABLE -- Format: NOT Var1\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for bitwise NOT", BinaryArray, NumOfVars, i); if (Found) { // variable found, perform bitwise NOT with NotBin BinaryArray[i].NotBin(BinaryString); cout << "The result of bitwise NOT = " << BinaryString << "\n"; } else cout << "Sorry, the variable name is not found\n"; break; // break out of SWITCH statement } case 'Q' : // // Compare < (less than) // { cout << "COMPARE < TWO VARIABLES -- Format: Is Var1 < Var2 ?\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for < compare", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for < compare",BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, compare them using CompareBin Result = BinaryArray[i].CompareBin(BinaryArray[j]); // if Result < 0 then Var1 < Var2 if (Result < 0) strcpy (BinaryString, "TRUE"); else strcpy (BinaryString, "FALSE"); cout << "The result of < compare is " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'R' : // // Compare <= (less than or equal) // { cout << "COMPARE <= TWO VARIABLES -- Format: Is Var1 <= Var2 ?\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for <= compare", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for <= compare",BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, compare them using CompareBin Result = BinaryArray[i].CompareBin(BinaryArray[j]); // if Result <= 0 then Var1 <= Var2 if (Result <= 0) strcpy (BinaryString, "TRUE"); else strcpy (BinaryString, "FALSE"); cout << "The result of <= compare is " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'S' : // // Compare > (greater than) // { cout << "COMPARE > TWO VARIABLES -- Format: Is Var1 > Var2 ?\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for > compare", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for > compare",BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, compare them using CompareBin Result = BinaryArray[i].CompareBin(BinaryArray[j]); // if Result > 0 then Var1 > Var2 if (Result > 0) strcpy (BinaryString, "TRUE"); else strcpy (BinaryString, "FALSE"); cout << "The result of > compare is " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case 'T' : // // Compare >= (greater than or equal) // { cout << "COMPARE >= TWO VARIABLES -- Format: Is Var1 >= Var2 ?\n"; // call the GetAVariable function, returns true if variable found Found = GetAVariable ("Enter Var1 for >= compare", BinaryArray, NumOfVars, i); Found2 = GetAVariable ("Enter Var2 for >= compare",BinaryArray, NumOfVars, j); if (Found && Found2) { // both variables found, compare them using CompareBin Result = BinaryArray[i].CompareBin(BinaryArray[j]); // if Result >= 0 then Var1 >= Var2 if (Result >= 0) strcpy (BinaryString, "TRUE"); else strcpy (BinaryString, "FALSE"); cout << "The result of >= compare is " << BinaryString << "\n"; } else cout << "Sorry, one of the variable names not found\n"; break; // break out of SWITCH statement } case '?' : // // Display the full menu options // { cout << "\n Menu and Binary Operations\n"; cout << " ==========================\n\n"; cout << "(A) Enter a new variable and amount (B) List all variables and amounts\n"; cout << "(C) Change an existing amount (D) Clear/Delete variables list\n"; cout << "(E) Add two variables (F) Subtract two variables\n"; cout << "(G) Multiply two variables (H) (integer) Divide two variables\n"; cout << "(I) Increment a variable (J) Decrement a variable\n"; cout << "(K) Modulus function (L) Raise to Power function\n"; cout << "(M) Binary OR (N) Binary AND\n"; cout << "(O) Binary XOR (P) Binary NOT\n"; cout << "(Q) Compare < (less than) (R) Compare <= (less than or equal)\n"; cout << "(S) Compare > (greater than) (T) Compare >= (greater than or equal)\n"; cout << "(X) Exit\n"; break; // break out of SWITCH statement } case 'X' : exit(0); default : cout << "Sorry, that item is not a selection\n"; } // END SWITCH } // END WHILE MENU } // END MAIN // **************************************** // // GetAVariable function definition // // **************************************** bool GetAVariable (char Prompt[], BaseTwo BinaryArray[], int NumOfVars, int &i) { char VarName[80]; cout << Prompt << "\n"; cin.getline(VarName,80); // check if variable exists, set to not found for (i=1; i<=NumOfVars; i++) { if (BinaryArray[i].CheckForVariable(VarName)) return true; } return false; }