1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/* Calculate resistor values in ohms. Approximate string matching is supported. */
/* Copyright (C) 2015, Max Bozzi */
/* This file is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* This file. If not, see <http://www.gnu.org/licenses/>.
*/
# include <iostream>
# include <memory>
# include <vector>
# include <cmath>
// wagner-fischer/levenshtein/ukkonen's alrorithm: what's the difference?
# include <string>
# include <algorithm>
# include <numeric>
int levenshtein(std::string a, std::string b) {
/* TODO: Maybe slightly buggy(?) */
if (a == b) return 0;
if (a.length () == 0) return b.length ();
if (b.length () == 0) return a.length ();
std::vector <std::size_t> r0 (b.length () + 1, 0);
std::iota(r0.begin(), r0.end(), 0);
std::vector <std::size_t> r1 (b.length () + 1, 0);
for (std::size_t i = 0; i < a.length (); i ++) {
r1 [0] = i + 1;
for (std::size_t j = 0; j < b.length (); j ++) {
bool const diff = a [j] != b [j];
r1 [j + 1] = std::min
(std::min (r1 [j] + 1, r0 [j + 1] + 1), r0 [j] + (diff? 2: 0));
/* if (r0 [j + 1] >= max_edit_distance) { return 0; } */
}
std::swap(r0, r1);
}
return r1 [b.length()];
}
# include <map>
std::map <std::string, int> const band_value {
{"black" , 0}, {"brown", 1}, {"red" , 2}, {"orange", 3},
{"yellow", 4}, {"green", 5}, {"blue", 6}, {"violet", 7},
{"grey" , 8}, {"white", 9},
};
std::map <std::string, double> const multiplier_value {
{"black" , 1e0}, {"brown", 1e1}, {"red" , 1e2}, {"orange", 1e3},
{"yellow", 1e4}, {"green", 1e5}, {"blue", 1e6}, {"violet", 1e7},
{"grey" , 1e8}, {"white", 1e9}, {"gold", 1e-1}, {"silver", 1e-2}
};
/* Temperature coefficient in parts-per-million per degree celsius. */
std::map <std::string, int> const temp_coeff_value {
{"brown", 100}, {"red" , 50}, {"orange", 15}, {"yellow", 25}
};
/* Resistance tolerance in +/- %. */
std::map <std::string, double> const tolerance_value {
{"brown", 1.0}, {"red" , 2.0}, {"green", 0.5}, {"blue", 0.25},
{"violet", 0.1}, {"grey", 0.05}, {"gold", 5.0}, {"silver", 10.0},
{"none", 20.0}
};
std::vector <std::string> const colors {
"black" , "brown", "red" , "orange", "yellow", "green", "blue",
"violet", "grey" , "white", "silver", "gold"
};
std::vector <std::string> match_prefixes(std::string match,
std::vector <std::string> candidates) {
/* The best way to do this is via a trie. I'm not writng one now. */
std::vector <std::string> prev_candidates;
for (std::size_t i = 0; i < match.length (); i ++) {
prev_candidates = candidates;
for (auto iter = candidates.begin (); iter != candidates.end (); ) {
if (i >= iter->length () || match [i] != (*iter)[i])
iter = candidates.erase (iter);
else
iter ++;
}
// return candidates;
if (!candidates.size ()) return (prev_candidates);
}
return candidates;
}
/* Does some sort of ad-hoc approximate matching. Code was copied in from
various programs, all GPL'd, all my property. Don't even try to use this for
large dictionaries. */
std::string amatch (std::string match,
std::vector<std::string> candidates) {
auto results = match_prefixes(match, candidates);
if (results.size() == 1) {
return results.back();
}
/* Check for matching ending characters: */
auto backmatch = results;
for (auto iter = backmatch.begin(); iter != backmatch.end(); ) {
if (match.back() != iter->back())
iter = backmatch.erase(iter);
else
iter ++;
}
if (backmatch.size() == 1) {
return backmatch.back();
}
/* Sort by levenshtein distance... */
std::sort (results.begin (), results.end (), [match](std::string a,
std::string b) {
return levenshtein(match, a) < levenshtein(match, b);
});
return results.front();
}
void __attribute__((noreturn)) usage_and_die (char ** argv) {
std::cerr << "usage: " << *argv << "\n"
<< " (band1 band2 band3 [band4] [band5] [tolerance_band])|\n"
<< "output: resistor value in ohms, tolerance.\n";
std::exit(1);
}
void print_resistor (std::vector <std::string> bands) {
auto compute_ohms = [](auto start_iter, auto end_iter) -> double {
double ohm_result = 0.0;
while (start_iter != end_iter) {
/* Make sure we're not looking at the multiplier band */
if (start_iter != (end_iter - 1)) {
ohm_result *= 10; /* decimal ALSH (e.g., 14 -> 140) */
ohm_result += band_value.at(*start_iter++);
} else { /* Looking at last band. */
return ohm_result * multiplier_value.at(*start_iter);
}
}
return ohm_result;
};
double ohm_result, tolerance, temp_coeff;
ohm_result = tolerance = temp_coeff =
std::numeric_limits<double>::infinity();
try {
switch (bands.size()) {
case 3: {
ohm_result = compute_ohms(bands.begin(), bands.end());
tolerance = tolerance_value.at("none");
} break;
case 4:
case 5: {
ohm_result = compute_ohms(bands.begin(), bands.end() - 1);
tolerance = tolerance_value.at(bands.back());
} break;
case 6: {
ohm_result = compute_ohms(bands.begin(), bands.end() - 2);
tolerance = tolerance_value.at(*(bands.end () - 2));
temp_coeff = temp_coeff_value.at(bands.back());
} break;
default:
std::cerr << "Incorrect number of bands. \n";
std::exit(1);
}
} catch (std::out_of_range const e) {
std::cerr << "Unrecognized band type. Check your resistor!\n";
std::exit(1);
}
using namespace std::literals::string_literals;
std::cout << ohm_result << " ohms at " << tolerance << " percent tolerance";
if (! std::isinf(temp_coeff)) {
std::cout <<", temperature coeff. " << temp_coeff << "\n";
} else std::cout << ".\n";
}
auto main (int argc, char **argv) -> int {
if (argc == 1 || argc > 7) { /* no more than 6 bands! */
usage_and_die (argv);
}
std::vector<std::string> curr_bands;
for (int i = 1; i < argc; i ++) {
curr_bands.emplace_back(amatch(argv [i], colors));
}
print_resistor(curr_bands);
for (auto c: curr_bands) std::cout << c << " ";
std::cout << "\n";
}
|