Language: C++

#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;

bool digit(char c){
  if( c - '0'< 0 || c-'0' > 9)return false;
  return true;
}

int stoi(string s){
  int res,tmp;

  res = 0;
  tmp = 1;
  for(int i=s.size()-1;i>=0;i--){
    res += (s[i]-'0') * tmp;
    tmp *= 10;
  }
  return res;
}  

set<int> cal(set<int> a,set<int> b,char o){
  set<int> res;
  set<int>::iterator i,j;
  res.clear();
  if(a.empty() || b.empty)return res;

  if(o=='+'){
    for(i=a.begin();i!=a.end();i++){
      for(j=b.begin();j!=b.end();j++){
	res.insert((*i) + (*j));
      }
    }
  }else if(o=='-'){
    for(i=a.begin();i!=a.end();i++){
      for(j=b.begin();j!=b.end();j++){
	res.insert((*i) - (*j));
      }
    }
  }else if(o=='*'){
    for(i=a.begin();i!=a.end();i++){
      for(j=b.begin();j!=b.end();j++){
	res.insert((*i) * (*j));
      }
    }
  }else if(o=='/'){
    for(i=a.begin();i!=a.end();i++){
      for(j=b.begin();j!=b.end();j++){
	if(!(*j))res.insert((*i) / (*j));
      }
    }
  }
  return res;
}

set<int> parse(string s){
  string tmp;
  set<int> ans[20],store[20],res;
  char op[20];
  int pos,cnt;
  vector<int> order;

  res.clear();
  pos = 0;
  cnt = 0;

  while(pos < (int)s.size()){
    if(s[pos] == '('){
      tmp.clear();
      pos++;
      while(s[pos] != ')'){
	tmp.push_back(s[pos]);
	pos++;
      }
      ans[cnt].clear();
      ans[cnt] = parse(tmp);
      pos++;
    }else if(digit(s[pos])){
      tmp.clear();
      while(digit(s[pos])){
	tmp.push_back(s[pos]);
	pos++;
      }
      ans[cnt].clear();
      ans[cnt].insert(stoi(tmp));
    }
    if(pos==(int)s.size())break;

    op[cnt] = s[pos];
    cnt++;
    pos++;
  }

  order.clear();
  for(int i=0;i<cnt;i++)order.push_back(i);
  res.clear();

  do{
    for(int i=0;i<=cnt;i++)store[i] = ans[i];
    int use[20],num;
    set<int> hoge;
    for(int i=0;i<=cnt;i++)use[i] = i;
    hoge.clear();
    
    for(int i=0;i<cnt;i++){
      hoge = cal(store[order[i]],store[order[i]+1],op[order[i]]);
      
      num = use[order[i]+1];
      for(int j=0;j<=cnt;j++){
	if(use[j] == num)use[j] = use[order[i]];
      }
      for(int j=0;j<=cnt;j++){
	if(use[j] == use[order[i]])store[j] = hoge;
      }
    }
    
    set<int>::iterator i;
    for(i=store[0].begin();i!=store[0].end();i++)res.insert(*i);

  }while(next_permutation(order.begin(),order.end()));
  

  return res;
}


int main(){
  string str;
  set<int> s;

  while(1){
    cin >> str;
    if(str == "#")break;

    s = parse(str);

    cout << s.size() << endl;    
  }
}

Compiler output:
a.cc: In function 'std::set<int> cal(std::set<int>, std::set<int>, char)':
a.cc:28:21: error: invalid use of member function 'bool std::set<_Key, _Compare, _Alloc>::empty() const [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]' (did you forget the '()' ?)
   28 |   if(a.empty() || b.empty)return res;
      |                   ~~^~~~~
      |                          ()
a.cc: In function 'std::set<int> parse(std::string)':
a.cc:87:27: error: call of overloaded 'stoi(std::string&)' is ambiguous
   87 |       ans[cnt].insert(stoi(tmp));
      |                       ~~~~^~~~~
a.cc:12:5: note: candidate: 'int stoi(std::string)'
   12 | int stoi(string s){
      |     ^~~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/basic_string.h:4164:3: note: candidate: 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~