fork download
  1. class TStr2PPN {
  2. AnsiString instr, outstr; //input & output strings
  3. char curc; //the current character
  4. int iin; //the index of the input string
  5.  
  6. char nextChar(); //get the next character
  7. void begin(); //handle plus & minus
  8. void mult_div(); //handle multiplication & division
  9. void symbol(); //handle characters & brackets
  10.  
  11. public:
  12. TStr2PPN() { //constructor
  13. iin = 1;
  14. }
  15.  
  16. void convert(char *str); //convert to PPN
  17. AnsiString get_outstr(); //get the output string
  18. };
  19.  
  20.  
  21. //get the next character
  22. inline char TStr2PPN::nextChar() {
  23. if(iin <= instr.Length()) return curc = instr[iin++];
  24. return curc = '\0';
  25. }
  26.  
  27. //get the output string
  28. inline AnsiString TStr2PPN::get_outstr(){return outstr;}
  29.  
  30. //convert to PPN
  31. void TStr2PPN::convert(char *str) {
  32. try {
  33. instr = str;
  34. outstr.SetLength(0);
  35. iin = 1;
  36. nextChar();
  37. //begin the convertation
  38. begin();
  39. if(iin != (instr.Length()+1) || curc != '\0') {
  40. throw Exception("Syntax error");
  41. }
  42. if(!isalpha(instr[instr.Length()]) && instr[instr.Length()]!=')') {
  43. throw Exception("Syntax error");
  44. }
  45. }
  46. catch(...) {throw;}
  47. }
  48.  
  49. //handle plus & minus
  50. void TStr2PPN::begin() {
  51. try {
  52. mult_div();
  53. while(curc=='+' || curc=='-') {
  54. char temp = curc;
  55. nextChar();
  56. mult_div();
  57. outstr += temp;
  58. }
  59. }
  60. catch(...) {throw;}
  61. }
  62.  
  63. //handle multiplication & division
  64. void TStr2PPN::mult_div() {
  65. try {
  66. symbol();
  67. while(curc=='*' || curc=='/') {
  68. char temp = curc;
  69. nextChar();
  70. symbol();
  71. outstr += temp;
  72. }
  73. }
  74. catch(...) {throw;}
  75. }
  76.  
  77. //handle characters
  78. void TStr2PPN::symbol() {
  79. try {
  80. if(curc=='(') {
  81. nextChar();
  82. begin();
  83. if(curc!=')') {
  84. throw Exception("Error: wrong number of brackets");
  85. }
  86. else nextChar();
  87. }
  88. else
  89. if(isalpha(curc)) {
  90. outstr += curc;
  91. nextChar();
  92. }
  93. else {throw Exception("Syntax error");}
  94. }
  95. catch(...) {throw;}
  96. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
(a+b)/(c+d)
compilation info
prog.cpp:2:5: error: ‘AnsiString’ does not name a type
     AnsiString instr, outstr;     //input & output strings
     ^~~~~~~~~~
prog.cpp:17:5: error: ‘AnsiString’ does not name a type
     AnsiString get_outstr();      //get the output string
     ^~~~~~~~~~
prog.cpp: In member function ‘char TStr2PPN::nextChar()’:
prog.cpp:23:14: error: ‘instr’ was not declared in this scope
    if(iin <= instr.Length()) return curc = instr[iin++];
              ^~~~~
prog.cpp: At global scope:
prog.cpp:28:8: error: ‘AnsiString’ does not name a type
 inline AnsiString TStr2PPN::get_outstr(){return outstr;}
        ^~~~~~~~~~
prog.cpp: In member function ‘void TStr2PPN::convert(char*)’:
prog.cpp:33:7: error: ‘instr’ was not declared in this scope
       instr = str;
       ^~~~~
prog.cpp:34:7: error: ‘outstr’ was not declared in this scope
       outstr.SetLength(0);
       ^~~~~~
prog.cpp:40:40: error: ‘Exception’ was not declared in this scope
          throw Exception("Syntax error");
                                        ^
prog.cpp:42:40: error: ‘isalpha’ was not declared in this scope
       if(!isalpha(instr[instr.Length()]) && instr[instr.Length()]!=')') {
                                        ^
prog.cpp:43:40: error: ‘Exception’ was not declared in this scope
          throw Exception("Syntax error");
                                        ^
prog.cpp: In member function ‘void TStr2PPN::begin()’:
prog.cpp:57:11: error: ‘outstr’ was not declared in this scope
           outstr += temp;
           ^~~~~~
prog.cpp: In member function ‘void TStr2PPN::mult_div()’:
prog.cpp:71:11: error: ‘outstr’ was not declared in this scope
           outstr += temp;
           ^~~~~~
prog.cpp: In member function ‘void TStr2PPN::symbol()’:
prog.cpp:84:62: error: ‘Exception’ was not declared in this scope
             throw Exception("Error: wrong number of brackets");
                                                              ^
prog.cpp:89:25: error: ‘isalpha’ was not declared in this scope
          if(isalpha(curc)) {
                         ^
prog.cpp:90:13: error: ‘outstr’ was not declared in this scope
             outstr += curc;
             ^~~~~~
prog.cpp:93:46: error: ‘Exception’ was not declared in this scope
          else {throw Exception("Syntax error");}
                                              ^
stdout
Standard output is empty