Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef string::const_iterator State;
typedef pair<int, int> Pii;

#define fr first
#define sc second

bool is_over = false;

struct Parser {
  Pii expression(State &idx) {
    Pii ret = term(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    while ( 1 ) {
      if ( *idx == '+' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr + tmp.fr;
	ret.sc = ret.sc + tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else if ( *idx == '-' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr - tmp.fr;
	ret.sc = ret.sc - tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }
  
  Pii term(State &idx) {
    Pii ret = factor(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    //
    while ( 1 ) {
      if ( *idx == '*' ) {
	idx++;
	Pii tmp = factor(idx), tmp2 = ret;
	//cout << ret.fr << " " << ret.sc << endl;
	//cout << tmp.fr << " " << tmp.sc << endl;
	ret.fr = tmp2.fr * tmp.fr - tmp2.sc * tmp.sc;
	ret.sc = tmp2.fr * tmp.sc + tmp2.sc * tmp.fr;
	if ( abs(ret.fr)> 10000 || abs(ret.sc) > 10000 ) is_over = true;
	//	cout << ret.fr << " " << ret.sc << endl;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii factor(State &idx) {
    Pii ret;
    if ( *idx == '(' ) {
      idx++;
      ret = expression(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      idx++;
    } else {
      ret = number(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii number(State &idx) {
    int tmp = 0;
    while ( '0' <= *idx && *idx <= '9' ) {
      tmp *= 10;
      tmp += *idx - '0';
      if ( abs(tmp) > 10000 ) is_over = true;
      idx++;
      /*if ( *idx == 'i' ) {
	is_real = false;
	idx++;
	}*/
    }
    if ( abs(tmp) > 10000 ) is_over = true;
    //cout << tmp << endl;
    if ( *idx == 'i' ) {
      idx++;
      return Pii(0, 1);
    }
 
    else return Pii(tmp, 0);
  }
};

int main()#include <bits/stdc++.h>

using namespace std;

typedef string::const_iterator State;
typedef pair<int, int> Pii;

#define fr first
#define sc second

bool is_over = false;

struct Parser {
  Pii expression(State &idx) {
    Pii ret = term(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    while ( 1 ) {
      if ( *idx == '+' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr + tmp.fr;
	ret.sc = ret.sc + tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else if ( *idx == '-' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr - tmp.fr;
	ret.sc = ret.sc - tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }
  
  Pii term(State &idx) {
    Pii ret = factor(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    //
    while ( 1 ) {
      if ( *idx == '*' ) {
	idx++;
	Pii tmp = factor(idx), tmp2 = ret;
	//cout << ret.fr << " " << ret.sc << endl;
	//cout << tmp.fr << " " << tmp.sc << endl;
	ret.fr = tmp2.fr * tmp.fr - tmp2.sc * tmp.sc;
	ret.sc = tmp2.fr * tmp.sc + tmp2.sc * tmp.fr;
	if ( abs(ret.fr)> 10000 || abs(ret.sc) > 10000 ) is_over = true;
	//	cout << ret.fr << " " << ret.sc << endl;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii factor(State &idx) {
    Pii ret;
    if ( *idx == '(' ) {
      idx++;
      ret = expression(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      idx++;
    } else {
      ret = number(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii number(State &idx) {
    int tmp = 0;
    while ( '0' <= *idx && *idx <= '9' ) {
      tmp *= 10;
      tmp += *idx - '0';
      if ( abs(tmp) > 10000 ) is_over = true;
      idx++;
      /*if ( *idx == 'i' ) {
	is_real = false;
	idx++;
	}*/
    }
    if ( abs(tmp) > 10000 ) is_over = true;
    //cout << tmp << endl;
    if ( *idx == 'i' ) {
      idx++;
      return Pii(0, 1);
    }
 
    else return Pii(tmp, 0);
  }
};

int main()
{
  string s;
  while ( cin >> s ) {
    is_over = false;
    Parser ps;
    string tmp = s.substr(0, s.size() - 1);
    State idx = s.begin();

    Pii ret = ps.expression(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    if ( is_over ) cout << "overflow" << endl;
    else if ( ret.fr == 0 && ret.sc == 0 ) cout << 0 << endl;
    else if ( ret.fr == 0 ) {
      printf("%di\n", ret.sc);#include <bits/stdc++.h>

using namespace std;

typedef string::const_iterator State;
typedef pair<int, int> Pii;

#define fr first
#define sc second

bool is_over = false;

struct Parser {
  Pii expression(State &idx) {
    Pii ret = term(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    while ( 1 ) {
      if ( *idx == '+' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr + tmp.fr;
	ret.sc = ret.sc + tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else if ( *idx == '-' ) {
	idx++;
	Pii tmp = term(idx);
	ret.fr = ret.fr - tmp.fr;
	ret.sc = ret.sc - tmp.sc;
	if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }
  
  Pii term(State &idx) {
    Pii ret = factor(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    //
    while ( 1 ) {
      if ( *idx == '*' ) {
	idx++;
	Pii tmp = factor(idx), tmp2 = ret;
	//cout << ret.fr << " " << ret.sc << endl;
	//cout << tmp.fr << " " << tmp.sc << endl;
	ret.fr = tmp2.fr * tmp.fr - tmp2.sc * tmp.sc;
	ret.sc = tmp2.fr * tmp.sc + tmp2.sc * tmp.fr;
	if ( abs(ret.fr)> 10000 || abs(ret.sc) > 10000 ) is_over = true;
	//	cout << ret.fr << " " << ret.sc << endl;
      } else {
	break;
      }
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii factor(State &idx) {
    Pii ret;
    if ( *idx == '(' ) {
      idx++;
      ret = expression(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      idx++;
    } else {
      ret = number(idx);
      if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    }
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    return ret;
  }

  Pii number(State &idx) {
    int tmp = 0;
    while ( '0' <= *idx && *idx <= '9' ) {
      tmp *= 10;
      tmp += *idx - '0';
      if ( abs(tmp) > 10000 ) is_over = true;
      idx++;
      /*if ( *idx == 'i' ) {
	is_real = false;
	idx++;
	}*/
    }
    if ( abs(tmp) > 10000 ) is_over = true;
    //cout << tmp << endl;
    if ( *idx == 'i' ) {
      idx++;
      return Pii(0, 1);
    }
 
    else return Pii(tmp, 0);
  }
};

int main()
{
  string s;
  while ( cin >> s ) {
    is_over = false;
    Parser ps;
    string tmp = s.substr(0, s.size() - 1);
    State idx = s.begin();

    Pii ret = ps.expression(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    if ( is_over ) cout << "overflow" << endl;
    else if ( ret.fr == 0 && ret.sc == 0 ) cout << 0 << endl;
    else if ( ret.fr == 0 ) {
      printf("%di\n", ret.sc);
    }
    else if ( ret.sc == 0 ) {
      printf("%d\n", ret.fr);
    }
    else {
      printf("%d", ret.fr);
      printf("%+di\n", ret.sc);
      //   cout << ret.fr << '+' << ret.sc << 'i' << endl;
    }
  }

  return 0;
}

    }
    else if ( ret.sc == 0 ) {
      printf("%d\n", ret.fr);
    }
    else {
      printf("%d", ret.fr);
      printf("%+di\n", ret.sc);
      //   cout << ret.fr << '+' << ret.sc << 'i' << endl;
    }
  }

  return 0;
}

{
  string s;
  while ( cin >> s ) {
    is_over = false;
    Parser ps;
    string tmp = s.substr(0, s.size() - 1);
    State idx = s.begin();

    Pii ret = ps.expression(idx);
    if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
    if ( is_over ) cout << "overflow" << endl;
    else if ( ret.fr == 0 && ret.sc == 0 ) cout << 0 << endl;
    else if ( ret.fr == 0 ) {
      printf("%di\n", ret.sc);
    }
    else if ( ret.sc == 0 ) {
      printf("%d\n", ret.fr);
    }
    else {
      printf("%d", ret.fr);
      printf("%+di\n", ret.sc);
      //   cout << ret.fr << '+' << ret.sc << 'i' << endl;
    }
  }

  return 0;
}

Compiler output:
a.cc:98:11: error: stray '#' in program
   98 | int main()#include <bits/stdc++.h>
      |           ^
a.cc:209:31: error: stray '#' in program
  209 |       printf("%di\n", ret.sc);#include <bits/stdc++.h>
      |                               ^
a.cc:98:12: error: expected initializer before 'include'
   98 | int main()#include <bits/stdc++.h>
      |            ^~~~~~~
a.cc:108:6: error: redefinition of 'bool is_over'
  108 | bool is_over = false;
      |      ^~~~~~~
a.cc:11:6: note: 'bool is_over' previously defined here
   11 | bool is_over = false;
      |      ^~~~~~~
a.cc:110:8: error: redefinition of 'struct Parser'
  110 | struct Parser {
      |        ^~~~~~
a.cc:13:8: note: previous definition of 'struct Parser'
   13 | struct Parser {
      |        ^~~~~~
a.cc: In function 'int main()':
a.cc:209:32: error: 'include' was not declared in this scope
  209 |       printf("%di\n", ret.sc);#include <bits/stdc++.h>
      |                                ^~~~~~~
a.cc:209:41: error: 'bits' was not declared in this scope
  209 |       printf("%di\n", ret.sc);#include <bits/stdc++.h>
      |                                         ^~~~
a.cc:209:46: error: 'stdc' was not declared in this scope; did you mean 'std'?
  209 |       printf("%di\n", ret.sc);#include <bits/stdc++.h>
      |                                              ^~~~
      |                                              std
a.cc:211:1: error: expected primary-expression before 'using'
  211 | using namespace std;
      | ^~~~~
a.cc: In member function 'Pii main()::Parser::expression(State&)':
a.cc:224:55: error: use of local variable with automatic storage from containing function
  224 |     if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                       ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:231:59: error: use of local variable with automatic storage from containing function
  231 |         if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                           ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:237:59: error: use of local variable with automatic storage from containing function
  237 |         if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                           ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:242:55: error: use of local variable with automatic storage from containing function
  242 |     if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                       ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc: In member function 'Pii main()::Parser::term(State&)':
a.cc:248:55: error: use of local variable with automatic storage from containing function
  248 |     if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                       ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:258:58: error: use of local variable with automatic storage from containing function
  258 |         if ( abs(ret.fr)> 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                          ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:264:55: error: use of local variable with automatic storage from containing function
  264 |     if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                       ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc: In member function 'Pii main()::Parser::factor(State&)':
a.cc:273:57: error: use of local variable with automatic storage from containing function
  273 |       if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                         ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:277:57: error: use of local variable with automatic storage from containing function
  277 |       if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                         ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:279:55: error: use of local variable with automatic storage from containing function
  279 |     if ( abs(ret.fr) > 10000 || abs(ret.sc) > 10000 ) is_over = true;
      |                                                       ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc: In member function 'Pii main()::Parser::number(State&)':
a.cc:288:31: error: use of local variable with automatic storage from containing function
  288 |       if ( abs(tmp) > 10000 ) is_over = true;
      |                               ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc:295:29: error: use of local variable with automatic storage from containing function
  295 |     if ( abs(tmp) > 10000 ) is_over = true;
      |                             ^~~~~~~
a.cc:219:6: note: 'bool is_over' declared here
  219 | bool is_over = false;
      |      ^~~~~~~
a.cc: In function 'int main()':
a.cc:306:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  306 | int main()
      |         ^~
a.cc:306:9: note: remove parentheses to default-initialize a variable
  306 | int main()
      |         ^~
      |         --
a.cc:306:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:307:1: error: a function-definition is not allowed here before '{' token
  307 | {
      | ^
a.cc: At global scope:
a.cc:349:1: error: expected unqualified-id before '{' token
  349 | {
      | ^