Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Adam An WOW! * HOT *
- CSCI 304 | |_| |__ ___
- section 03 | __| '_ \ / _ \ edition 2.0 new look same great taste
- _ | |_| | | | __/ _ _ ___ ___ ___ ___
- / \ ___ _\__|_| |_|\___|__ _ _| | __ _| |_ _ __ ___ _ __ ( _ ) / _ \ / _ \ / _ \
- / _ \ / __/ __| | | | '_ ` _ \| | | | |/ _` | __| '__/ _ \| '_ \ / _ \| | | | | | | | | |
- / ___ \ (_| (__| |_| | | | | | | |_| | | (_| | |_| | | (_) | | | | | (_) | |_| | |_| | |_| |
- /_/ \_\___\___|\__,_|_| |_| |_|\__,_|_|\__,_|\__|_| \___/|_| |_| \___/ \___/ \___/ \___/
- */
- #include <stdio.h>
- #include <ctype.h>
- unsigned short get_binary_op(char *bin) {
- short operand = 0;
- int length = 16;
- int i;
- for (i = 0; i <= 16; i++) {
- if (*(bin + i) == '\0') {
- length = i - 1;
- break;
- }
- }
- for (i = 0; i <= length; i++) {
- if (*(bin + i) == '1') {
- // operand += 2^(length - i)
- if (length - i == 0) {
- operand += 1;
- } else {
- int j;
- int powerOfTwo = 2;
- for (j = 1; j < length - i; j++) {
- powerOfTwo *= 2;
- }
- operand += powerOfTwo;
- }
- }
- }
- return operand;
- }
- void convert_to_binary(short acc, char *bin) {
- int i;
- for (i = 0; i <= 15; i++) {
- // get the ith bit of acc.
- short bit = (acc >> (16 - i - 1)) & 1;
- if (bit == 0) {
- bin[i] = '0';
- } else if (bit == 1) {
- bin[i] = '1';
- }
- }
- }
- short get_operand(char mode) {
- short operand;
- switch(mode) {
- case 'B':
- printf("Enter binary value: ");
- char binaryString[17];
- int i;
- for (i = 0; i <= 16; i++) {
- binaryString[i] = '\0';
- }
- scanf("%16s", &binaryString);
- operand = get_binary_op(&binaryString);
- break;
- case 'O':
- printf("Enter octal value: ");
- scanf(" %ho", &operand);
- break;
- case 'H':
- printf("Enter hexadecimal value: ");
- scanf(" %hx", &operand);
- break;
- case 'D':
- default:
- printf("Enter decimal value: ");
- scanf(" %hd", &operand);
- break;
- }
- return operand;
- }
- void print_acc(short acc) {
- char binaryString[17];
- int i;
- for (i = 0; i <= 16; i++) {
- binaryString[i] = '0';
- }
- convert_to_binary(acc, &binaryString);
- printf("\n");
- printf("**************************************\n");
- printf("* Accumulator: *\n");
- printf("* Binary : ");
- int digit;
- for (digit = 0; digit <= 3; digit++) {
- printf("%c", binaryString[digit]);
- }
- printf(" ");
- for (digit = 4; digit <= 7; digit++) {
- printf("%c", binaryString[digit]);
- }
- printf(" ");
- for (digit = 8; digit <= 11; digit++) {
- printf("%c", binaryString[digit]);
- }
- printf(" ");
- for (digit = 12; digit <= 15; digit++) {
- printf("%c", binaryString[digit]);
- }
- printf(" ");
- printf(" *\n");
- printf("* Hex : %04hX *\n", acc);
- printf("* Octal : %06ho *\n", acc);
- printf("* Decimal : %-6d *\n", acc);
- printf("**************************************\n");
- }
- char print_menu() {
- printf("\n");
- printf("Please enter your command:\n");
- printf("\n");
- printf("B Binary Mode & AND with Accumulator + Add to Accumulator\n");
- printf("O Octal Mode | OR with Accumulator - Subtract from Accumulator\n");
- printf("H Hexadecimal Mode ^ XOR with Accumulator N Negate Accumulator\n");
- printf("D Decimal Mode ~ Complement Accumulator\n");
- printf("C Clear Accumulator < Shift Accumulator left\n");
- printf("S Set Accumulator > Shift Accumulator right\n");
- printf("\n");
- printf("Q Quit Accumulator\n");
- printf("\n");
- printf("Command: ");
- char choice[17];
- scanf(" %s", &choice);
- if (strlen(choice) != 1) {
- printf("Invalid option: %s\n", choice);
- return print_menu();
- }
- return choice[0];
- }
- void add(short * acc, char mode) {
- short operand = get_operand(mode);
- short result = * acc + operand;
- if (* acc > 0 && operand > 0 && result < 0) {
- printf("Overflow Error");
- }
- if (* acc < 0 && operand < 0 && result > 0) {
- printf("Negative Overflow Error");
- }
- * acc = result;
- }
- void subtract(short * acc, char mode) {
- short operand = get_operand(mode);
- short result = * acc - operand;
- if (* acc > 0 && operand < 0 && result < 0) {
- printf("Overflow Error");
- }
- if (* acc < 0 && operand > 0 && result > 0) {
- printf("Negative Overflow Error");
- }
- * acc = result;
- }
- int main() {
- short accumulator = 0;
- char mode = 'D';
- short operand;
- short result;
- 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 'B':
- mode = input_char;
- printf("Mode is binary!\n");
- 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;
- case '&':
- accumulator &= get_operand(mode);
- break;
- case '|':
- accumulator |= get_operand(mode);
- break;
- case '^':
- accumulator ^= get_operand(mode);
- break;
- case '~':
- accumulator = ~accumulator;
- break;
- case '<':
- accumulator <<= get_operand(mode);
- break;
- case '>':
- accumulator >>= get_operand(mode);
- break;
- case '+':
- add(&accumulator, mode);
- break;
- case '-':
- subtract(&accumulator, mode);
- break;
- case 'N':
- accumulator *= -1;
- break;
- default:
- printf("Invalid option: %c\n", input_char);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement