-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.cpp
More file actions
71 lines (54 loc) · 1.61 KB
/
Q3.cpp
File metadata and controls
71 lines (54 loc) · 1.61 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <algorithm>
//add other libraries if needed
using namespace std;
vector<string> split(const string &expression) {
//returns a vector of strings that represent operands or numbers
vector<string> result;
string expression1=expression;
vector<int> amount;
string current,signs;
expression1.erase(std::remove(expression1.begin(),expression1.end(),' '),expression1.end());
for (int i=0; i<expression1.size();i++){
if ((expression1[i]=='*') || (expression1[i]=='+')){
amount.push_back(i);
}
}
if (amount.size()>0){
current=expression1.substr(0,amount[0]);
result.push_back(current);
}
else{
result.push_back(expression1);
}
for (int i=0;i<amount.size()-1;i++){
signs=expression1[amount[i]];
result.push_back(signs);
int amountz=amount[i+1]-amount[i]-1;
current=expression1.substr(amount[i]+1,amountz);
result.push_back(current);
}
signs=expression1[amount[amount.size()-1]];
result.push_back(signs);
int amountz=expression1.size()-amount[amount.size()-1]-1;
current=expression1.substr(amount[amount.size()-1]+1,amountz);
result.push_back(current);
return result;
//implement your function here
}
int main () {
string test;
cout<<"Enter an Expression: "<<endl;
getline(cin,test);
vector<string> result=split(test);
for (int i=0;i<result.size();i++){
cout<<result[i]<<endl;
}
//test code:
//ask the user to enter an expression
//call the split function
//display the split items (numbers and operands) on the console
//add more test cases if needed
return 0;
}