Pages

Senin, 04 April 2011

PROGRAM KALKULATOR

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void instructUser();
  7. double doDivideZero(double &);
  8.  
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14. instructUser();
  15.  
  16.  
  17. double displayedVal;
  18. double newEntry;
  19. char command_character ;
  20.  
  21.  
  22. displayedVal = 0.0;
  23.  
  24.  
  25. cout << " Enter accepted Operator:" ;
  26. cin >> command_character;
  27. while (command_character != 'Q' || command_character != 'q')
  28. {
  29. switch(command_character)
  30. {
  31. case 'c':
  32. case 'C': displayedVal = 0.0;
  33. break;
  34. case '+': cout << " Enter Number:";
  35. cin >> newEntry;
  36. displayedVal = displayedVal + newEntry;
  37. break;
  38. case '-': cout << " Enter Number:";
  39. cin >> newEntry;
  40. displayedVal = displayedVal - newEntry;
  41. break;
  42. case '*': cout << " Enter Number:";
  43. cin >> newEntry;
  44. displayedVal = displayedVal * newEntry;
  45. break;
  46. case '/': cout << " Enter Number:";
  47. cin >> newEntry;
  48. displayedVal = displayedVal / newEntry;
  49. if (newEntry == 0)
  50. {
  51. doDivideZero(double &);
  52. }
  53.  
  54. break;
  55. case '^': cout << " Enter Number:";
  56. cin >> newEntry;
  57. displayedVal = pow (displayedVal,newEntry);
  58. break;
  59. default : cout << " Unacceptable Operator(" << command_character << ")" << endl;
  60. }
  61. cout << " The result so far is: " <<displayedVal<< endl;
  62. cout << " Enter Operator:";
  63. cin >> command_character;
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. system ("pause");
  72. return 0;
  73. }
  74.  
  75. void instructUser()
  76.  
  77. {
  78. cout << " " <<endl;
  79. cout << " ***************************************************************************" <<endl;
  80. cout << " * This program takes your input and selected mathematical operator *" <<endl;
  81. cout << " * and returns the answer to the screen. If an illegal operator is *" << endl;
  82. cout << " * selected, an error message will be displayed. Be careful which *" <<endl;
  83. cout << " * operator you select because the program depends on you for input. *" <<endl;
  84. cout << " * The only error check function it has, is for unacceptable opreators. *" <<endl;
  85. cout << " * Acceptable operators are : (+ , - , / , * ,^,c). The character c sets *" <<endl;
  86. cout << " * the value stored to ZERO. Enter Q to exit. ENJOY YOUR PROGRAM !!!!!! *" <<endl;
  87. cout << " ***************************************************************************" <<endl;
  88. cout << " " <<endl;
  89.  
  90.  
  91.  
  92. }
  93.  
  94. double doDivideZero(double &)
  95. {
  96. double newEntry;
  97. double displayedVal;
  98. newEntry =0;
  99. displayedVal = 0.0;
  100.  
  101.  
  102.  
  103. if (newEntry !=0)
  104. {
  105. displayedVal = displayedVal / newEntry;
  106. } else cout << "Wrong Operation, Cannot Divide by Zero" << endl;
  107.  
  108.  
  109.  
  110.  
  111. return 0;
  112. }
:cry: :cry: :cry:

0 komentar:

Posting Komentar