Guest User

Untitled

a guest
Apr 26th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. // Calc.h file:
  2.  
  3. #import <Foundation/Foundation.h>
  4. #import <Foundation/NSString.h>
  5. #import <Cocoa/Cocoa.h>
  6.  
  7. int tGlobalVar = 0;
  8. int xGlobalVar = 0;
  9. int gGlobalVar = 0;
  10.  
  11. @interface Calc : NSObject
  12. {
  13. IBOutlet NSTextField *answer;
  14. IBOutlet NSTextField *a;
  15. IBOutlet NSTextField *b;
  16. IBOutlet NSTextField *c;
  17. IBOutlet NSTextField *percent;
  18. IBOutlet NSTextField *x;
  19. IBOutlet NSTextField *g;
  20. }
  21. -(IBAction) answer: (id) sender;
  22. //-(IBAction) right: (id) sender;
  23. -(IBAction) wrong: (id) sender;
  24. -(IBAction) clearAll: (id) sender;
  25. -(IBAction) multiply: (id) sender;
  26. @end
  27.  
  28.  
  29. // Calc.m file:
  30.  
  31. #import "Calc.h"
  32. @implementation Calc
  33. -(IBAction) multiply: (id) sender
  34. {
  35. NSInteger buttonTitle = [sender tag];
  36. [answer setIntegerValue: buttonTitle];
  37. }
  38. -(IBAction) answer: (id) sender
  39. {
  40. int aAnswer, bAnswer, cAnswer, result;
  41. aAnswer = [a intValue];
  42. bAnswer = [b intValue];
  43. cAnswer = [c intValue];
  44.  
  45. result = aAnswer * bAnswer;
  46.  
  47. if (cAnswer != result)
  48. {
  49. ++tGlobalVar;
  50. ++xGlobalVar;
  51. [x setIntValue: xGlobalVar];
  52. }
  53. else if ((cAnswer == result) && (cAnswer != 0))
  54. {
  55. ++tGlobalVar;
  56. ++gGlobalVar;
  57. [g setIntValue: gGlobalVar];
  58. }
  59. }
  60. -(IBAction) clearAll: (id) sender
  61. {
  62. [a setIntValue: 0];
  63. [b setIntValue: 0];
  64. [c setIntValue: 0];
  65. [answer setIntValue: 0];
  66. gGlobalVar = gGlobalVar - gGlobalVar;
  67. [x setIntValue: gGlobalVar];
  68. xGlobalVar = xGlobalVar - xGlobalVar;
  69. [g setIntValue: gGlobalVar];
  70. NSString *wrongValue = @"Please enter a value to begin";
  71. [percent setStringValue: wrongValue];
  72. }
  73. -(IBAction) wrong: (id) sender
  74. {
  75. float num, denum, result;
  76.  
  77. num = [x floatValue];
  78. denum = (float) tGlobalVar;
  79. result = (num/denum)*100.0;
  80.  
  81. if (xGlobalVar == 0 && gGlobalVar == 0)
  82. {
  83. NSString *wrongValue = @"You must first compute to begin.";
  84. [percent setStringValue: wrongValue];
  85. }
  86. else if (xGlobalVar == 0 && gGlobalVar != 0)
  87. {
  88. NSString *wrongValue = @"You have 100% correct.";
  89. [percent setStringValue: wrongValue];
  90. }
  91. else if (xGlobalVar != 0 && gGlobalVar == 0)
  92. {
  93. NSString *wrongValue = @"You have 100% incorrect.";
  94. [percent setStringValue: wrongValue];
  95. }
  96. else if (xGlobalVar != 0 && gGlobalVar != 0)
  97. {
  98. NSString *wrongValue = [NSString stringWithFormat: @"The amount you have wrong is %.1f%%", result];
  99. [percent setStringValue: wrongValue];
  100. }
  101. }
  102. @end
Add Comment
Please, Sign In to add comment