Advertisement
Guest User

Untitled

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