Posted By

maggie_guo on 11/10/10


Tagged

Calculator 664


Versions (?)

Objective -c 2.0 Chapter 6.5 Exercise


 / Published in: Objective C
 

Begins Calculations 10 S Set Accumulator to 10 = 10.000000 contens of Accumulator 2 / Divide by 2 = 5.000000 contents of Accumulator 55 - Substract 55 = - 50.000000 100.25 S Set Accumulator to 100.25 = 100.25 4 * Multiply by 4 = 401.000000 0 E End of program = 401.000000 End of Calculations

  1. //Program to implement the following:
  2. //Enter the expression: number operator
  3. // + - * / S E
  4. // S: scanf mode
  5. // E: end of program
  6. /*Begins Calculations
  7.  10 S Set Accumulator to 10
  8.  = 10.000000 contens of Accumulator
  9.  2 / Divide by 2
  10.  = 5.000000 contents of Accumulator
  11.  55 - Substract 55
  12.  = - 50.000000
  13.  100.25 S Set Accumulator to 100.25
  14.  = 100.25
  15.  4 * Multiply by 4
  16.  = 401.000000
  17.  0 E End of program
  18.  = 401.000000
  19.  End of Calculations
  20.  */
  21.  
  22. #import <Foundation/Foundation.h>
  23. @interface Calculator : NSObject
  24. {
  25. double accumulator;
  26. }
  27.  
  28. -(void) setAccumulator :(double) value;
  29. -(void) clear;
  30. -(double) accumulator;
  31.  
  32. //arithmetic methods
  33. -(double) add:(double) value;
  34. -(double) substract:(double) value;
  35. -(double) multiply:(double) value;
  36. -(double) divide :(double) value;
  37.  
  38. @end
  39. @implementation Calculator
  40.  
  41. -(void) setAccumulator:(double) value
  42. {
  43. accumulator = value;
  44. }
  45. -(void) clear {
  46. accumulator = 0.0;
  47. }
  48. -(double) accumulator {
  49. return accumulator;
  50. }
  51.  
  52. -(double) add: (double) value {
  53. accumulator += value;
  54. return accumulator;
  55. }
  56.  
  57. -(double) substract:(double) value {
  58. accumulator -= value;
  59. return accumulator;
  60. }
  61. -(double) multiply:(double) value {
  62. accumulator *= value;
  63. return accumulator;
  64. }
  65. -(double) divide :(double) value {
  66. if( value == 0){
  67. NSLog(@"Division by 0");
  68. return 99999.9;
  69. }else {
  70. accumulator /= value;
  71. return accumulator;
  72. }
  73. }
  74.  
  75. @end
  76.  
  77. int main (int argc, const char * argv[]) {
  78. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  79. double value1,value2;
  80. char operator;
  81. Calculator *deskCal = [[Calculator alloc] init];
  82. NSLog(@"Begin Calculations");
  83. //set Accumulator to 10
  84. NSLog(@"Please Enter you expression");
  85. scanf("%lf %c", &value1, &operator);
  86. [deskCal setAccumulator:value1];
  87. NSLog(@"=%f", [deskCal accumulator]);
  88. if(operator =='S'){
  89. do{
  90. scanf("%lf %c", &value2, &operator);
  91. if(operator =='+')
  92. [deskCal add:value2];
  93. else if( operator =='-')
  94. [deskCal substract:value2];
  95. else if(operator == '*')
  96. [deskCal multiply:value2];
  97. else if(operator =='/')
  98. [deskCal divide:value2];
  99. else if(operator =='S')
  100. [deskCal setAccumulator:value2];
  101. else if (operator =='E'){
  102. NSLog(@"=%f", [deskCal accumulator]);
  103. NSLog(@"End of Calculations");
  104. break;
  105. }
  106. else
  107. NSLog(@"Unknown Operator");
  108. NSLog(@"=%f", [deskCal accumulator]);
  109. }while(operator != 'E');
  110. }
  111.  
  112. [pool drain];
  113. return 0;
  114. }

Report this snippet  

You need to login to post a comment.