Last active
August 29, 2015 14:12
-
-
Save chitoku-k/adc65b4717de80d743ca to your computer and use it in GitHub Desktop.
JAN コードチェックプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define to_int(code, digit, length) do { \ | |
for (int i = 0; i < length; i++) \ | |
{ \ | |
code[i] = digit[i] - 48; \ | |
} \ | |
} while (0) | |
#include <stdio.h> | |
#include <string.h> | |
int input_text(char *buf, int length) | |
{ | |
fgets(buf, length, stdin); | |
int len = strlen(buf); | |
buf[len - 1] = '\0'; | |
fflush(stdin); | |
return len - 1; | |
} | |
int check_digit(const char *digit, int length) | |
{ | |
int code[13] = { 0 }; | |
to_int(code, digit, length); | |
int check; | |
if (length == 8) | |
{ | |
check = (code[6] + code[4] + code[2] + code[0]) * 3 + code[5] + code[3] + code[1]; | |
} | |
if (length == 13) | |
{ | |
check = (code[11] + code[9] + code[7] + code[5] + code[3] + code[1]) * 3 + code[10] + code[8] + code[6] + code[4] + code[2] + code[0]; | |
} | |
check = check % 10 == 0 ? 0 : 10 - check % 10; | |
return check == code[length - 1]; | |
} | |
void get_code(const char *jancode, char *buf, int length) | |
{ | |
int code[13] = { 0 }; | |
to_int(code, jancode, length); | |
if (length == 8) | |
{ | |
sprintf_s(buf, 64, "国コード:%d%d\nメーカコード:%d%d%d%d\n商品コード:%d", code[0], code[1], code[2], code[3], code[4], code[5], code[6]); | |
} | |
if (length == 13) | |
{ | |
if (code[0] == 9 && code[1] == 7) | |
{ | |
sprintf_s(buf, 64, "ISBN:%d%d%d%d%d%d%d%d%d", code[3], code[4], code[5], code[6], code[7], code[8], code[9], code[10], code[11]); | |
} | |
else if (code[0] == 1 && code[1] == 9) | |
{ | |
sprintf_s(buf, 64, "分類コード:%d%d%d%d\n価格コード:%d%d%d%d%d", code[3], code[4], code[5], code[6], code[7], code[8], code[9], code[10], code[11]); | |
} | |
else | |
{ | |
sprintf_s(buf, 64, "国コード:%d%d\nメーカコード:%d%d%d%d\n商品コード:%d%d%d%d%d", code[0], code[1], code[2], code[3], code[4], code[5], code[6], code[7], code[8], code[9], code[10], code[11]); | |
} | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
char buf[64] = { '\0' }; | |
char code[16] = { '\0' }; | |
while (1) | |
{ | |
printf("JAN コードを入力してください: "); | |
int length = input_text(code, sizeof(code)); | |
if (code[0] == 0x04) | |
{ | |
break; | |
} | |
if (length != 8 && length != 13) | |
{ | |
printf("JAN コードの桁数が正しくありません。\n\n"); | |
continue; | |
} | |
if (!check_digit(code, length)) | |
{ | |
printf("JAN コードが正しくありません。\n\n"); | |
continue; | |
} | |
get_code(code, buf, length); | |
printf("%s\n\n", buf); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace JanCodeCheckerSharp | |
{ | |
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
while (true) | |
{ | |
try | |
{ | |
Console.Write("JAN コードを入力してください: "); | |
string code = Console.ReadLine(); | |
if (code == "\x04") | |
{ | |
return; | |
} | |
Console.WriteLine(GetCode(code.Select(x => int.Parse(x.ToString())).ToArray()) + "\n"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message + "\n"); | |
} | |
} | |
} | |
static void ValidateCheckDigit(int checkDigit, int lastDigit) | |
{ | |
checkDigit = checkDigit % 10 == 0 ? 0 : 10 - checkDigit % 10; | |
if (checkDigit != lastDigit) | |
{ | |
throw new ArgumentException("JAN コードが正しくありません。"); | |
} | |
} | |
static string GetCode(IList<int> digits) | |
{ | |
if (digits.Count == 8) | |
{ | |
ValidateCheckDigit((digits[6] + digits[4] + digits[2] + digits[0]) * 3 + digits[5] + digits[3] + digits[1], digits[digits.Count - 1]); | |
return string.Format("国コード:{0}\nメーカコード:{1}\n商品コード:{2}", digits.Take(2).ToDisplayString(), digits.Skip(2).Take(4).ToDisplayString(), digits.Skip(6).Take(1).ToDisplayString()); | |
} | |
else if (digits.Count == 13) | |
{ | |
ValidateCheckDigit((digits[11] + digits[9] + digits[7] + digits[5] + digits[3] + digits[1]) * 3 + digits[10] + digits[8] + digits[6] + digits[4] + digits[2] + digits[0], digits[digits.Count - 1]); | |
if (digits[0] == 9 && digits[1] == 7) | |
{ | |
return "ISBN:" + digits.Skip(3).Take(9).ToDisplayString(); | |
} | |
if (digits[0] == 1 && digits[1] == 9) | |
{ | |
return string.Format("分類コード:{0}\n価格コード:{1}", digits.Skip(3).Take(4).ToDisplayString(), digits.Skip(7).Take(5).ToDisplayString()); | |
} | |
return string.Format("国コード:{0}\nメーカコード:{1}\n商品コード:{2}", digits.Take(2).ToDisplayString(), digits.Skip(2).Take(5).ToDisplayString(), digits.Skip(7).Take(5).ToDisplayString()); | |
} | |
else | |
{ | |
throw new ArgumentException("JAN コードの桁数が正しくありません。"); | |
} | |
} | |
static string ToDisplayString(this IEnumerable<int> collection) | |
{ | |
return collection.Aggregate("", (x, y) => x + y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment