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
|
#include <iostream>
#include <cmath>
#include <string>
#include <limits>
#include <utility>
#include <cctype>
using namespace std;
using Point = std::pair<double, double>;
enum Menu : char { letter = 'l', number = 'n', point = 'p', quit = 'q' };
template<typename T = int>
auto getNum(const std::string& prm)
{
const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
T n {};
while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
std::cout << "Not a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return n;
}
std::string remzero(const std::string& str) {
auto s(str);
if (str.find('.')) {
while (*s.rbegin() == '0')
s.resize(s.size() - 1);
if (*s.rbegin() == '.')
s.resize(s.size() - 1);
}
return s;
}
char asUpper(char ch) {
return static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
}
Menu domenu() {
char choice {};
cout << "Options: " << letter << ")etter; " << number << ")umber; " << point << ")oint; " << quit << ")uit: ";
cin >> choice;
return static_cast<Menu>(choice);
}
void errorMessage() {
cout << "This is not an option, try again!\n";
}
double distance(char c1, char c2) {
return abs(asUpper(c2) - asUpper(c1));
}
double distance(double c1, double c2) {
return abs(c2 - c1);
}
double distance(const Point& p1, const Point& p2) {
return {sqrt((p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second))};
}
void display(const string& msg, char c1, char c2) {
static unsigned cnt {};
cout << "(#" << ++cnt << ") " << msg << ' ' << c1
<< " & " << c2 << " is: " << distance(c1, c2) << '\n';
}
void display(const string& msg, double d1, double d2) {
static unsigned cnt {};
cout << "(#" << ++cnt << ") " << msg << ' ' << d1
<< " & " << d2 << " is: " << distance(d1, d2) << '\n';
}
void display(const string& msg, const Point& p1, const Point& p2) {
static unsigned cnt {};
cout << "(#" << ++cnt << ") " << msg << " ("
<< p1.first << "," << p1.second << ")" << " and ("
<< p2.first << "," << p2.second << ") is: "
<< distance(p1, p2) << " units.\n";
}
char input(char, const std::string& inptxt, const std::string& errtxt) {
char ch {};
while ((std::cout << inptxt << " (a - z) : ") && (std::cin >> ch) && !std::isalpha(static_cast<unsigned char>(ch)))
std::cout << errtxt << '\n';
return ch;
}
double input(double, double minn, double maxn, const std::string& inptxt, const std::string& errtxt) {
double no {};
do {
no = getNum<double>(inptxt + " (" + remzero(std::to_string(minn)) + ", " + remzero(std::to_string(maxn)) + ") : ");
} while ((no < minn || no > maxn) && (std::cout << errtxt << '\n'));
return no;
}
Point input(Point, const std::string& inptxt) {
const auto x1 {getNum<double>(inptxt + " (x) : ")};
const auto y1 {getNum<double>(inptxt + " (y) : ")};
return {x1, y1};
}
int main() {
for (Menu userChoice {}; userChoice != quit; ) {
switch (userChoice = domenu()) {
case letter:
{
static const auto letErr {"Not a letter"s};
const auto c1 {input(char {}, "Enter first letter", letErr)};
const auto c2 {input(char {}, "Enter second letter", letErr)};
display("The distance between letters", c1, c2);
}
break;
case number:
{
static const auto numErr {"Out of range"s};
const auto x {input(double {}, -100, 100, "Enter first number", numErr)};
const auto y {input(double {}, -200, 200, "Enter next number", numErr)};
display("The units between", x, y);
}
break;
case point:
{
const auto x {input(Point {}, "Enter the first point")};
const auto y {input(Point {}, "Enter the second point")};
display("The straight line distance between points", x, y);
}
break;
case quit:
cout << "Goodbye!\n";
break;
default:
errorMessage();
break;
}
}
}
|