fork download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class Stack
  7. {
  8. private :
  9. char *elements ;
  10. int Size ;
  11. int top = -1 ;
  12. public :
  13. Stack ()
  14. {
  15. Size = 100 ;
  16. elements = new char [Size] ;
  17. }
  18. void puch ( char x )
  19. {
  20. if ( top != Size+1 )
  21. elements[++top] = x ;
  22. }
  23.  
  24. void pop ()
  25. {
  26. top-- ;
  27. }
  28.  
  29. void view ()
  30. {
  31. for ( int i = 0 ; i <= top ; i++ )
  32. cout << elements[i] << endl ;
  33. }
  34.  
  35. char get_the_last ( )
  36. {
  37. return elements[top] ;
  38. }
  39.  
  40. bool Is_empty ()
  41. {
  42. if ( top < 0 ) return true ;
  43. return false ;
  44. }
  45.  
  46. };
  47.  
  48. bool check ( char x , char y )
  49. {
  50. if ( x == '(' || x == '[' || x == '{' ) return false ;
  51. else if ( y == '(' || y == '[' || y == '{' ) return false ;
  52. else if ( x == '-' ) return true ;
  53. else if ( y == '-' ) return false ;
  54. else if ( x == '*' && y == '/' ) return false ;
  55. else if ( x == '*' && y == '+' ) return false ;
  56. else if ( x == '/' && y == '*' ) return true ;
  57. else if ( x == '/' && y == '+' ) return false ;
  58. else true ;
  59.  
  60. }
  61.  
  62. char get_bracket ( char x )
  63. {
  64. if ( x == ')' ) return '(' ;
  65. else if ( x == '}' ) return '{' ;
  66. else return '[' ;
  67. }
  68.  
  69. string Handel_Exp ( string exp )
  70. {
  71. Stack s ;
  72. string e ;
  73. int i = 0 , q = 0 ;
  74.  
  75. while ( i < exp.length() )
  76. {
  77. if ( exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' )
  78. {
  79. if ( q > 0 )
  80. {
  81. e+=' ' ;
  82. q = 0 ;
  83. }
  84. if ( s.Is_empty() == false )
  85. {
  86. char prev = s.get_the_last() ;
  87. if ( check(exp[i],prev) == true )
  88. {
  89. while ( s.get_the_last() != '(' || s.get_the_last() != '{' || s.get_the_last() != '[' )
  90. {
  91. e = e+s.get_the_last() ;
  92. s.pop() ;
  93. if ( s.Is_empty() == true ) break ;
  94. if (check(exp[i],s.get_the_last()) != true ) break ;
  95.  
  96. }
  97. s.puch(exp[i]) ;
  98. }
  99. else s.puch(exp[i]) ;
  100. }
  101.  
  102. else
  103. {
  104. s.puch(exp[i]) ;
  105. }
  106. }
  107.  
  108. else if ( exp[i] == ')' || exp[i] == '}' || exp[i] == ']' )
  109. {
  110. if ( q > 0 )
  111. {
  112. e+=' ' ;
  113. q = 0 ;
  114. }
  115. char bracket = get_bracket(exp[i]) ;
  116. while ( s.get_the_last() != bracket )
  117. {
  118. e = e+s.get_the_last() ;
  119. s.pop() ;
  120. }
  121. s.pop() ;
  122. }
  123. else
  124. {
  125. e = e + exp[i] ;
  126. q++ ;
  127. }
  128. i++ ;
  129. }
  130. if ( exp[i] != '+' || exp[i] != '-' || exp[i] != '/' || exp[i] != '*' ) { e+=' ' ; }
  131. while ( s.Is_empty() == false )
  132. {
  133. e = e + s.get_the_last() ;
  134. s.pop() ;
  135. }
  136.  
  137. return e ;
  138. }
  139.  
  140. string calculate ( string x , string y , char op )
  141. {
  142. stringstream ss ;
  143. float first=0 , second=0 , third=0 ;
  144. string z ;
  145. ss.clear() ;
  146. ss << x ;
  147. ss >> first ;
  148. ss.clear() ;
  149. ss << y ;
  150. ss >> second ;
  151.  
  152. if ( op == '+' ) third = first + second ;
  153. else if ( op == '-' ) third = first - second ;
  154. else if ( op == '*' ) third = first * second ;
  155. else if ( op == '/' ) third = first / second ;
  156.  
  157. ss.clear() ;
  158. ss << third ;
  159. ss >> z ;
  160.  
  161. return z ;
  162.  
  163. }
  164.  
  165. string Reverse ( string x )
  166. {
  167. cout << x << endl ;
  168. string y ;
  169. int i = x.length()-1 ;
  170.  
  171. while ( i >= 0 )
  172. {
  173. y+= x[i] ;
  174. i-- ;
  175. }
  176.  
  177. return y ;
  178. }
  179.  
  180. string evaluate ( string x )
  181. {
  182. Stack s ;
  183. int i = 0 ;
  184. string first , second ;
  185. while ( i < x.length() )
  186. {
  187. if ( x[i] == '+' || x[i] == '-' || x[i] == '*' || x[i] == '/' )
  188. {
  189. s.pop() ;
  190. while ( s.get_the_last() != ' ' )
  191. {
  192. second += s.get_the_last() ;
  193. s.pop() ;
  194.  
  195. }
  196.  
  197. s.pop() ;
  198. while ( s.get_the_last() != ' ' && s.Is_empty() == false )
  199. {
  200. first += s.get_the_last() ;
  201. s.pop() ;
  202. }
  203. first = Reverse(first) ;
  204. second = Reverse(second) ;
  205. string third = calculate (first , second , x[i]) ;
  206. first.clear() ;
  207. second.clear() ;
  208.  
  209. int q = 0 ;
  210. while ( q < third.length() )
  211. {
  212. s.puch(third[q]) ;
  213. q++ ;
  214. }
  215. s.puch(' ') ;
  216.  
  217. }
  218.  
  219. else s.puch(x[i]) ;
  220. i++ ;
  221. }
  222.  
  223. string result ;
  224. s.pop() ;
  225. while ( s.Is_empty() == false )
  226. {
  227. result +=s.get_the_last() ;
  228. s.pop() ;
  229. }
  230. result = Reverse(result) ;
  231. return result ;
  232. }
  233.  
  234. bool openPrace(char c)
  235. {
  236. if(c=='{' || c=='(' || c=='[') return true;
  237. return false;
  238. }
  239.  
  240. bool closePrace(char c)
  241. {
  242. if(c=='}' || c==')' || c==']') return true;
  243. return false;
  244. }
  245.  
  246. bool matched(char o,char c)
  247. {
  248. if(o=='{' && c=='}') return true;
  249. if(o=='(' && c==')') return true;
  250. if(o=='[' && c==']') return true;
  251. return false;
  252. }
  253.  
  254. bool checkPraces(string exp)
  255. {
  256. Stack s;
  257. for(int i=0;i< exp.length();i++)
  258. {
  259. char c=exp[i];
  260. if(openPrace(c))
  261. {
  262. s.puch(c);
  263. }
  264. else if(closePrace(c))
  265. {
  266. if(s.Is_empty()) return false;
  267. char tmp=s.get_the_last();
  268. if(matched(tmp,c)) s.pop();
  269. else return false;
  270. }
  271. }
  272. return s.Is_empty();
  273. }
  274.  
  275.  
  276. int main()
  277. {
  278. string x ;
  279. cin >> x ;
  280. string exp = Handel_Exp (x) ;
  281. if ( checkPraces(x) == true )
  282. {
  283. cout << "matched" << endl ;
  284. string c = evaluate(exp) ;
  285. cout << c << endl ;
  286. }
  287. else
  288. cout << "not matched" << endl ;
  289.  
  290.  
  291. return 0;
  292. }
  293.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
matched