我必须编写一个程序,用户输入一个zipcode并输出带有校验位的条形码。我能够让程序打印正确的条形码,但我不确定程序是否打印条形码和结果的邮政编码。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string Bar_Code(char digit)
/* Write a program that asks the user for a zip code and prints the
bar code. Use : for half bars, | for full bars.*/
{
if (digit == '0' ){
return "||:::";
else if (digit == '1')
return ":::||";
else if (digit == '2')
return "::|:|";
else if (digit == '3')
return "::||:";
else if (digit == '4')
return ":|::|";
else if (digit == '5')
return ":|:|:";
else if (digit == '6')
return ":||::";
else if (digit == '7')
return "|:::|";
else if (digit == '8')
return "|::|:";
else if (digit == '9')
return "|:|::";
else
return "Invalid";
}
string user_input;
string result = "";
int digits[5];
int sum = 0;
cout << "Enter zipcode and the check digit: ";
/* check digit is the sum of all the digits + check digit to make
the sum a multiple of 10 */
cin >> user_input;
for(int i=0; i < user_input.length();i++)
{
digit[i] = user_input[i] - '0';
string current_barcode = Bar_Code(user_input[i]);
result += current_barcode;
sum += digits[i];
}
int remainder = sum % 10;
int checkDigit = 10 - remainder;
result += Bar_Code(checkDigit + '0');
cout << checkDigit << endl;
cout << endl;
return 0;
}