Language: C++

#include <iostream>
#include <algorithm>
#include <complex>
#include <cassert>

using namespace std;
typedef complex<long long> P;

long long abs(long long x){ return x < 0 ? -x : x; }
int pos;
string ln;
bool f = 0;

P term();
P factor();
P formula(){
	P ans = term();
	while(ln[pos] == '+' || ln[pos] == '-'){
		if(ln[pos] == '+'){
			pos++;
			ans += term();
		}else{
			pos++;
			ans -= term();
		}
		if( abs(ans.real()) > 10000 || abs(ans.imag()) > 10000 ) f = 1;
	}
	//cout << ln[pos] << endl;
	if(ln[pos] == '(') assert(0);
	return ans;
}

P term(){
	P r = factor();
	while(ln[pos] == '*'){
		if(ln[pos]=='*'){	
			pos++;
			r *= factor();
			if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
		}
	}
	return r;
}
P factor(){
	bool minus = 0;
	while(ln[pos] == '-') minus ^= 1 , pos++; 
	if(ln[pos] == '('){
		pos++;
		P r = formula();
		pos++;
		if(ln[pos] == 'i') r *= P(0,1) , pos++;
		if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
		return r * (minus?P(-1,0):P(1,0));
	}else{
		P r(0,0);
		while(ln[pos] == '-') minus ^= 1 , pos++; 
		if(ln[pos] != 'i'){
			while( (ln[pos] <= '9' && ln[pos] >= '0') || ln[pos] == 'i'){
				if(ln[pos] == 'i') r *= P(0,1); 
				else r = P(r.real() * 10 + ln[pos]-'0',0);
				if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
				//cout << r << " " << ln[pos] << endl;
				pos++;
			}
			return r * (minus?P(-1,0):P(1,0));
		}else{
			pos++;
			return P(0,1) * (minus?P(-1,0):P(1,0)); 
		}
	}
}

int main(){
	while(cin >> ln){
		ln += "~";
		pos = 0;
		f = 0;
		P r = formula();
		if(!f){
			if(r.real() != 0){
				cout << r.real();
				if(r.imag()<0){
					if(r.imag() == -1) cout << "-i";
					else cout << r.imag() << "i";
				}else if(r.imag()>0){
					if(r.imag() == 1) cout << "+i";
					else cout << "+" << r.imag() << "i";
				}
				cout << endl;
			}else{
				if(r.imag() == 0){
					cout << 0 << endl;
				}else{
					if(r.imag() ==-1) cout << "-i" << endl;
					else if(r.imag() == 1) cout << "i" << endl;
					else cout << r.imag() << "i" << endl;
				}
			}
		}else{
			cout << "overflow" << endl;
		}
	}
}

Compiler output:
a.cc: In function 'P formula()':
a.cc:26:24: error: call of overloaded 'abs(long long int)' is ambiguous
   26 |                 if( abs(ans.real()) > 10000 || abs(ans.imag()) > 10000 ) f = 1;
      |                     ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 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/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:26:51: error: call of overloaded 'abs(long long int)' is ambiguous
   26 |                 if( abs(ans.real()) > 10000 || abs(ans.imag()) > 10000 ) f = 1;
      |                                                ~~~^~~~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc: In function 'P term()':
a.cc:39:32: error: call of overloaded 'abs(long long int)' is ambiguous
   39 |                         if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
      |                             ~~~^~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:39:57: error: call of overloaded 'abs(long long int)' is ambiguous
   39 |                         if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
      |                                                      ~~~^~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc: In function 'P factor()':
a.cc:52:24: error: call of overloaded 'abs(long long int)' is ambiguous
   52 |                 if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
      |                     ~~~^~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:52:49: error: call of overloaded 'abs(long long int)' is ambiguous
   52 |                 if( abs(r.real()) > 10000 || abs(r.imag()) > 10000 ) f = 1;
      |                                              ~~~^~~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:9:11: note: candidate: 'long long int abs(long long int)'
    9 | long long abs(long long x){ return x < 0 ? -x : x; }
      |           ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: n