Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- short get_operand(char mode) {
- short operand;
- switch(mode) {
- case 'O':
- printf("Enter octal value: ");
- scanf(" %o", &operand);
- break;
- case 'H':
- printf("Enter hexadecimal value: ");
- scanf(" %hx", &operand);
- break;
- case 'D':
- default:
- printf("Enter decimal value: ");
- scanf(" %d", &operand);
- break;
- }
- return operand;
- }
- void print_acc(short acc) {
- printf("\n");
- printf("**************************************\n");
- printf("* Accumulator: *\n");
- printf("* Hex : %04hX *\n", acc);
- printf("* Octal : %06ho *\n", acc);
- printf("* Decimal : %-5d *\n", acc);
- printf("**************************************\n");
- }
- char print_menu() {
- printf("\n");
- printf("Please enter your command:\n");
- printf("\n");
- printf("D Decimal Mode\n");
- printf("O Octal Mode\n");
- printf("H Hexadecimal Mode\n");
- printf("\n");
- printf("C Clear accumulator\n");
- printf("S Set accumulator\n");
- printf("\n");
- printf("Q Quit accumulator\n");
- printf("\n");
- printf("Command: ");
- char choice;
- scanf(" %c", &choice);
- return choice;
- }
- int main() {
- short accumulator = 0;
- char mode = 'D';
- while (1) {
- print_acc(accumulator);
- char input_char = toupper(print_menu());
- switch (input_char) {
- case '\n':
- break;
- case 'Q':
- return 0;
- break;
- case 'C':
- accumulator = 0;
- printf("accumulator cleared!\n");
- break;
- case 'S':
- accumulator = get_operand(mode);
- break;
- case 'D':
- mode = input_char;
- printf("Mode is decimal!\n");
- break;
- case 'O':
- mode = input_char;
- printf("Mode is octal!\n");
- break;
- case 'H':
- mode = input_char;
- printf("Mode is hexadecimal!\n");
- break;
- default:
- printf("Invalid input! :(\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement