/* encode_freq.c - normalize a frequency w/ or w/o decimal for R8 */ /* * Copyright (C) 1995 Jeffrey Chilton * * Permission is granted to anyone to make or distribute copies of * this program, in any medium, provided that the copyright notice * and permission notice are preserved, and that the distributor * grants the recipient permission for further redistribution as * permitted by this notice. * * Author's E-mail address: jwc@chilton.com * */ /* * New maximum-tolerance rule: multiply or divide by 1000 as * required to bring value into 110KHz to 30MHz range (if it * can be--30.001 through 99.999 will still cause an error). * * Input R8 command * ----- ---------- * 110 F0011000 (MHz) * 110.0 F0011000 (MHz) * 1120 F0112000 (KHz) * 1120.5 F0112500 (KHz) * 10.0 F1000000 (MHz) * 12 F1200000 (MHz) * 0.12 F0012000 (MHz) * 121.1 F0012110 (KHz and probably not what they had in mind) * * Set exit status to non-zero for frequency out of range for R8. * */ #include main() { register int i, j; char line[80]; char fixup[80]; int decimal; double f; long out; fgets(line, 80, stdin); while (!feof(stdin)) { /* * Clean up the input. We allow comma as the decimal * point for our euro customers and multiple points for * old radio hands who like to write ``12.067.5'' */ j = 0; decimal = 0; for (i = 0; line[i] != '\n' && i < 80; i++) { if (line[i] == '.' || line[i] == ',') { if (!decimal) { fixup[j++] = '.'; decimal = 1; } continue; } else if (line[i] == ' ') { continue; } else if (line[i] < '0' || line[i] > '9') { exit(1); } fixup[j++] = line[i]; } fixup[j] = '\0'; sscanf(fixup, "%lf", &f); /* If it's too small to be KHz, treat it as MHz */ if (f < 100.0) { f *= 1000.0; } out = f * 100.0 + 0.5; if (out < 11000 || out > 3000000) { printf("\n"); exit(1); } printf("F%07ld\n", out); fgets(line, 80, stdin); } exit(0); }