/**************************************************************** * Program: binadd.c * * Takes as input two 32-bit binary numbers and prints * * out their sum. Handles overflow and underflow * * errors. * * * * Author: Ben Bartlett, Drawer 425, x2021, bartlbe@earlham.edu * ****************************************************************/ #include /* Include the standard libary */ #include /* Include the standard input-output library */ int check_string(char str[50]); /* Checks to make certain that the string is 32 bits and contains only 0 and 1 characters */ void input(char first[50], char second[50]); /* Gets the user input */ /* Adds the two binary strings together */ void add(char first[50], char second[50], char sum[50]); int main() { char first[50], second[50], sum[50]; /* First and second hold the two strings to be added; sum holds their sum */ input(first, second); /* Get the two binary strings from the user */ add(first, second, sum); /* Add the two binary strings */ /* Print the two strings */ printf("\nThe sum of those two numbers are:\n"); printf("%s\n", sum); /* Exit the program */ exit(0); } /* Checks to make sure that a string is 32 chars and that it only holds binary characters */ int check_string(char str[50]) { int i; /* Counter */ /* Check to make sure length is 32 */ if((strlen(str) < 32) || (strlen(str) > 32)) return 2; /* Make sure all chars are binary */ for(i = 0; i < strlen(str); i++) { if((str[i] != '0') && (str[i] != '1')) { printf("Nonbinary char: %c\n", str[i]); return 1; } } return 0; } /* Gets the two binary strings from the user */ void input(char first[50], char second[50]) { int check; /* Holds the check return from check_string() */ char newline; /* Holds the newline char */ /* Get the first binary string */ printf("Enter first 32-bit binary number:\n"); scanf("%s%c", first, &newline); check = check_string(first); /* If the first string does not have 32 chars, or has a non-binary char, print an error message and exit the program */ if(check == 1) { printf("Error: Non-binary character.\n"); exit(0); } if(check == 2) { printf("Error: String is not 32-bits.\n"); printf("%d chars in string.\n", strlen(first)); exit(0); } /* Get the second binary string */ printf("\nEnter second 32-bit binary number:\n"); scanf("%s%c", second, &newline); check = check_string(second); /* If the second binary string does not have 32 chars or has a non-binary char, print an error message and exit the program */ if(check == 1) { printf("Error: Non-binary character.\n"); exit(0); } if(check == 2) { printf("Error: String is not 32-bits.\n"); exit(0); } } /* Add the two binary strings input by the user, and place the result in sum[]. Handles overflow and underflow error. */ void add(char first[50], char second[50], char sum[50]) { int i; /* Counter */ int carry = 0; /* 0 means nothing is to be carried, 1 means that a a 1 is to be carried */ /* The following loop goes through, checks the equivalent chars in first[] and second[], and changes the carry value and the equivalent char in sum[] accordingly. The loop also checks for overflow and underflow error. */ for(i = 31; i >= 0; i--) { if((((first[i] == '1') && (second[i] == '0')) || ((first[i] == '0') && (second[i] == '1'))) && (carry == 0)) { sum[i] = '1'; } else if((((first[i] == '1') && (second[i] == '0')) || ((first[i] == '0') && (second[i] == '1'))) && (carry == 1)) { sum[i] = '0'; } else if((first[i] == '1') && (second[i] == '1') && (carry == 0)) { /* Check for underflow error */ if(i == 0) { printf("\nUnderflow error.\n"); exit(0); } sum[i] = '0'; carry = 1; } else if((first[i] == '1') && (second[i] == '1') && (carry == 1)) { sum[i] = '1'; } else if((first[i] == '0') && (second[i] == '0') && (carry == 0)) { sum[i] = '0'; } else if((first[i] == '0') && (second[i] == '0') && (carry == 1)) { /* Check for overflow error */ if(i == 0) { printf("\nOverflow error.\n"); exit(0); } sum[i] = '1'; carry = 0; } } sum[32] = '\0'; /* Set the final char in sum[] to null, so that it works like a string */ }