Advertisement
wtmhahagd

accumulator++++

Feb 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. /*
  2.  
  3. Adam An WOW! * HOT *
  4. CSCI 304 | |_| |__ ___
  5. section 03 | __| '_ \ / _ \ edition 2.0 new look same great taste
  6. _ | |_| | | | __/ _ _ ___ ___ ___ ___
  7. / \ ___ _\__|_| |_|\___|__ _ _| | __ _| |_ _ __ ___ _ __ ( _ ) / _ \ / _ \ / _ \
  8. / _ \ / __/ __| | | | '_ ` _ \| | | | |/ _` | __| '__/ _ \| '_ \ / _ \| | | | | | | | | |
  9. / ___ \ (_| (__| |_| | | | | | | |_| | | (_| | |_| | | (_) | | | | | (_) | |_| | |_| | |_| |
  10. /_/ \_\___\___|\__,_|_| |_| |_|\__,_|_|\__,_|\__|_| \___/|_| |_| \___/ \___/ \___/ \___/
  11.  
  12. */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17.  
  18. unsigned short get_binary_op(char *bin) {
  19. short operand = 0;
  20.  
  21. int length = 16;
  22. int i;
  23. for (i = 0; i <= 16; i++) {
  24. if (*(bin + i) == '\0') {
  25. length = i - 1;
  26. break;
  27. }
  28. }
  29.  
  30. for (i = 0; i <= length; i++) {
  31. if (*(bin + i) == '1') {
  32. // operand += 2^(length - i)
  33. if (length - i == 0) {
  34. operand += 1;
  35. } else {
  36. int j;
  37. int powerOfTwo = 2;
  38. for (j = 1; j < length - i; j++) {
  39. powerOfTwo *= 2;
  40. }
  41. operand += powerOfTwo;
  42. }
  43. }
  44. }
  45.  
  46. return operand;
  47. }
  48.  
  49. void convert_to_binary(short acc, char *bin) {
  50. int i;
  51. for (i = 0; i <= 15; i++) {
  52. // get the ith bit of acc.
  53. short bit = (acc >> (16 - i - 1)) & 1;
  54.  
  55. if (bit == 0) {
  56. bin[i] = '0';
  57. } else if (bit == 1) {
  58. bin[i] = '1';
  59. }
  60. }
  61. }
  62.  
  63. short get_operand(char mode) {
  64. short operand;
  65. switch(mode) {
  66. case 'B':
  67. printf("Enter binary value: ");
  68. char binaryString[17];
  69.  
  70. int i;
  71. for (i = 0; i <= 16; i++) {
  72. binaryString[i] = '\0';
  73. }
  74.  
  75. scanf("%16s", &binaryString);
  76. operand = get_binary_op(&binaryString);
  77. break;
  78. case 'O':
  79. printf("Enter octal value: ");
  80. scanf(" %ho", &operand);
  81. break;
  82. case 'H':
  83. printf("Enter hexadecimal value: ");
  84. scanf(" %hx", &operand);
  85. break;
  86. case 'D':
  87. default:
  88. printf("Enter decimal value: ");
  89. scanf(" %hd", &operand);
  90. break;
  91. }
  92. return operand;
  93. }
  94.  
  95. void print_acc(short acc) {
  96. char binaryString[17];
  97. int i;
  98. for (i = 0; i <= 16; i++) {
  99. binaryString[i] = '0';
  100. }
  101.  
  102. convert_to_binary(acc, &binaryString);
  103.  
  104. printf("\n");
  105. printf("**************************************\n");
  106. printf("* Accumulator: *\n");
  107.  
  108. printf("* Binary : ");
  109. int digit;
  110. for (digit = 0; digit <= 3; digit++) {
  111. printf("%c", binaryString[digit]);
  112. }
  113. printf(" ");
  114. for (digit = 4; digit <= 7; digit++) {
  115. printf("%c", binaryString[digit]);
  116. }
  117. printf(" ");
  118. for (digit = 8; digit <= 11; digit++) {
  119. printf("%c", binaryString[digit]);
  120. }
  121. printf(" ");
  122. for (digit = 12; digit <= 15; digit++) {
  123. printf("%c", binaryString[digit]);
  124. }
  125. printf(" ");
  126. printf(" *\n");
  127.  
  128. printf("* Hex : %04hX *\n", acc);
  129. printf("* Octal : %06ho *\n", acc);
  130. printf("* Decimal : %-6d *\n", acc);
  131. printf("**************************************\n");
  132. }
  133.  
  134. char print_menu() {
  135. printf("\n");
  136. printf("Please enter your command:\n");
  137. printf("\n");
  138. printf("B Binary Mode & AND with Accumulator + Add to Accumulator\n");
  139. printf("O Octal Mode | OR with Accumulator - Subtract from Accumulator\n");
  140. printf("H Hexadecimal Mode ^ XOR with Accumulator N Negate Accumulator\n");
  141. printf("D Decimal Mode ~ Complement Accumulator\n");
  142. printf("C Clear Accumulator < Shift Accumulator left\n");
  143. printf("S Set Accumulator > Shift Accumulator right\n");
  144. printf("\n");
  145. printf("Q Quit Accumulator\n");
  146.  
  147. printf("\n");
  148. printf("Command: ");
  149.  
  150. char choice[17];
  151. scanf(" %s", &choice);
  152.  
  153. if (strlen(choice) != 1) {
  154. printf("Invalid option: %s\n", choice);
  155. return print_menu();
  156. }
  157.  
  158. return choice[0];
  159. }
  160.  
  161. void add(short * acc, char mode) {
  162. short operand = get_operand(mode);
  163. short result = * acc + operand;
  164.  
  165. if (* acc > 0 && operand > 0 && result < 0) {
  166. printf("Overflow Error");
  167. }
  168. if (* acc < 0 && operand < 0 && result > 0) {
  169. printf("Negative Overflow Error");
  170. }
  171.  
  172. * acc = result;
  173. }
  174.  
  175. void subtract(short * acc, char mode) {
  176. short operand = get_operand(mode);
  177. short result = * acc - operand;
  178.  
  179. if (* acc > 0 && operand < 0 && result < 0) {
  180. printf("Overflow Error");
  181. }
  182.  
  183. if (* acc < 0 && operand > 0 && result > 0) {
  184. printf("Negative Overflow Error");
  185. }
  186.  
  187. * acc = result;
  188. }
  189.  
  190. int main() {
  191. short accumulator = 0;
  192. char mode = 'D';
  193.  
  194. short operand;
  195. short result;
  196.  
  197. while (1) {
  198. print_acc(accumulator);
  199. char input_char = toupper(print_menu());
  200.  
  201. switch (input_char) {
  202. case '\n':
  203. break;
  204. case 'Q':
  205. return 0;
  206. break;
  207. case 'C':
  208. accumulator = 0;
  209. printf("accumulator cleared!\n");
  210. break;
  211. case 'S':
  212. accumulator = get_operand(mode);
  213. break;
  214. case 'B':
  215. mode = input_char;
  216. printf("Mode is binary!\n");
  217. break;
  218. case 'D':
  219. mode = input_char;
  220. printf("Mode is decimal!\n");
  221. break;
  222. case 'O':
  223. mode = input_char;
  224. printf("Mode is octal!\n");
  225. break;
  226. case 'H':
  227. mode = input_char;
  228. printf("Mode is hexadecimal!\n");
  229. break;
  230. case '&':
  231. accumulator &= get_operand(mode);
  232. break;
  233. case '|':
  234. accumulator |= get_operand(mode);
  235. break;
  236. case '^':
  237. accumulator ^= get_operand(mode);
  238. break;
  239. case '~':
  240. accumulator = ~accumulator;
  241. break;
  242. case '<':
  243. accumulator <<= get_operand(mode);
  244. break;
  245. case '>':
  246. accumulator >>= get_operand(mode);
  247. break;
  248. case '+':
  249. add(&accumulator, mode);
  250. break;
  251. case '-':
  252. subtract(&accumulator, mode);
  253. break;
  254. case 'N':
  255. accumulator *= -1;
  256. break;
  257. default:
  258. printf("Invalid option: %c\n", input_char);
  259. break;
  260. }
  261. }
  262. return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement