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, ¢s); 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; }