int main()
{
printWelcome();
string strFile;
char invalid = 'i';
do
{
cout << "\n Pick a choice type e to quit." << endl;
cin >> choice;
if (choice == 'e')
return 0;
}
choice = toupper(choice);
result = GetPickMenu(choice);
if(choice == invalid)
{
cout << "enter a valid character" << endl;
}
}
while(choice = invalid);
}
void PreformParse(string& strValOne, string& strValTwo)
{
string strCurrLine;
string strTemp;
int intCurrPos;
char currChar;
int countPlus = count + 1;
if(fileName == "")
{
cout << "Please enter a file" ;
cin >> fileName; //filename
}
//Priming Read
din.open(fileName.c_str());
if(count == 1)
{
din >> strValOne;
din >> strValTwo;
}
while(countPlus >= 1)
{
din >> strValOne;
din >> strValTwo;
countPlus--;
}
if(din)
{
din >> strValOne;
din >> strValTwo;
count++;
}
else
cout << "Error: File Not Found" << endl;
din.close();
}
bool ValidBin(string binary)
{
int count = 0;
bool valid = true;
if(binary.size() == BYTE_SIZE)
{
while (count < BYTE_SIZE && valid)
{
if (binary[count] !='0' && binary [count] != '1')
{
valid = false;
}
else
valid = true;
//cout << binary[count];
count++;
}
return valid;
}
}
int ConvertFromBinary(string binary)
{
float placeVal = pow(2.0f,7.0f);
float sum = 0;
for(int pos = 0; pos < BYTE_SIZE ; pos++)
{
if (binary[pos] == '1')
{
sum= sum + placeVal;
placeVal = placeVal / 2;
}
}
return sum;
}
string ConvertDecimaltoBinary(int decimal)
{
int digit = 255;
int pos = 0;
string binary = "00000000";
while (digit >= 1)
{
if(decimal>=digit)
{
binary[pos] = '1';
decimal = decimal - digit;
}
digit = digit / 2;
pos++;
}
return binary;
}
char GetPickMenu(char choice)
char invalid = 'i';
while ( choice != SENT_VALUE)
{
switch(choice)
{
case '+': PerformAddition(ValOne, ValTwo, choice);
totalMath++
;break;
case '-': PerformSubtraction(ValOne, ValTwo, choice); totalMathOne++ ;break;
case '%': PerformModulus(ValOne, ValTwo, choice); totalMathTwo++ ;break;
case '/': PerformDivision(ValOne, ValTwo, choice); totalMathThree++ ;break;
case '*': PerformMultiplication(ValOne, ValTwo, choice); totalMathFour++ ;break;
case 'P': PreformParse(ValOne, ValTwo); totalMathFive++ ; break;
case 'Q': printSummary(totalMath, totalMathOne,
totalMathTwo, totalMathThree,
totalMathFour, totalMathFive); break;
case 'B':
{
cout << " enter a valid binary number" << endl;
cin >> binNum;
if (ValidBin(binNum));
binAns=ConvertFromBinary(binNum);
cout << "the converted binary number is" << binAns;
totalMath++ ;
break;
}
case 'C' :
{
float floatTemp;
cout << "enter a valid decimal number" << endl;
cin >> floatTemp;
binNum = ConvertDecimaltoBinary(floatTemp);
cout << " the converted decimal number is" << binNum;
totalMath++ ;
break;
}
default: choice = invalid ; break;
return choice;
}
choice = SENT_VALUE;
}
}
void PerformAddition(string& ValOne, string& ValTwo, char choice)
{
ifstream din;
ofstream dout;
float decimalOne = 0;
float decimalTwo = 0;
string operationOne = ValOne;
string operationTwo = ValTwo;
string strResult;
float result = 0;
int mathTotal = 0;
dout.open("Addition.txt");
while (din)
{
switch(choice)
{
case '+':
{
din >> operationOne >> operationTwo;
if(ValidBin(operationOne) || ValidBin (operationTwo))
{
dout << "these are valid binary numbers";
}
else
{
cout << "not a valid binary number";
}
if(result < 256)
{
decimalOne = ConvertFromBinary(operationOne);
decimalTwo = ConvertFromBinary(operationTwo);
result = decimalOne + decimalTwo;
strResult = ConvertDecimaltoBinary(result);
dout << operationOne << '+'<< operationTwo << '=' << strResult; //dout
}
else
{
dout << "Error Overflow";
}
din.close();
}
break;
}
PreformParse(ValOne, ValTwo);
}
}
void PerformSubtraction(string ValOne, string ValTwo, char choice)
{
ifstream din;
ofstream dout;
float decimalOne = 0;
float decimalTwo = 0;
string operationOne = ValOne;
string operationTwo = ValTwo;
string strResult;
float result = 0;
dout.open("Subtraction.txt");
while (din)
{
switch(choice)
{
case '-':
{
din >> operationOne >> operationTwo;
if(ValidBin(operationOne) || ValidBin (operationTwo))
{
dout << "these are valid binary numbers";
}
else
{
cout << "not a valid binary number";
}
if(result <= 0)
{
decimalOne = ConvertFromBinary(operationOne);
decimalTwo = ConvertFromBinary(operationTwo);
result = decimalOne - decimalTwo;
strResult = ConvertDecimaltoBinary(result);
dout << operationOne << '-'<< operationTwo << '=' << strResult;
}
else
{
dout << "Error Underflow";
}
din.close();
}
break;
}
}
}
void PerformMultiplication(string ValOne, string ValTwo, char choice)
{
ifstream din;
ofstream dout;
float decimalOne = 0;
float decimalTwo = 0;
string operationOne = ValOne;
string operationTwo = ValTwo;
string strResult;
float result = 0;
dout.open("Multiplication.txt");
while (din)
{
switch(choice)
{
case '*':
{
din >> operationOne >> operationTwo;
if(ValidBin(operationOne) || ValidBin (operationTwo))
{
dout << "these are valid binary numbers";
}
else
{
cout << "not a valid binary number";
}
if(result < 256)
{
decimalOne = ConvertFromBinary(operationOne);
decimalTwo = ConvertFromBinary(operationTwo);
result = decimalOne * decimalTwo;
strResult = ConvertDecimaltoBinary(result);
dout << operationOne << '*'<< operationTwo << '=' << strResult;
}
else
{
dout << "Error Overflow";
}
din.close();
}
break;
}
}
}
void PerformDivision(string ValOne, string ValTwo, char choice)
{
ifstream din;
ofstream dout;
float decimalOne = 0;
float decimalTwo = 0;
string operationOne = ValOne;
string operationTwo = ValTwo;
string strResult;
float result = 0;
dout.open("Division.txt");
while (din)
{
switch(choice)
{
case '/':
{
din >> operationOne >> operationTwo;
if(ValidBin(operationOne) || ValidBin (operationTwo))
{
dout << "these are valid binary numbers";
}
else
{
cout << "not a valid binary number";
}
if(result < 256)
{
decimalOne = ConvertFromBinary(operationOne);
decimalTwo = ConvertFromBinary(operationTwo);
result = decimalOne / decimalTwo;
strResult = ConvertDecimaltoBinary(result);
dout << operationOne << '/'<< operationTwo << '=' << strResult;
}
else
{
dout << "Error Divided by zero";
}
din.close();
}
break;
}
}
}
void PerformModulus(string ValOne, string ValTwo, char choice)
{
ifstream din;
ofstream dout;
float decimalOne = 0;
float decimalTwo = 0;
string operationOne = ValOne;
string operationTwo = ValTwo;
string strResult;
float result = 0;
dout.open("Modulus.txt");
while (din)
{
switch(choice)
{
case '%':
{
din >> operationOne >> operationTwo;
if(ValidBin(operationOne) || ValidBin (operationTwo))
{
dout << "these are valid binary numbers";
}
else
{
cout << "not a valid binary number";
}
if(result < 256)
{
decimalOne = (float)ConvertFromBinary(operationOne);
decimalTwo = (float)ConvertFromBinary(operationTwo);
result = decimalOne + decimalTwo;
strResult = ConvertDecimaltoBinary(result);
dout << operationOne << '%'<< operationTwo << '=' << strResult;
}
else
{
dout << "Error Overflow";
}
din.close();
}
break;
}
}
}
[/code]
I'm extremely new to programming most of the code above is from a friend, btw this is suppose to be a binary calculator.