[ create a new paste ] login | about

Link: http://codepad.org/ua71XDW9    [ raw code | fork ]

C, pasted on May 9:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
	char buf[256];
	int k = 3;
	int total = 0;
	int len = 0;

	// データを入力
	printf("data: ");
	fgets(buf, sizeof(buf), stdin);

	// 文字数を調べる
	len = strlen(buf);

	// 文字列の右端から順に計算
	for (int i = len - 1; i >= 0; i--) {
		// 数字じゃなければ読み飛ばす
		if (!isdigit(buf[i])) {
			continue;
		}

		// (右端を奇数桁として)奇数桁なら 3 倍、偶数桁なら 1 倍
		total += ((buf[i] - '0') % 10) * k;

		// 3, 1 を交互に切り替える
		k ^= 2;
	}

	// 10 - (合計 mod 10) が check-digit
	printf("check-digit: %d\n", (10 - total % 10));
}


Create a new paste based on this one


Comments: