1 module calcbrain; 2 3 import bindbc.cocoa; 4 import core.stdc.stdio; 5 6 import core.stdc.math : pow, sqrt, ceil; 7 8 import calcface; 9 10 CalcFace face; 11 double result = 0.0; 12 double enteredNumber = 0.0; 13 NSInteger op = none; 14 int fractionalDigits; 15 BOOL decSep = NO; 16 BOOL editing = YES; 17 18 // Set the corresponding face 19 void setFace(ref CalcFace aFace){ 20 face = aFace; 21 face.setDisplayedNumber(enteredNumber, decSep, fractionalDigits); 22 } 23 24 // Jump here on calculation errors 25 void error(){ 26 result = 0; 27 enteredNumber = 0; 28 op = none; 29 fractionalDigits = 0; 30 decSep = NO; 31 editing = YES; 32 face.setError(); 33 } 34 35 //extern (C): 36 // The various buttons 37 void clear(id sender, SEL sel, ...){ 38 result = 0; 39 enteredNumber = 0; 40 op = none; 41 fractionalDigits = 0; 42 decSep = NO; 43 editing = YES; 44 face.setDisplayedNumber(0, NO, 0); 45 } 46 47 void equal(id sender){ 48 switch (op) 49 { 50 case none: 51 result = enteredNumber; 52 enteredNumber = 0; 53 decSep = NO; 54 fractionalDigits = 0; 55 return; 56 case addition: 57 result = result + enteredNumber; 58 break; 59 case subtraction: 60 result = result - enteredNumber; 61 break; 62 case multiplication: 63 result = result * enteredNumber; 64 break; 65 case division: 66 if (enteredNumber == 0) 67 { 68 error(); 69 return; 70 } 71 else 72 result = result / enteredNumber; 73 break; 74 default: assert(0); 75 } 76 face.setDisplayedNumber(result, (ceil(result) != result), 7); 77 enteredNumber = result; 78 op = none; 79 editing = NO; 80 } 81 82 void digit(id sender, SEL sel, ...){ 83 import std.conv; 84 import std.stdio; 85 //writefln("You clicked the button with tag `%s`", NSButton(sender).tag); 86 if (!editing) 87 { 88 enteredNumber = 0; 89 decSep = NO; 90 fractionalDigits = 0; 91 editing = YES; 92 } 93 if(decSep){ 94 enteredNumber = enteredNumber + NSButton(sender).tag * pow (0.1, 1+fractionalDigits); 95 fractionalDigits++; 96 }else{ 97 enteredNumber = enteredNumber * 10 + NSButton(sender).tag; 98 if (enteredNumber > pow (10, 15)){ 99 error(); 100 return; 101 } 102 } 103 face.setDisplayedNumber(enteredNumber, decSep, fractionalDigits); 104 } 105 106 void decimalSeparator(id sender){ 107 if (!editing) 108 { 109 enteredNumber = 0; 110 decSep = NO; 111 fractionalDigits = 0; 112 editing = YES; 113 } 114 if (!decSep) 115 { 116 decSep = YES; 117 face.setDisplayedNumber(enteredNumber, YES, 0); 118 } 119 } 120 121 void operation(id sender, SEL sel, ...){ 122 if (op == none) 123 { 124 result = enteredNumber; 125 enteredNumber = 0; 126 decSep = NO; 127 fractionalDigits = 0; 128 op = NSButton(sender).tag; 129 130 } 131 else // operation 132 { 133 equal(null); 134 operation(sender, null); 135 } 136 137 } 138 139 void squareRoot(id sender, SEL sel, ...){ 140 if (op == none) 141 { 142 result = sqrt (enteredNumber); 143 face.setDisplayedNumber(result, (ceil(result) != result ? YES : NO), 7); 144 enteredNumber = result; 145 editing = NO; 146 op = none; 147 } 148 else // operation 149 { 150 equal(null); 151 squareRoot(sender, null); 152 } 153 }