Fotolog

A través del espejo
2010-10-12: A través del espejo
¡La radio habla en binario!
2010-10-10: ¡La radio habla en binario!
Gigaminx (regalo por mi cumple)
2010-09-16: Gigaminx (regalo por mi cumple)
Trini en bici
2010-09-05: Trini en bici
Valporquero
2010-08-28: Valporquero
Mi bici nueva
2010-08-22: Mi bici nueva
Boda de Mario y Ana
2010-08-13: Boda de Mario y Ana
De cañones en Guara
2010-08-07: De cañones en Guara
Trini y Mari en Marbella
2010-08-05: Trini y Mari en Marbella
Trini y Chelo en Tabarca
2010-08-03: Trini y Chelo en Tabarca
Valid XHTML 1.1
Acceder
Volver a la lista de problemas

Telephone Tangles

139.c

/* 139 - Telephone Tangles */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct t {
	char code[20];
	char place[30];
	int cents;
};

int nt = 0;
struct t * t = NULL;

struct r {
	char * place;
	char * num;
	int cents;
};

struct r *
calc(char * code) {
	static struct r r;
	int i;

	if (code[0]>='1' && code[0]<='9') {
		r.place = "Local";
		r.num = code;
		r.cents = 0;
		return &r;
	}
	for (i=0; i<nt; i++) {
		if (!strncmp(code, t[i].code, strlen(t[i].code))) {
			if (t[i].code[0] != '0') {
				continue;
			}
			if (t[i].code[1] == '0' &&
					(strlen(code) - strlen(t[i].code) < 4 ||
					strlen(code) - strlen(t[i].code) > 10)) {
				continue;
			}
			if (t[i].code[1] != '0' &&
					(strlen(code) - strlen(t[i].code) < 4 ||
					strlen(code) - strlen(t[i].code) > 7)) {
				continue;
			}
			r.place = t[i].place;
			r.cents = t[i].cents;
			r.num = code+strlen(t[i].code);
			return &r;
		}
	}
	return NULL;
}

int
main(void) {
	char buf[1024];
	char code[1024];
	char place[1024];
	int cents;
	int minutes;

	while (fgets(buf, 1024, stdin)) {
		sscanf(buf, "%s %[^$]$%d", code, place, &cents);
		if (!strcmp(code, "000000")) {
			break;
		}
		if (code[0] != '0') {
			continue;
		}
		if (code[1]=='0' && (strlen(code)<3 || strlen(code)>5)) {
			continue;
		} else if (strlen(code)<2 || strlen(code)>6) {
			continue;
		}
		t = realloc(t, (nt+1)*sizeof(struct t));
		strcpy(t[nt].code, code);
		strcpy(t[nt].place, place);
		t[nt].cents = cents;
		nt++;
	}
	while (scanf(" %s %d", code, &minutes)==2 && code[0]!='#') {
		struct r * r = calc(code);
		if (r) {
			printf("%-15s %-25s%10s%5d%3d.%02d%4d.%02d\n",
					code, r->place, r->num, minutes, (r->cents)/100, (r->cents)%100,
					(minutes*r->cents)/100, (minutes*r->cents)%100);
		} else {
			printf("%-15s %-25s            %3d        -1.00\n",
					code, "Unknown", minutes);
		}

	}

	return 0;
}