fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5.  
  6. int foo_mult ( int x , int y )
  7. {
  8. return x * y ;
  9. }
  10.  
  11. int foo_plus ( int x , int y )
  12. {
  13. return x + y ;
  14. }
  15.  
  16. int foo_minus ( int x , int y )
  17. {
  18. return x - y ;
  19. }
  20.  
  21.  
  22.  
  23. int main()
  24. {
  25. std::vector < int > vec ;
  26. std::map < const char , int (*)(int,int) > oper ;
  27. std::string str ;
  28. std::getline ( std::cin , str ) ;
  29. int num = 0 ;
  30. while ( std::cin >> num ) {
  31. vec.push_back ( num ) ;
  32. }
  33. for ( std::string::size_type i = 0 ; i < str.size() ; ++i ) {
  34. int (*foo)(int,int) = 0 ;
  35. switch ( str[i] )
  36. {
  37. case '-' : foo = foo_minus ; break ;
  38. case '+' : foo = foo_plus ; break ;
  39. case '*' : foo = foo_mult ; break ;
  40. }
  41. oper [ str[i] ] = foo ;
  42. }
  43. int result = vec[0] ;
  44. for ( std::vector<int>::size_type i = 1 ; i < vec.size() ; ++i ) {
  45. result = oper[str[i-1]](result,vec[i]) ;
  46. }
  47. std::cout << "Result = " << result << std::endl ;
  48. }
Success #stdin #stdout 0s 3480KB
stdin
++-*+
10 4 6 8 3 14
stdout
Result = 50