Guest User

Untitled

a guest
May 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. using namespace std;
  5.  
  6. enum mathType
  7. {
  8. ADD = 0,
  9. SUB = 1,
  10. MUL = 2,
  11. DIV = 3
  12. };
  13.  
  14. double calculate(mathType op, double num1, double num2);
  15. mathType getSumType();
  16.  
  17. int main ()
  18. {
  19. mathType type;
  20. double num1, num2;
  21.  
  22. cout << "x, /, + or - : ";
  23. type = getSumType();
  24.  
  25. cout << "Please enter number 1: ";
  26. cin >> num1;
  27. cout << endl;
  28.  
  29. cout << "Please enter number 2: ";
  30. cin >> num2;
  31. cout << endl;
  32.  
  33. cout << calculate (type, num1, num2) << endl;
  34.  
  35. system("PAUSE");
  36. return 0;
  37.  
  38. }
  39.  
  40. mathType getSumType()
  41. {
  42. char input = NULL;
  43. mathType type;
  44.  
  45. while(input != 'x' && input != '/' && input != '+' && input != '-')
  46. {
  47. cin >> input;
  48.  
  49. switch(input)
  50. {
  51. case 'x':
  52. type = MUL;
  53. break;
  54. case '/':
  55. type = DIV;
  56. break;
  57. case '+':
  58. type = ADD;
  59. break;
  60. case '-':
  61. type = SUB;
  62. break;
  63. default:
  64. cout << endl << "Invalid command!" << endl;
  65. break;
  66. }
  67. }
  68.  
  69. return type;
  70. }
  71.  
  72. double calculate (mathType op, double num1, double num2)
  73. {
  74. switch(op)
  75. {
  76. case MUL:
  77. return (num1*num2);
  78. break;
  79. case DIV:
  80. return (num1/num2);
  81. break;
  82. case ADD:
  83. return (num1+num2);
  84. break;
  85. case SUB:
  86. return (num1-num2);
  87. break;
  88. default:
  89. MessageBox(NULL, "Error, invalid type of primitive!", "Error!", NULL);
  90. return NULL;
  91. break;
  92. }
  93. }
Add Comment
Please, Sign In to add comment