Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

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;
}