Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #include<iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int convertNumber(char hexnum) //A function which converts out hex number to a dec number to operate with
  6. { //We use the asce table to see if the user has written a number or a sybol and we convert it
  7. int decNum = 0; //The value of our dec number is 0 at the beggining
  8. if ((int)hexnum >= 48 && (int)hexnum <= 57) //If our input was a number from 0 to 9
  9. {
  10. int count = 48; //This counter saves the value of the code of our input
  11. double dechelp = 0; //This variable saves the value of our dec number
  12. while ((int)hexnum != count) //If the code of our input is diggerent than the code we are checking with (count)
  13. { //we move to the next code (count++) and to a next number from 0 to 9
  14. count++;
  15. dechelp++;
  16. }
  17. decNum = dechelp; //The dec number we return is a digit from 0 to 9
  18. }
  19. if ((int)hexnum >= 65 && (int)hexnum <= 70) //If the user's input was a letter
  20. {
  21. int count = 65; //We proceed the same way but we start checking with the code ot the letter A and our first
  22. int dechelp = 10; //possible dec number is 10; If there isn't a match we move to B and 11 and so on
  23. while ((int)hexnum != count) {
  24. count++;
  25. dechelp++;
  26. }
  27. decNum = dechelp;
  28. }
  29. return decNum; //We return our dec number to operate wth
  30. }
  31.  
  32. char fromDecToHex(int num)
  33. {
  34. char digit = ' ';
  35.  
  36. 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
  37. {
  38. case 0:
  39. digit = '0';
  40. return digit;
  41. break;
  42. case 1:
  43. digit = '1';
  44. return digit;
  45. break;
  46. case 2:
  47. digit = '2';
  48. return digit;
  49. break;
  50. case 3:
  51. digit = '3';
  52. return digit;
  53. break;
  54. case 4:
  55. digit = '4';
  56. return digit;
  57. break;
  58. case 5:
  59. digit = '5';
  60. return digit;
  61. break;
  62. case 6:
  63. digit = '6';
  64. return digit;
  65. break;
  66. case 7:
  67. digit = '7';
  68. return digit;
  69. break;
  70. case 8:
  71. digit = '8';
  72. return digit;
  73. break;
  74. case 9:
  75. digit = '9';
  76. return digit;
  77. break;
  78. case 10:
  79. digit = 'A';
  80. return digit;
  81. break;
  82. case 11:
  83. digit = 'B';
  84. return digit;
  85. break;
  86. case 12:
  87. digit = 'C';
  88. return digit;
  89. break;
  90. case 13:
  91. digit = 'D';
  92. return digit;
  93. break;
  94. case 14:
  95. digit = 'E';
  96. return digit;
  97. break;
  98. case 15:
  99. digit = 'F';
  100. return digit;
  101. break;
  102.  
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. int main()
  110. {
  111. char num1 = ' ';
  112. char num2 = ' ';
  113. char operation = ' ';
  114. int result = 0;
  115. cout << "Input operation " << endl;
  116. cin.get(operation); //If the user's input is more than one symbol the program only takes one symbol
  117. cin.ignore(); //So if we write a negative number it onlu takes the - and then when we
  118. cout << "Input first number " << endl; //check if the input is from 0 to 9 or A to F it consuders it a wrong input
  119. cin.get(num1);
  120. cin.ignore(); //Luybo said we can use this type of verification
  121. cout << "Input second number " << endl;
  122. cin.get(num2);
  123. cin.ignore();
  124.  
  125. if (!cin) //Checks if the input is somehow wrong
  126. {
  127. cout << "Wrong input!" << endl;
  128. return 1;
  129. }
  130.  
  131. else
  132. {
  133.  
  134. if ((int)num1 < 48 || ((int)num1 > 57 && (int)num1 < 65) || (int)num1>70)
  135. {
  136. cout << "Wrong input! " << endl; //Checks if the input is in the right range
  137. }
  138. else if ((int)num2 < 48 || (int)num2>57 && (int)num2 < 65 || (int)num2>70)
  139. {
  140. cout << "Wrong input! " << endl;
  141. }
  142. else {
  143. int number1 = 0;
  144. int number2 = 0;
  145.  
  146. number1 = convertNumber(num1); //We call the function we use to convert our input from char to int
  147. number2 = convertNumber(num2);
  148.  
  149. switch (operation) //A switch for the operation
  150. {
  151. case '+':
  152. result = number1 + number2;
  153. break;
  154. case '-':
  155. result = number1 - number2;
  156. break;
  157. case '*':
  158. result = number1 * number2;
  159. break;
  160. case '/':
  161. result = number1 / number2;
  162. break;
  163. case '%':
  164. result = number1 % number2;
  165. break;
  166. default:
  167. cout << "Wrong input of operation! " << endl; //If the input of operation is not accurate it shows a message
  168. return 0;
  169. }
  170. int absResult = abs(result); //Takes the positive value of our result if it happens to be negative
  171. int left = 0;
  172. int firstDigit = 0;
  173. int secondDigit = 0;
  174.  
  175. firstDigit = absResult % 16; //The first digit of our result we get when we divide by 16
  176. left = absResult / 16;
  177.  
  178. if (left != 0) //If what we have left from our result divided by 16 is diggerent than 0 that means our result has two
  179. { //digits when converted to hex
  180. secondDigit = left ; //If it has 2 digits we save the value of the second one
  181. if (absResult != result) //If we have a negative result we display the result with a -
  182. {
  183. cout << "-" << fromDecToHex(secondDigit) << fromDecToHex(firstDigit) << endl; //We call a function to convert the digits to hex
  184. }
  185. else
  186. {
  187. cout << fromDecToHex(secondDigit) << fromDecToHex(firstDigit) << endl;
  188. }
  189.  
  190. }
  191. if (left == 0) //If what is left from the number divided by 16 is 0 we only have one digit
  192. { //and we proceed the same way as we did with 2 digits
  193. if (absResult != result)
  194. {
  195. cout << "-" << fromDecToHex(firstDigit) << endl;
  196. }
  197. else cout << fromDecToHex(firstDigit) << endl;
  198. }
  199. }
  200.  
  201. }
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement