fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string compute(string s){
  4. if (s.length() <= 1) {
  5. return s;
  6. }
  7. stack<int> t;
  8. int global = 1;
  9. int start = 0;
  10. if (s[0] == '(') {
  11. t.push(1);
  12. start = 1;
  13. }
  14. for (int i=start; i < s.length() ; i++) {
  15. if (s[i] == ')') {
  16. if (t.top() == 0){
  17. global = !global;
  18. }
  19. t.pop();
  20. } else if (s[i]=='-' || s[i]=='+') {
  21. if (s[i+1] == '(') {
  22. if (s[i] == '-') {
  23. t.push(0);
  24. global = !global;
  25. }
  26. i++;
  27. if (global == 0)
  28. cout << '-';
  29. else
  30. cout << '+';
  31. } else {
  32. if (global == 1) {
  33. cout<<s[i];
  34. } else {
  35. if (s[i] == '-') {
  36. cout<<'+';
  37. } else {
  38. cout << '-';
  39. }
  40. }
  41. }
  42. } else {
  43. cout << s[i];
  44. }
  45. }
  46. }
  47. int main() {
  48. string s = "-(a-(b+c)-d";
  49. cout<<compute(s);
  50. return 0;
  51. }
Runtime error #stdin #stdout #stderr 0s 4276KB
stdin
Standard input is empty
stdout
-a+b+c+d@N�$VM�$VN�$V�����

T� � ���o�B�$VE�$V�B�$V
�@aA�+`�$VP�I�$V`H�$V8	���o���o����o�o�G�$V���ol��+���+@sK�+� haA�+pj �+K�$V&K�$V6K�$V@M��+�k��+�HJ�+pgJ�+�DR�+�K�$VpjK�+�K�$V�K�$V�K�$V�K�$V�`�$VWJ�+�	y�+�	y�+�`�$V@vy�+�Vy�+`py�+�oy�+py�+
stderr
*** Error in `./prog': free(): invalid pointer: 0x00005624fae65d88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2b85ebf41bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2b85ebf47f96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x2b85ebf4878e]
./prog(+0xc87)[0x5624fac64c87]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x2b85ebef12b1]
./prog(+0xd6a)[0x5624fac64d6a]
======= Memory map: ========
2b85eb1f1000-2b85eb214000 r-xp 00000000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b85eb214000-2b85eb218000 rw-p 00000000 00:00 0 
2b85eb221000-2b85eb226000 rw-p 00000000 00:00 0 
2b85eb414000-2b85eb415000 r--p 00023000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b85eb415000-2b85eb416000 rw-p 00024000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2b85eb416000-2b85eb417000 rw-p 00000000 00:00 0 
2b85eb417000-2b85eb589000 r-xp 00000000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b85eb589000-2b85eb789000 ---p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b85eb789000-2b85eb793000 r--p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b85eb793000-2b85eb795000 rw-p 0017c000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b85eb795000-2b85eb799000 rw-p 00000000 00:00 0 
2b85eb799000-2b85eb89c000 r-xp 00000000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b85eb89c000-2b85eba9b000 ---p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b85eba9b000-2b85eba9c000 r--p 00102000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b85eba9c000-2b85eba9d000 rw-p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2b85eba9d000-2b85ebab3000 r-xp 00000000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b85ebab3000-2b85ebcb2000 ---p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b85ebcb2000-2b85ebcb3000 r--p 00015000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b85ebcb3000-2b85ebcb4000 rw-p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b85ebcb4000-2b85ebccc000 r-xp 00000000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b85ebccc000-2b85ebecb000 ---p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b85ebecb000-2b85ebecc000 r--p 00017000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b85ebecc000-2b85ebecd000 rw-p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b85ebecd000-2b85ebed1000 rw-p 00000000 00:00 0 
2b85ebed1000-2b85ec066000 r-xp 00000000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b85ec066000-2b85ec265000 ---p 00195000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b85ec265000-2b85ec269000 r--p 00194000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b85ec269000-2b85ec26b000 rw-p 00198000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2b85ec26b000-2b85ec26f000 rw-p 00000000 00:00 0 
2b85f0000000-2b85f0021000 rw-p 00000000 00:00 0 
2b85f0021000-2b85f4000000 ---p 00000000 00:00 0 
5624fac64000-5624fac66000 r-xp 00000000 fd:00 23353346                   /home/oTwKlk/prog
5624fae65000-5624fae66000 r--p 00001000 fd:00 23353346                   /home/oTwKlk/prog
5624fae66000-5624fae67000 rw-p 00002000 fd:00 23353346                   /home/oTwKlk/prog
5624fc26b000-5624fc29d000 rw-p 00000000 00:00 0                          [heap]
7ffe5f2c0000-7ffe5f2e1000 rw-p 00000000 00:00 0                          [stack]
7ffe5f3da000-7ffe5f3dc000 r-xp 00000000 00:00 0                          [vdso]
7ffe5f3dc000-7ffe5f3de000 r--p 00000000 00:00 0                          [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]