Skip to content

Instantly share code, notes, and snippets.

@grayed
Created February 21, 2024 12:46
Show Gist options
  • Save grayed/7485dce04efb72a044b6e1e89cb05979 to your computer and use it in GitHub Desktop.
Save grayed/7485dce04efb72a044b6e1e89cb05979 to your computer and use it in GitHub Desktop.
КМБО-07-23 калькулятор со string
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
float calc(string op, float x, float y);
int main() {
float x, y, res;
char op, c = '1', arr[] = "123456789";
string ops;
cout << "Enter x: ";
cin >> x;
cout << "Enter operation: ";
cin >> ops;
cout << "Enter y: ";
cin >> y;
res = calc(ops, x, y);
cout << "x " << ops << " y = " << res << endl;
return 0;
}
float calc(string op, float x, float y) {
float res;
if (op == "+")
res = x + y;
else if (op == "-")
res = x - y;
else if (op == "*")
res = x * y;
else if (op == "/")
res = x / y;
else if (op == "ln")
res = log(x) / log(y);
else {
cerr << "Unsupported operation: '" << op << "'\n";
exit(1);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment