Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Really appreciate your help. Will rate fast. The U.S. Postal Service prints a ba

ID: 3643495 • Letter: R

Question

Really appreciate your help. Will rate fast.

The U.S. Postal Service prints a bar code on every envelope that represents a five (or more) digit zip code using a format called POSTNET (this format is being deprecated in favor of a new system, OneCode, in 2009). The bar code consists of long and short bars as shown: For this program, we will represent the bar code as a string of digits. The digit 1 represents a long bar, and the digit 0 represents a short bar. Therefore, the bar code would be represented in our program as: 110100101000101011000010011 The first and last digits of the bar code are always 1. Removing these 25 digits If these 25 digits are split into groups of five digits each. 10100 10100 01010 11000 01002 Next, consider each group of five digits. There will always I'sin each group of digits Each digit stands for.a number. From the digits encode the values 7, 4, 2, 1. and 0. Multiply the cc value with the digit and compute the sum to get the final encoded for the zip code The table below shows the encoding for 10100. Zip Code Digit = 7 +0 + 2 + 0 + 0= 9 Repeat ill is for each group of five digits and concatenate to complete zip code. There is one special value If the sum of a; digits is 11, then this represents the digit 0 (this is necessary because; two digits per group it is not possible to represent zero) The zip the sample bar code decodes to 995001 While the POSINET scheme seem unnecessarily complex, its design allows machines to detect if have been made in scanning the zip code. Write a zip code class that encodes and decodes five-digit bar codes i by die U.S. Postal Service on envelopes. The class should constructors. the first constructor should input the zip code as an integer and the second constructor should input the zip code as a ban consisting of 0's and 1 's, as described above. Although you have two ways to input die zip code, internally, the class should only store the: using one format (you may choose to store ii as a bar code siring or as a zip code number.) The class should also have at least two public member functions, one to return the zip code as an integer, and the other to return the zip code in bar code format as a string. All helper functions declared private. Embed your class definition in a suitable test program. Your program should print an error message if an invalid bar passed to the constructor.

Explanation / Answer

#include #include #include #include #include #include #include #include #define FALSE 0 #define TRUE 1 #define BAUDRATE B38400 volatile int STOP=FALSE; void signal_handler_IO (int status); //definition of signal handler int wait_flag=TRUE; //TRUE while no signal received long Baud_Rate = 38400; // default Baud Rate (110 through 38400) long BAUD; // derived baud rate from command line long DATABITS; long STOPBITS; long PARITYON; long PARITY; int Data_Bits = 8; // Number of data bits int Stop_Bits = 1; // Number of stop bits int Parity = 0; // Parity as follows: // 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space int fd, tty, error; struct termios oldtio, newtio; //place for old and new port settings for serial port struct termios oldkey, newkey; //place tor old and new port settings for keyboard teletype struct sigaction saio; //definition of signal action /*----------------------------------------------------------------------------* * Serial port: initialise io_port, set baud rate, set data bits, one stop bit*/ int rs_initialise (int io_port, const long int BaudRate, const char parity, const char data) { // set new port settings for non-canonical input processing //must be NOCTTY newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newkey.c_iflag = IGNPAR; newkey.c_oflag = 0; newkey.c_lflag = 0; //ICANON; newkey.c_cc[VMIN]=1; newkey.c_cc[VTIME]=0; tcflush(tty, TCIFLUSH); tcsetattr(tty,TCSANOW,&newkey); switch (BaudRate) { case 38400: default: BAUD = B38400; break; case 19200: BAUD = B19200; break; case 9600: BAUD = B9600; break; case 4800: BAUD = B4800; break; case 2400: BAUD = B2400; break; case 1800: BAUD = B1800; break; case 1200: BAUD = B1200; break; case 600: BAUD = B600; break; case 300: BAUD = B300; break; case 200: BAUD = B200; break; case 150: BAUD = B150; break; case 134: BAUD = B134; break; case 110: BAUD = B110; break; case 75: BAUD = B75; break; case 50: BAUD = B50; break; } //end of switch baud_rate switch (Data_Bits) { case 8: default: DATABITS = CS8; break; case 7: DATABITS = CS7; break; case 6: DATABITS = CS6; break; case 5: DATABITS = CS5; break; } //end of switch data_bits switch (Stop_Bits) { case 1: default: STOPBITS = 0; break; case 2: STOPBITS = CSTOPB; break; } //end of switch stop bits switch (Parity) { case 0: default: //none PARITYON = 0; PARITY = 0; break; case 1: //odd PARITYON = PARENB; PARITY = PARODD; break; case 2: //even PARITYON = PARENB; PARITY = 0; break; } //end of switch parity //open the device(com port) to be non-blocking (read will return immediately) fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror("/dev/ttyS0"); return 0; } //install the serial handler before making the device asynchronous saio.sa_handler = signal_handler_IO; sigemptyset(&saio.sa_mask); //saio.sa_mask = 0; saio.sa_flags = 0; saio.sa_restorer = NULL; sigaction(SIGIO,&saio,NULL); // allow the process to receive SIGIO fcntl(fd, F_SETOWN, getpid()); // Make the file descriptor asynchronous (the manual page says only // O_APPEND and O_NONBLOCK, will work with F_SETFL...) fcntl(fd, F_SETFL, O_ASYNC);//FASYNC); tcgetattr(fd,&oldtio); // save current port settings // set new port settings for canonical input processing newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; //ICANON; newtio.c_cc[VMIN]=1; newtio.c_cc[VTIME]=0; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); return 1; // OK } /*----------------------------------------------------------------------------* * Serial port: terminate io_port, sets DTR and RTS to low */ void rs_terminate(const int io_port) { // restore old port settings tcsetattr(fd,TCSANOW,&oldtio); tcsetattr(tty,TCSANOW,&oldkey); close(fd); //close the com port } /*----------------------------------------------------------------------------* * Serial port: read character from io_port (ignored in this version) */ char rs_getch(const int io_port) { static char buf[255]; // buffer for where data is put static int index = 0, count = 0; // check if data already in input buffer - if so return a character if(count>0) { count--; return buf[index++]; } // otherwise check if any characters receivedfrom serial line if (wait_flag==FALSE) //if input is available { count = read(fd,buf,255); wait_flag = TRUE; /* wait for new input */ index=0; count--; return buf[index++]; // return first character } return 0; // return 0 if no character read } /*----------------------------------------------------------------------------* * Serial port: transmit character to io_port */ void rs_putch(const int io_port, const int txchar) { write(fd,&txchar,1); //write 1 byte to the port } /*----------------------------------------------------------------------------* * Serial port: transmit a string of characters to io_port */ void rs_putstring(const int io_port, const char *string) { while (*string != '') rs_putch(io_port, *string++); } /************************************************** ************************* * signal handler. sets wait_flag to FALSE, to indicate above loop that * * characters have been received. * ************************************************** *************************/ void signal_handler_IO (int status) { //printf("received SIGIO signal. "); wait_flag = FALSE; } // -------------------------------------------------------------------------------------// FILE *input; FILE *output; // open keyboard/screen terminal functions void terminal_open() { input = fopen("/dev/tty", "r"); //open the terminal keyboard output = fopen("/dev/tty", "w"); //open the terminal screen if (!input || !output) { fprintf(stderr, "Unable to open /dev/tty "); exit(1); } tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK); //set the user console port up tcgetattr(tty,&oldkey); // save current port settings //so commands are interpreted right for this program fputs("Linux terminal program ",output); //display the program introduction } // close keyboard/screen void terminal_close() { close(tty); } // output character to screen void terminal_putchar(int ch) { fputc(ch,output); fflush(output); } // if keyboard was hit return character else 0 int terminal_getchar() { int status, Key; status = fread(&Key,1,1,input); if (status==1) return Key; //if a key was hit else return 0; // else return 0 } // main terminal program int main() { int port = 1; terminal_open(); error=0; // open the RS232 serial line if(!rs_initialise(port ,9600, '8', 'N')) { exit(1); } char letter; while(!STOP) { // if keyboard hit read character and transmit it if((letter=terminal_getchar())>0) // if character received display it { if(letter == 0x1b) break; // Esc to EXIT program terminal_putchar(letter); // remove for no echo rs_putch(port, letter); } if((letter=rs_getch(port))>0) // if character received display it { terminal_putchar(letter);; if(letter==' ') terminal_putchar(' '); } } rs_terminate(port); terminal_close(); return 0; }