C++ code:
#include <string>
#include <iostream>
using namespace std;
string numberToBarcode(int);
int main() {
int zip;
int num1, num2, num3, num4, num5, checkNum;
int tempNum;
int checkTotal;
string barcode = "|"; // initialized with a front framing bar
cout << "Please enter a 5 digit zip code.\n"
<< " --> ";
cin >> zip; // prompt for zip code
// check if valid zip code
if (zip >= 100000 || zip < 0) {
cout << "Error: Not a zip code.\n";
return 0;
}
tempNum = zip;
//parsing each digit to determine check value
// the parsing is done in reverse order, so the digits are stored in reverse order
num5 = tempNum % 10;
tempNum = tempNum / 10;
num4 = tempNum % 10;
tempNum = tempNum / 10;
num3 = tempNum % 10;
tempNum = tempNum / 10;
num2 = tempNum % 10;
tempNum = tempNum / 10;
num1 = tempNum % 10;
tempNum = tempNum / 10;
// generate check num
checkTotal = num1 + num2 + num3 + num4 + num5;
checkNum = (10-(checkTotal % 10))%10; // the last %10 is to deal with cases where checkTotal%10 == 0
// turn the zip into a barcode, digit by digit
barcode += numberToBarcode(num1);
barcode += numberToBarcode(num2);
barcode += numberToBarcode(num3);
barcode += numberToBarcode(num4);
barcode += numberToBarcode(num5);
barcode += numberToBarcode(checkNum);
// final framing bar
barcode += "|";
cout << "\nYour zip code's barcode is:\n"
<< " " << barcode << "\n";
return 0;
}
string numberToBarcode(int num) {
string barcode = "";
int dig; // the single digit being converted
int tempNum; // temporary variable
int /*bc7=0, bc4=0, bc2=0, bc1=0, bc0=0,*/ bcTotal=0;
tempNum = num;
if(tempNum >= 10) {
// if the digit is actually multiple digits, this function will
// recursively determine the multiple barcode sections they
// represent and return them together
// this strips the last digit...
dig = tempNum%10;
tempNum /= 10;
// then recursively strips each digit and adds their barcodes together
barcode = numberToBarcode(tempNum);
} else {
dig = tempNum;
}
// reusing the temp variable
tempNum = dig;
if (dig == 0) { // 0 is a special case
barcode += "||:::";
} else { // otherwise...
if (tempNum / 7 == 1 && bcTotal < 2) { // check for 1st bar - 7s digit
barcode += "|"; // if true, full bar
tempNum -= 7;
bcTotal++;
} else
barcode += ":"; // if false, half bar
if (tempNum / 4 == 1 && bcTotal < 2) { // check for 2nd bar - 4s digit
barcode += "|";
tempNum -= 4;
bcTotal++;
} else
barcode += ":";
if (tempNum / 2 == 1 && bcTotal < 2) { // check for 3rd bar - 2s digit
barcode += "|";
tempNum -= 2;
bcTotal++;
} else
barcode += ":";
if (tempNum / 1 == 1 && bcTotal < 2) { // check for 4th bar - 1s digit
barcode += "|";
tempNum -= 1;
bcTotal++;
} else
barcode += ":";
if (bcTotal < 2) { // check for last bar - 0s digit - This one just takes up slack
barcode += "|";
bcTotal++;
} else
barcode += ":";
}
/*
if (dig == 0) { // 0 is a special case
bc7 = 1;
bc4 = 1;
bcTotal = 2;
} else { // otherwise...
if (tempNum / 7 == 1 && bcTotal < 2) { // check for 1st bar - 7s digit
bc7 = 1;
tempNum -= 7;
bcTotal++;
}
if (tempNum / 4 == 1 && bcTotal < 2) { // check for 2nd bar - 4s digit
bc4 = 1;
tempNum -= 4;
bcTotal++;
}
if (tempNum / 2 == 1 && bcTotal < 2) { // check for 3rd bar - 2s digit
bc2 = 1;
tempNum -= 2;
bcTotal++;
}
if (tempNum / 1 == 1 && bcTotal < 2) { // check for 4th bar - 1s digit
bc1 = 1;
tempNum -= 1;
bcTotal++;
}
if (bcTotal < 2) { // check for last bar - 0s digit - This one just takes up slack
bc0 = 1;
bcTotal++;
}
}
*/
return barcode;
}
</iostream></string>
Java Code:
import java.util.Scanner;
public class zipBar {
String numberToBarcode(int n1){
return null;
}
public static void main(String[] args) {
int z;
int n1, n2, n3, n4, n5, cN;
int tN;
int cT;
String barcode = "|";
System.out.println("Enter zip code: ");
Scanner zip = new Scanner(System.in);
z = zip.nextInt();
if (z >= 10000 || z < 0)
{
System.out.println("Input Error: Input not a valid zip code");
}
tN = z;
n5 = tN % 10;
tN = tN / 10;
n4 = tN % 10;
tN = tN / 10;
n3 = tN % 10;
tN = tN / 10;
n2 = tN % 10;
tN = tN / 10;
n1 = tN % 10;
tN = tN / 10;
cT = n1 + n2 + n3 + n4 +5;
cN = (10-(cT % 10)) % 10;
barcode += numberToBarcode(n1);
}
}

New Topic/Question
Reply



MultiQuote






|