Cod sursa(job #1559591)

Utilizator daniel.sanduSandu Daniel daniel.sandu Data 31 decembrie 2015 11:01:13
Problema Cifra Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <cstring>
#include <cstdio>

const int partialSum[] = {
	0, 1, 5, 2, 8, 3, 9, 2, 8, 7, 7, 8, 4, 7, 3, 8, 4, 1, 5, 4
};

bool isNumber(char x) {
	return '0' <= x && x <= '9';
}

inline int cifra(int n) {
	return (partialSum[n % 20] + 4 * (((((n - (n % 20)) / 10) % 5) * 3) % 5)) % 10;
}

int cifra(char const * buffer) {
	int size = strlen(buffer),
			n = buffer[0] - '0',
	    multiplier,
			result = 0;
	
  if (buffer[1]) {
		n += 10 * (buffer[1] - '0');
		multiplier = ((((n - (n % 20)) / 10) % 5) * 3) % 5;
		result += 4 * multiplier;
	}
	result += partialSum[n % 20];
	return result % 10;
}

char* prettyPrint(char* c) {
	if (isNumber(c[1]))
		std::swap(c[0], c[1]);
	return c;
}

int main() {
	char const * const inputFile = "cifra.in",
						 * const outputFile = "cifra.out";
	std::ifstream in(inputFile);
	std::ofstream out(outputFile);
	
	char x, c[3] = {'\0', '\0', '\0'};
	in >> x;
	while (in) {
		x = in.get();
		if (isNumber(x)) {
			c[1] = c[0];
			c[0] = x;
		} else if (c[0]) {
			out << cifra(c) << std::endl;
			c[0] = c[1] = '\0';
		}
	}
	return 0;
}