fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. typedef double Operation(double a,double b);
  8. const struct { char sign; Operation *op; } tb[]=
  9. {
  10. {'+',[](double a,double b) { return a+b; }},
  11. {'-',[](double a,double b) { return a-b; }},
  12. {'*',[](double a,double b) { return a*b; }},
  13. {'/',[](double a,double b) { return a/b; }},
  14. };
  15. char sign;
  16. for(double a,b;(cout<<"Podaj wyrażenie (np 2*2): ")&&(cin>>a>>sign>>b>>ws);)
  17. {
  18. for(size_t i=0;i<sizeof(tb)/sizeof(*tb);++i)
  19. {
  20. if(tb[i].sign==sign)
  21. {
  22. cout<<a<<' '<<sign<<' '<<b<<" = "<<tb[i].op(a,b)<<endl;
  23. sign=0;
  24. break;
  25. }
  26. }
  27. if(sign) cout<<"Nieznana operacja"<<endl;
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 4536KB
stdin
2*4
stdout
Podaj wyrażenie (np 2*2): 2 * 4 = 8
Podaj wyrażenie (np 2*2):