Advertisement
wtmhahagd

accumulator

Feb 14th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. short get_operand(char mode) {
  5.     short operand;
  6.     switch(mode) {
  7.         case 'O':
  8.             printf("Enter octal value: ");
  9.             scanf(" %o", &operand);
  10.             break;
  11.         case 'H':
  12.             printf("Enter hexadecimal value: ");
  13.             scanf(" %hx", &operand);
  14.             break;
  15.         case 'D':
  16.         default:
  17.             printf("Enter decimal value: ");
  18.             scanf(" %d", &operand);
  19.             break;
  20.     }
  21.     return operand;
  22. }
  23.  
  24. void print_acc(short acc) {
  25.     printf("\n");
  26.     printf("**************************************\n");
  27.     printf("* Accumulator:                       *\n");
  28.     printf("*   Hex     :  %04hX                  *\n", acc);
  29.     printf("*   Octal   :  %06ho                *\n", acc);
  30.     printf("*   Decimal :  %-5d                 *\n", acc);
  31.     printf("**************************************\n");
  32. }
  33.  
  34. char print_menu() {
  35.     printf("\n");
  36.     printf("Please enter your command:\n");
  37.     printf("\n");
  38.     printf("D   Decimal Mode\n");
  39.     printf("O   Octal Mode\n");
  40.     printf("H   Hexadecimal Mode\n");
  41.     printf("\n");
  42.     printf("C   Clear accumulator\n");
  43.     printf("S   Set accumulator\n");
  44.     printf("\n");
  45.     printf("Q   Quit accumulator\n");
  46.    
  47.     printf("\n");
  48.     printf("Command: ");
  49.    
  50.     char choice;
  51.     scanf(" %c", &choice);
  52.     return choice;
  53. }
  54.  
  55. int main() {
  56.     short accumulator = 0;
  57.     char mode = 'D';
  58.    
  59.     while (1) {
  60.         print_acc(accumulator);
  61.         char input_char = toupper(print_menu());
  62.        
  63.         switch (input_char) {
  64.             case '\n':
  65.                 break;
  66.             case 'Q':
  67.                 return 0;
  68.                 break;
  69.             case 'C':
  70.                 accumulator = 0;
  71.                 printf("accumulator cleared!\n");
  72.                 break;
  73.             case 'S':
  74.                 accumulator = get_operand(mode);
  75.                 break;
  76.             case 'D':
  77.                 mode = input_char;
  78.                 printf("Mode is decimal!\n");
  79.                 break;
  80.             case 'O':
  81.                 mode = input_char;
  82.                 printf("Mode is octal!\n");
  83.                 break;
  84.             case 'H':
  85.                 mode = input_char;
  86.                 printf("Mode is hexadecimal!\n");
  87.                 break;
  88.             default:
  89.                 printf("Invalid input! :(\n");
  90.                 break;
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement