string
?std::regex arithmetic_regex("[\(\)\*\+\/-]");
std::vector<char> op;
std::vector<char> op2;
for (std::sregex_token_iterator it(begin(example), end(example), arithmetic_regex), last;
it != last; ++it) {
op.push_back(it->str()[0]);
op2.push_back(it->str()[0]);
if (op == op2)
std::cout << op.at(0);
}
#include <iostream>
#include <string>
#include <cctype>
std::string checkRepeat(std::string& str) {
if(!std::isdigit(str[0]) || !std::isdigit(str[str.length()-1])) return "Error!";
std::string symbols{"*/+-"}, res{"Ok"};
for(int i{0}; i < str.length(); ++i) {
bool flag{false}, flag2{false};
for(char ch : symbols) {
if(str[i] == ch) flag = true;
}
if(flag && i < str.length()-1) {
for(char ch : symbols) {
if(str[i+1] == ch) flag2 = true;
}
}
if(flag && flag2) res = "Error!";
}
return res;
}
int main()
{
std::string s{"43+2/4**2-5"};
std::cout << checkRepeat(s);
return 0;
}
Вот придумал покороче:
#include <iostream>
#include <string>
std::string checkRepeat(std::string& str) {
std::string symbols{"+-*/"};
for(int i{0}; i < str.length(); ++i) {
int ind = symbols.find(str[i]);
int ind2 = -1;
if(ind > -1 && i < str.length()-1) ind2 = symbols.find(str[i+1]);
if(ind2 > -1) return "Error!";
}
return "Ok";
}
int main() {
std::string s{"2+4/4**2-1"};
std::cout << checkRepeat(s);
return 0;
}