#include <iostream>
#include "Claculator.h"
#include <string>
#include <vector>
#include <sstream>
#pragma warning(disable:4996)
using namespace std;
vector<string> split(string str, char Delimiter) {
istringstream iss(str);
string buffer;
vector<string> result;
while (getline(iss, buffer, Delimiter)) {
result.push_back(buffer);
}
return result;
}
int main()
{
Calculator cal;
string str;
char acper;
vector<string> result;
printf("Calculator Console Application\n");
printf("Please enter the operation to perform. Format: a+b | a-b | a*b | a/b\n");
printf("Press \"ctrl + c\" to exit the program\n");
while (true)
{
cout<<endl<< "input >>";
getline(cin, str);
for (int i = 0;i < str.length();i++) {
if (str[i] == ' ') str.erase(i, 1);
}
for (int i = 0;i < str.length();i++) {
if (str[i] < 48 || str[i]>57) {
acper = str[i];
break;
}
}
result = split(str, acper);
try {
if (acper == '/' && stod(result[1]) == 0)throw stod(result[1]);
}
catch (double ex) {
cout << ex << "로 나눌 수 없습니다." << endl;
continue;
}
cout << "= " << cal.Calculate(stod(result[0]), acper, stod(result[1])) << endl;
}
return 0;
}
#pragma once
#ifndef __CALCULATOR_H_
#define __CALCULATOR_H_
#include <vector>
#include <string>
class Calculator
{
private:
double x;
char cper;
double y;
public:
double Calculate(double ax = 0, char acper = NULL, double ay = 0);
};
#endif
#include <iostream>
#include <sstream>
#include "Claculator.h"
using namespace std;
double Calculator::Calculate(double ax, char acper, double ay)
{
x = ax;
y = ay;
cper = acper;
if (cper == '+')
return x + y;
else if (cper == '-')
return x - y;
else if (cper == '*')
return x * y;
else if (cper == '/')
return x / y;
else
return 0;
}