Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- double distance;
- cin >> distance;
- cin.ignore();
- string inputMetric, outputMetric;
- cin >> inputMetric >> outputMetric;
- if (inputMetric == "m") {
- distance /= 1000;
- }
- else if (inputMetric == "cm") {
- distance /= 100000;
- }
- else if (inputMetric == "mm") {
- distance /= 1000000;
- }
- else if (inputMetric == "mi") {
- distance /= 0.621371192;
- }
- else if (inputMetric == "in") {
- distance /= 39370.0787;
- }
- else if (inputMetric == "ft") {
- distance /= 3280.8399;
- }
- else if (inputMetric == "yd") {
- distance /= 1093.6133;
- }
- if (outputMetric == "m") {
- distance *= 1000;
- }
- else if (outputMetric == "cm") {
- distance *= 100000;
- }
- else if (outputMetric == "mm") {
- distance *= 1000000;
- }
- else if (outputMetric == "mi") {
- distance *= 0.621371192;
- }
- else if (outputMetric == "in") {
- distance *= 39370.0787;
- }
- else if (outputMetric == "ft") {
- distance *= 3280.8399;
- }
- else if (outputMetric == "yd") {
- distance *= 1093.6133;
- }
- cout << distance << endl;
- return 0;
- }
- Решение с тернарен оператор:
- #include <iostream>
- using namespace std;
- int main() {
- double distance;
- cin >> distance;
- cin.ignore();
- string inputMetric, outputMetric;
- cin >> inputMetric >> outputMetric;
- distance /=
- inputMetric == "m" ? 1000 :
- inputMetric == "cm" ? 100000 :
- inputMetric == "mm" ? 1000000 :
- inputMetric == "mi" ? 0.621371192 :
- inputMetric == "in" ? 39370.0787 :
- inputMetric == "ft" ? 3280.8399 :
- inputMetric == "yd" ? 1093.6133 : 1;
- distance *=
- outputMetric == "m" ? 1000 :
- outputMetric == "cm" ? 100000 :
- outputMetric == "mm" ? 1000000 :
- outputMetric == "mi" ? 0.621371192 :
- outputMetric == "in" ? 39370.0787 :
- outputMetric == "ft" ? 3280.8399 :
- outputMetric == "yd" ? 1093.6133 : 1;
- cout << distance << endl;
- return 0;
- }
- Решение с колекция:
- #include <iostream>
- #include <map>
- using namespace std;
- int main() {
- double distance;
- cin >> distance;
- cin.ignore();
- string inputMetric, outputMetric;
- cin >> inputMetric >> outputMetric;
- map <string, double> metricsToKM = {
- {"km", 1},
- {"m", 1000},
- {"cm", 100000},
- {"mm", 1000000},
- {"mi", 0.621371192},
- {"in", 39370.0787},
- {"ft", 3280.8399},
- {"yd", 1093.6133}
- };
- cout << distance / metricsToKM[inputMetric] * metricsToKM[outputMetric] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement