c++ - Trying to Minimize Repetitive code in Zip-to-Barcode program -
first time posting here if i'm doing wrong let me know...new whole programming thing.
so in class doing zip bar code converter program. have program running fine , trying cut down on redundant or repetitive coding. instance function runs actual compiling of zip code bar code have 5 switch statements run same exact cases. difference condition switch (digita, b, c, d, e; digits of zip code):
// short zip code converter function int zip_to_bar(int digita, int digitb, int digitc, int digitd, int digite) { int sum; string bar_code = "!", check_code; switch(digita) { case 0: bar_code += "!!..."; break; case 1: bar_code += "...!!"; break; case 2: bar_code += "..!.!"; break; case 3: bar_code += "..!!."; break; case 4: bar_code += ".!..!"; break; case 5: bar_code += ".!.!."; break; case 6: bar_code += ".!!.."; break; case 7: bar_code += "!...!"; break; case 8: bar_code += "!..!."; break; case 9: bar_code += "!.!.."; break; } switch(digitb) { case 0: bar_code += "!!..."; break; case 1: bar_code += "...!!"; break; case 2: bar_code += "..!.!"; break; case 3: bar_code += "..!!."; break; case 4: bar_code += ".!..!"; break; case 5: bar_code += ".!.!."; break; case 6: bar_code += ".!!.."; break; case 7: bar_code += "!...!"; break; case 8: bar_code += "!..!."; break; case 9: bar_code += "!.!.."; break; } switch(digitc) { case 0: bar_code += "!!..."; break; case 1: bar_code += "...!!"; break; case 2: bar_code += "..!.!"; break; case 3: bar_code += "..!!."; break; case 4: bar_code += ".!..!"; break; case 5: bar_code += ".!.!."; break; case 6: bar_code += ".!!.."; break; case 7: bar_code += "!...!"; break; case 8: bar_code += "!..!."; break; case 9: bar_code += "!.!.."; break; } switch(digitd) { case 0: bar_code += "!!..."; break; case 1: bar_code += "...!!"; break; case 2: bar_code += "..!.!"; break; case 3: bar_code += "..!!."; break; case 4: bar_code += ".!..!"; break; case 5: bar_code += ".!.!."; break; case 6: bar_code += ".!!.."; break; case 7: bar_code += "!...!"; break; case 8: bar_code += "!..!."; break; case 9: bar_code += "!.!.."; break; } switch(digite) { case 0: bar_code += "!!..."; break; case 1: bar_code += "...!!"; break; case 2: bar_code += "..!.!"; break; case 3: bar_code += "..!!."; break; case 4: bar_code += ".!..!"; break; case 5: bar_code += ".!.!."; break; case 6: bar_code += ".!!.."; break; case 7: bar_code += "!...!"; break; case 8: bar_code += "!..!."; break; case 9: bar_code += "!.!.."; break; } // sum of zip_digits sum = (digita + digitb + digitc + digitd + digite); // calculation of check_digit_code check_code = check_digit_code(sum); // assignment of check_bar_code check_digit_code cout << bar_code + check_code << endl; return sum; }
i somehow minimize 1 switch statement since same , run each digit in loop. reason wanting having run short(#####), standard (#####-####), & advanced (#####-####+##) format zip codes. can imagine program getting hellishly long , repetitive looking.
i thinking statement loop work nicely i'm stuck on what/how set conditions (what should initialized when have 5 variables present in function?). have got not going work how want have far (any advice on how handle problem)?
// reformatted version of short zip code converter function int zip_to_bar_srt(int digita, int digitb, int digitc, int digitd, int digite) { for(int digita; digita < 5; digita++) { switch(digita) { int sum; string barcode = "!", check_bar; case 0: barcode += "!!..."; break; case 1: barcode += "...!!"; break; case 2: barcode += "..!.!"; break; case 3: barcode += "..!!."; break; case 4: barcode += ".!..!"; break; case 5: barcode += ".!.!."; break; case 6: barcode += ".!!.."; break; case 7: barcode += "!...!"; break; case 8: barcode += "!..!."; break; case 9: barcode += "!.!.."; break; sum += digita; check_bar = check(sum); cout<<barcode+check_bar<<endl; return sum; } } }
make function give part of barcode single digit:
string get_barcode_part(int digit) { switch(digita) { case 0: return "!!...";; case 1: return "...!!";; case 2: return "..!.!";; case 3: return "..!!.";; case 4: return ".!..!";; case 5: return ".!.!.";; case 6: return ".!!..";; case 7: return "!...!";; case 8: return "!..!.";; case 9: return "!.!..";; default: return "invalid"; // or whatever else want } }
then can call each of digits:
// short zip code converter function int zip_to_bar(int digita, int digitb, int digitc, int digitd, int digite) { int sum; string bar_code = "!", check_code; bar_code += get_barcode_part(digita); bar_code += get_barcode_part(digitb); bar_code += get_barcode_part(digitc); bar_code += get_barcode_part(digitd); bar_code += get_barcode_part(digite); // sum of zip_digits sum = (digita + digitb + digitc + digitd + digite); // calculation of check_digit_code check_code = check_digit_code(sum); // assignment of check_bar_code check_digit_code cout << bar_code + check_code << endl; return sum; }
this essential function of functions (heh): refactor , reduce copied code, putting common functionality in common place.
edit: in addition, should use array digits, so:
// short zip code converter function int zip_to_bar(int *digits, int numdigits) { int sum = 0; string bar_code = "!", check_code; (int = 0; < numdigits; i++) { bar_code += get_barcode_part(digits[i]); // sum of zip_digits sum += digits[i]; } // calculation of check_digit_code check_code = check_digit_code(sum); // assignment of check_bar_code check_digit_code cout << bar_code + check_code << endl; return sum; }
Comments
Post a Comment