fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6. class ICommand{
  7. public:
  8. virtual void execute(istream &args) = 0;
  9. };
  10.  
  11. class Add : public ICommand{
  12. void execute(istream &args){
  13. int a,b;
  14. args >> a;
  15. args >> b;
  16. cout << "Add result:" << a + b << endl;
  17. }
  18. };
  19.  
  20. class Negate : public ICommand{
  21. void execute(istream &args){
  22. int a;
  23. args >> a;
  24. cout <<"Negate result:"<< -a << endl;
  25. }
  26. };
  27.  
  28. int main(){
  29. map<string,ICommand*> commands;
  30.  
  31. commands["Add"] = new Add();
  32. commands["Negate"] = new Negate();
  33. string cmd;
  34. while(cin >> cmd){
  35. auto it = commands.find(cmd);
  36. if(it!= commands.end()){
  37. it->second->execute(cin);
  38. }else{
  39. cout << "Unknown command \"" << cmd << "\"." << endl;
  40. }
  41. }
  42. }
Success #stdin #stdout 0s 3480KB
stdin
Add 1 2
Negate 4
stdout
Add result:3
Negate result:-4