/* POSTNET Barcodes (15 points) Have you ever wondered why mail envelopes have barcodes printed on them? The United States Postal Service uses the POSTNET barcode to automate mail routing. The barcode encodes the zip code of the destination address as a sequence of long and short vertical bars. Here is an example | | | | | || | | | | | | | | | || | | || |||||||||||||||||||||||||||||||||||||||||||||||||||| There are several POSTNET versions. This problem only focuses on the "C" version, which uses 52 bars. Here is how it works. Every POSTNET barcode starts and ends with a full bar. This is commonly referred to as the guard rail. The remaining bars are grouped into sections of 5, each representing a single digit in the code. The table shows the encoding for each individual digit. The first 9 digits represent the ZIP code (5 zip + 4 zip extension). The 10-th digit, often called the checksum digit, is not part of the zip code, but it is used for error detection in case the barcode is unreadable or damaged. The checksum digit is calculated based on the previous nine digits such that when all ten digits are added the result should be a multiple of 10. If the sum is already a multiple of 10 the checksum digit is 0. Example: The barcode shown above encodes the zip code 55122-4428. Adding these nine digits we get 5+5+1+2+2+4+4+2+8=33. The tenth digit of the barcode must be 7, as 33+7=40 and 40 is a multiple of 10. In other words, 40%10=0. The figure below groups the individual digits. | | | | | || | | | | | | | | | | | | | | | | ||||| ||||| ||||| ||||| ||||| ||||| ||||| ||||| ||||| ||||| | 5 5 1 2 2 4 4 2 8 7 See also: http://en.wikipedia.org/wiki/POSTNET Write a complete C program that reads two ints from the keyboard. The first one represents the 5-digit zip code. The second one represents the 4-digit zip code extension. The program must print the POSTNET barcode for this 9-digit zip code using ASCII characters. You can use the '|' character to print a short vertical bar. Printing two of these on two consecutive lines will give you a long vertical bar. Here is an example of a sample run of the program: Enter 5-digit zip code: 50014 Enter 4-digit extension: 1234 50014-1234 in POSTNET is | | | || || || | | || | | || | ||| | |||||||||||||||||||||||||||||||||||||||||||||||||||| */ #include #include void printRow1(int n) { switch (n) { case 0: printf("|| "); break; case 1: printf(" ||"); break; case 2: printf(" | |"); break; case 3: printf(" || "); break; case 4: printf(" | |"); break; case 5: printf(" | | "); break; case 6: printf(" || "); break; case 7: printf("| |"); break; case 8: printf("| | "); break; case 9: printf("| | "); break; default: printf("Unexpected number %d\n", n); break; } //printf(" "); //for debugging only } void printRow2(int n) { printf("|||||"); // It is the same for all //printf(" "); //for debugging only } int main() { int zip; int ext; //zip_extension printf("Enter 5-digit zip code: "); scanf("%d", &zip); printf("Enter 4-digit extension: "); scanf("%d", &ext); printf("\n%05d-%04d in POSTNET is\n\n\n", zip, ext); // print leading 0's as well // extract the individual digits of the zip and ext int z1, z2, z3, z4, z5; z5= zip%10; zip/=10; z4= zip%10; zip/=10; z3= zip%10; zip/=10; z2= zip%10; zip/=10; z1= zip%10; int e1, e2, e3, e4; e4= ext%10; ext/=10; e3= ext%10; ext/=10; e2= ext%10; ext/=10; e1= ext%10; // calculate the checksum int checksum; int sum = z1+ z2+ z3+ z4+ z5 + e1+ e2+ e3+ e4; if(sum%10 == 0) checksum=0; else checksum = 10 - sum%10; // print 1st row printf("|"); // termination character printRow1(z1); printRow1(z2); printRow1(z3); printRow1(z4); printRow1(z5); printRow1(e1); printRow1(e2); printRow1(e3); printRow1(e4); printRow1(checksum); printf("|\n"); // termination character // print 2nd row printf("|"); // termination character printRow2(z1); printRow2(z2); printRow2(z3); printRow2(z4); printRow2(z5); printRow2(e1); printRow2(e2); printRow2(e3); printRow2(e4); printRow2(checksum); printf("|\n\n\n\n"); // termination character system("pause"); }