-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
57 lines (45 loc) · 1.03 KB
/
Copy pathinput.cpp
File metadata and controls
57 lines (45 loc) · 1.03 KB
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
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// make it accept more and more parameters
float perform(string first, char sign, string second) {
switch (sign) {
case '+':
return stof(first) + stof(second);
break;
case '-':
return stof(first) - stof(second);
break;
case 'x':
return stof(first) * stof(second);
break;
case '/':
return stof(first) / stof(second);
break;
default:
return 0;
}
return 0;
}
// Optimize this function
vector<string> split(string s, string del = " ") {
int start, end = -1 * del.size();
vector<string> items;
do {
start = end + del.size();
end = s.find(del, start);
items.push_back(s.substr(start, end - start));
} while (end != -1);
return items;
}
int main() {
string text;
printf("please enter your operation: ");
getline(cin, text);
vector<string> inputs = split(text);
printf("%f\n", perform(inputs.at(0), inputs.at(1).at(0), inputs.at(2)));
return 0;
}