Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <cmath>
- using namespace std;
- int convertNumber(char hexnum) //A function which converts out hex number to a dec number to operate with
- { //We use the asce table to see if the user has written a number or a sybol and we convert it
- int decNum = 0; //The value of our dec number is 0 at the beggining
- if ((int)hexnum >= 48 && (int)hexnum <= 57) //If our input was a number from 0 to 9
- {
- int count = 48; //This counter saves the value of the code of our input
- double dechelp = 0; //This variable saves the value of our dec number
- while ((int)hexnum != count) //If the code of our input is diggerent than the code we are checking with (count)
- { //we move to the next code (count++) and to a next number from 0 to 9
- count++;
- dechelp++;
- }
- decNum = dechelp; //The dec number we return is a digit from 0 to 9
- }
- if ((int)hexnum >= 65 && (int)hexnum <= 70) //If the user's input was a letter
- {
- int count = 65; //We proceed the same way but we start checking with the code ot the letter A and our first
- int dechelp = 10; //possible dec number is 10; If there isn't a match we move to B and 11 and so on
- while ((int)hexnum != count) {
- count++;
- dechelp++;
- }
- decNum = dechelp;
- }
- return decNum; //We return our dec number to operate wth
- }
- char fromDecToHex(int num)
- {
- char digit = ' ';
- switch (num) //We create a switch to turn a variable from an int to a char and when there is a match we return the char
- {
- case 0:
- digit = '0';
- return digit;
- break;
- case 1:
- digit = '1';
- return digit;
- break;
- case 2:
- digit = '2';
- return digit;
- break;
- case 3:
- digit = '3';
- return digit;
- break;
- case 4:
- digit = '4';
- return digit;
- break;
- case 5:
- digit = '5';
- return digit;
- break;
- case 6:
- digit = '6';
- return digit;
- break;
- case 7:
- digit = '7';
- return digit;
- break;
- case 8:
- digit = '8';
- return digit;
- break;
- case 9:
- digit = '9';
- return digit;
- break;
- case 10:
- digit = 'A';
- return digit;
- break;
- case 11:
- digit = 'B';
- return digit;
- break;
- case 12:
- digit = 'C';
- return digit;
- break;
- case 13:
- digit = 'D';
- return digit;
- break;
- case 14:
- digit = 'E';
- return digit;
- break;
- case 15:
- digit = 'F';
- return digit;
- break;
- }
- }
- int main()
- {
- char num1 = ' ';
- char num2 = ' ';
- char operation = ' ';
- int result = 0;
- cout << "Input operation " << endl;
- cin.get(operation); //If the user's input is more than one symbol the program only takes one symbol
- cin.ignore(); //So if we write a negative number it onlu takes the - and then when we
- cout << "Input first number " << endl; //check if the input is from 0 to 9 or A to F it consuders it a wrong input
- cin.get(num1);
- cin.ignore(); //Luybo said we can use this type of verification
- cout << "Input second number " << endl;
- cin.get(num2);
- cin.ignore();
- if (!cin) //Checks if the input is somehow wrong
- {
- cout << "Wrong input!" << endl;
- return 1;
- }
- else
- {
- if ((int)num1 < 48 || ((int)num1 > 57 && (int)num1 < 65) || (int)num1>70)
- {
- cout << "Wrong input! " << endl; //Checks if the input is in the right range
- }
- else if ((int)num2 < 48 || (int)num2>57 && (int)num2 < 65 || (int)num2>70)
- {
- cout << "Wrong input! " << endl;
- }
- else {
- int number1 = 0;
- int number2 = 0;
- number1 = convertNumber(num1); //We call the function we use to convert our input from char to int
- number2 = convertNumber(num2);
- switch (operation) //A switch for the operation
- {
- case '+':
- result = number1 + number2;
- break;
- case '-':
- result = number1 - number2;
- break;
- case '*':
- result = number1 * number2;
- break;
- case '/':
- result = number1 / number2;
- break;
- case '%':
- result = number1 % number2;
- break;
- default:
- cout << "Wrong input of operation! " << endl; //If the input of operation is not accurate it shows a message
- return 0;
- }
- int absResult = abs(result); //Takes the positive value of our result if it happens to be negative
- int left = 0;
- int firstDigit = 0;
- int secondDigit = 0;
- firstDigit = absResult % 16; //The first digit of our result we get when we divide by 16
- left = absResult / 16;
- if (left != 0) //If what we have left from our result divided by 16 is diggerent than 0 that means our result has two
- { //digits when converted to hex
- secondDigit = left ; //If it has 2 digits we save the value of the second one
- if (absResult != result) //If we have a negative result we display the result with a -
- {
- cout << "-" << fromDecToHex(secondDigit) << fromDecToHex(firstDigit) << endl; //We call a function to convert the digits to hex
- }
- else
- {
- cout << fromDecToHex(secondDigit) << fromDecToHex(firstDigit) << endl;
- }
- }
- if (left == 0) //If what is left from the number divided by 16 is 0 we only have one digit
- { //and we proceed the same way as we did with 2 digits
- if (absResult != result)
- {
- cout << "-" << fromDecToHex(firstDigit) << endl;
- }
- else cout << fromDecToHex(firstDigit) << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement