Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 lines
17 KiB

  1. /**************************************************************************/
  2. /*** SCICALC Scientific Calculator for Windows 3.00.12 ***/
  3. /*** By Kraig Brockschmidt, Microsoft Co-op, Contractor, 1988-1989 ***/
  4. /*** (c)1989 Microsoft Corporation. All Rights Reserved. ***/
  5. /*** ***/
  6. /*** scistat.c ***/
  7. /*** ***/
  8. /*** Functions contained: ***/
  9. /*** SetStat--Enable/disable the stat box, show or destroy the ***/
  10. /*** modeless dialog box. ***/
  11. /*** StatBoxProc--procedure for the statbox. Handles the RET, LOAD, ***/
  12. /*** CD, and CAD buttons, and handles double-clicks. ***/
  13. /*** StatFunctions--routines for DATA, SUM, AVE, and deviations. ***/
  14. /*** ***/
  15. /*** Functions called: ***/
  16. /*** SetStat ***/
  17. /*** ***/
  18. /*** Last modification Thu 26-Jan-1990 ***/
  19. /*** -by- Amit Chatterjee [amitc] 26-Jan-1990. ***/
  20. /*** Following bug fix was made: ***/
  21. /*** ***/
  22. /*** Bug # 8499. ***/
  23. /*** While fixing numbers in the stat array in memory, instead of using ***/
  24. /*** the following for statement: ***/
  25. /*** for (lIndex=lData; lIndex < lStatNum - 1 ; lIndex++) ***/
  26. /*** the fix was to use: ***/
  27. /*** for (lIndex=lData; lIndex < lStatNum ; lIndex++) ***/
  28. /*** This is because lStatNum has already been decremented to care of ***/
  29. /*** a number being deleted. ***/
  30. /*** This fix will be in build 1.59. ***/
  31. /**************************************************************************/
  32. #include "scicalc.h"
  33. #include "calchelp.h"
  34. #include "unifunc.h"
  35. #define GMEMCHUNK 96L /* Amount of memory to allocate at a time. */
  36. extern HNUMOBJ ghnoNum;
  37. extern HWND hStatBox, hListBox, hEdit;
  38. extern TCHAR szBlank[6], *rgpsz[CSTRINGS];
  39. extern LPTSTR gpszNum;
  40. extern INT nTempCom;
  41. extern BOOL gbRecord;
  42. extern BOOL FireUpPopupMenu( HWND, HINSTANCE, LPARAM );
  43. GLOBALHANDLE hgMem, hMem; /* Coupla global memory handles. */
  44. BOOL bFocus=TRUE;
  45. LONG lStatNum=0, /* Number of data. */
  46. lReAllocCount; /* Number of data before ReAlloc. */
  47. HNUMOBJ * lphnoStatNum; /* Holding place for stat data. */
  48. /* Initiate or destroy the Statistics Box. */
  49. VOID APIENTRY SetStat (BOOL bOnOff)
  50. {
  51. static int aStatOnlyKeys[] = { IDC_AVE, IDC_B_SUM, IDC_DEV, IDC_DATA };
  52. int i;
  53. if (bOnOff)
  54. {
  55. /* Create. */
  56. lReAllocCount=GMEMCHUNK/sizeof(ghnoNum); /* Set up lReAllocCount. */
  57. /* Start the box. */
  58. hStatBox=CreateDialog(hInst, MAKEINTRESOURCE(IDD_SB), NULL, StatBoxProc);
  59. /* Get a handle on some memory (16 bytes initially. */
  60. if (!(hgMem=GlobalAlloc(GHND, 0L)))
  61. {
  62. StatError();
  63. SendMessage(hStatBox, WM_COMMAND, GET_WM_COMMAND_MPS(ENDBOX, 0, 0));
  64. return;
  65. }
  66. ShowWindow(hStatBox, SW_SHOWNORMAL);
  67. }
  68. else
  69. {
  70. int lIndex;
  71. if ( hStatBox )
  72. {
  73. DestroyWindow(hStatBox);
  74. // Free the numobj's
  75. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  76. for( lIndex = 0; lIndex < lStatNum; lIndex++ )
  77. NumObjDestroy( &lphnoStatNum[lIndex] );
  78. GlobalUnlock(hgMem);
  79. lStatNum = 0;
  80. GlobalFree(hgMem); /* Free up the memory. */
  81. hStatBox=0; /* Nullify handle. */
  82. }
  83. }
  84. // set the active state of the Ave, Sum, s, and Dat buttons
  85. for ( i=0; i<ARRAYSIZE(aStatOnlyKeys); i++)
  86. EnableWindow( GetDlgItem(g_hwndDlg, aStatOnlyKeys[i]), bOnOff );
  87. return;
  88. }
  89. /* Windows procedure for the Dialog Statistix Box. */
  90. INT_PTR FAR APIENTRY StatBoxProc (
  91. HWND hStatBox,
  92. UINT iMessage,
  93. WPARAM wParam,
  94. LPARAM lParam)
  95. {
  96. static LONG lData=-1; /* Data index in listbox. */
  97. LONG lIndex; /* Temp index for counting. */
  98. DWORD dwSize; /* Holding place for GlobalSize. */
  99. static DWORD control[] = {
  100. IDC_STATLIST, CALC_SCI_STATISTICS_VALUE,
  101. IDC_CAD, CALC_SCI_CAD,
  102. IDC_CD, CALC_SCI_CD,
  103. IDC_LOAD, CALC_SCI_LOAD,
  104. IDC_FOCUS, CALC_SCI_RET,
  105. IDC_NTEXT, CALC_SCI_NUMBER,
  106. IDC_NUMTEXT, CALC_SCI_NUMBER,
  107. 0, 0 };
  108. switch (iMessage)
  109. {
  110. case WM_HELP:
  111. {
  112. LPHELPINFO phi = (LPHELPINFO)lParam;
  113. HWND hwndChild = GetDlgItem(hStatBox,phi->iCtrlId);
  114. WinHelp( hwndChild, rgpsz[IDS_HELPFILE], HELP_WM_HELP, (ULONG_PTR)(void *)control );
  115. return TRUE;
  116. }
  117. case WM_CONTEXTMENU:
  118. WinHelp( (HWND)wParam, rgpsz[IDS_HELPFILE], HELP_CONTEXTMENU, (ULONG_PTR)(void *)control );
  119. return TRUE;
  120. case WM_CLOSE:
  121. SetStat(FALSE);
  122. case WM_DESTROY:
  123. lStatNum=0L; /* Reset data count. */
  124. return(TRUE);
  125. case WM_INITDIALOG:
  126. /* Get a handle to this here things listbox display. */
  127. hListBox=GetDlgItem(hStatBox, IDC_STATLIST);
  128. return TRUE;
  129. case WM_COMMAND:
  130. /* Check for LOAD or double-click and recall number if so. */
  131. if (GET_WM_COMMAND_CMD(wParam, lParam)==LBN_DBLCLK ||
  132. GET_WM_COMMAND_ID(wParam, lParam)==IDC_LOAD)
  133. {
  134. /* Lock data, get pointer to it, and get index of item. */
  135. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  136. lData=(LONG)SendMessage(hListBox,LB_GETCURSEL,0,0L);
  137. if (lStatNum>0 && lData !=LB_ERR)
  138. // SPEED: REVIEW: can we use a pointer instead of Assign?
  139. NumObjAssign( &ghnoNum, lphnoStatNum[lData]); /* Get the data. */
  140. else
  141. MessageBeep(0); /* Cannodo if no data nor selection. */
  142. // Cancel kbd input mode
  143. gbRecord = FALSE;
  144. DisplayNum ();
  145. nTempCom = 32;
  146. GlobalUnlock(hgMem); /* Let the memory move! */
  147. break;
  148. }
  149. // switch (wParam)
  150. switch (GET_WM_COMMAND_ID(wParam, lParam))
  151. {
  152. case IDC_FOCUS:
  153. /* Change focus back to main window. Primarily for */
  154. /* use with the keyboard. */
  155. SetFocus(g_hwndDlg);
  156. return (TRUE);
  157. case IDC_CD:
  158. /* Clear the selected item from the listbox. */
  159. /* Get the index and a pointer to the data. */
  160. lData=(LONG)SendMessage(hListBox,LB_GETCURSEL,0,0L);
  161. /* Check for possible error conditions. */
  162. if (lData==LB_ERR || lData > lStatNum-1 || lStatNum==0)
  163. {
  164. MessageBeep (0);
  165. break;
  166. }
  167. /* Fix listbox strings. */
  168. lIndex=(LONG)SendMessage(hListBox, LB_DELETESTRING, (WORD)lData, 0L);
  169. if ((--lStatNum)==0)
  170. goto ClearItAll;
  171. /* Place the highlight over the next one. */
  172. if (lData<lIndex || lIndex==0)
  173. lIndex=lData+1;
  174. SendMessage(hListBox, LB_SETCURSEL, (WORD)lIndex-1, 0L);
  175. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  176. /* Fix numbers in memory. */
  177. for (lIndex=lData; lIndex < lStatNum ; lIndex++)
  178. {
  179. NumObjAssign( &lphnoStatNum[lIndex], lphnoStatNum[lIndex+1] );
  180. }
  181. GlobalUnlock(hgMem); /* Movin' again. */
  182. /* Update the number by the "n=". */
  183. SetDlgItemInt(hStatBox, IDC_NUMTEXT, lStatNum, FALSE);
  184. dwSize=(DWORD)GlobalSize(hgMem); /* Get size of memory block.*/
  185. /* Unallocate memory if not needed after data removal.*/
  186. /* hMem is used so we don't possibly trach hgMem. */
  187. if ((lStatNum % lReAllocCount)==0)
  188. if ((hMem=GlobalReAlloc(hgMem, dwSize-GMEMCHUNK, GMEM_ZEROINIT)))
  189. hgMem=hMem;
  190. return(TRUE);
  191. case IDC_CAD:
  192. ClearItAll:
  193. /* Nuke it all! */
  194. SendMessage(hListBox, LB_RESETCONTENT, 0L, 0L);
  195. SetDlgItemInt(hStatBox, IDC_NUMTEXT, 0, FALSE);;
  196. // Free the numobj's
  197. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  198. for( lIndex = 0; lIndex < lStatNum; lIndex++ )
  199. NumObjDestroy( &lphnoStatNum[lIndex] );
  200. GlobalUnlock(hgMem);
  201. GlobalFree(hgMem); /* Drop the memory. */
  202. lStatNum = 0;
  203. hgMem=GlobalAlloc(GHND, 0L); /* Get a CLEAN slate. */
  204. return(TRUE);
  205. }
  206. }
  207. return (FALSE);
  208. }
  209. /* Routine for functions AVE, SUM, DEV, and DATA. */
  210. VOID APIENTRY StatFunctions (WPARAM wParam)
  211. {
  212. LONG lIndex; /* Temp index. */
  213. DWORD dwSize; /* Return value for GlobalSize. */
  214. switch (wParam)
  215. {
  216. case IDC_DATA: /* Add current fpNum to listbox. */
  217. if ((lStatNum % lReAllocCount)==0)
  218. {
  219. /* If needed, allocate another 96 bytes. */
  220. dwSize=(DWORD)GlobalSize(hgMem);
  221. if (StatAlloc (1, dwSize))
  222. {
  223. GlobalCompact((DWORD)-1L);
  224. if (StatAlloc (1, dwSize))
  225. {
  226. StatError ();
  227. return;
  228. }
  229. }
  230. hgMem=hMem;
  231. }
  232. /* Add the display string to the listbox. */
  233. hListBox=GetDlgItem(hStatBox, IDC_STATLIST);
  234. lIndex=StatAlloc (2,0L);
  235. if (lIndex==LB_ERR || lIndex==LB_ERRSPACE)
  236. {
  237. GlobalCompact((DWORD)-1L);
  238. lIndex=StatAlloc (2,0L);
  239. if (lIndex==LB_ERR || lIndex==LB_ERRSPACE)
  240. {
  241. StatError ();
  242. return;
  243. }
  244. }
  245. /* Highlight last entered string. */
  246. SendMessage(hListBox, LB_SETCURSEL, (WORD)lIndex, 0L);
  247. /* Add the number and increase the "n=" value. */
  248. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  249. NumObjAssign( &lphnoStatNum[lStatNum], ghnoNum );
  250. SetDlgItemInt(hStatBox, IDC_NUMTEXT, ++lStatNum, FALSE);
  251. break;
  252. case IDC_AVE: /* Calculate averages and sums. */
  253. case IDC_B_SUM: {
  254. DECLARE_HNUMOBJ( hnoTemp );
  255. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  256. /* Sum the numbers or squares, depending on bInv. */
  257. NumObjAssign( &ghnoNum, HNO_ZERO );
  258. for (lIndex=0L; lIndex < lStatNum; lIndex++)
  259. {
  260. NumObjAssign( &hnoTemp, lphnoStatNum[lIndex] );
  261. if (bInv)
  262. {
  263. DECLARE_HNUMOBJ( hno );
  264. /* Get sum of squares. */
  265. NumObjAssign( &hno, hnoTemp );
  266. mulrat( &hno, hnoTemp );
  267. addrat( &ghnoNum, hno );
  268. NumObjDestroy( &hno );
  269. }
  270. else
  271. {
  272. /* Get sum. */
  273. addrat( &ghnoNum, hnoTemp );
  274. }
  275. }
  276. if (wParam==IDC_AVE) /* Divide by lStatNum=# of items for mean. */
  277. {
  278. DECLARE_HNUMOBJ( hno );
  279. if (lStatNum==0)
  280. {
  281. DisplayError (SCERR_DIVIDEZERO);
  282. break;
  283. }
  284. NumObjSetIntValue( &hno, lStatNum );
  285. divrat( &ghnoNum, hno );
  286. NumObjDestroy( &hno );
  287. }
  288. NumObjDestroy( &hnoTemp );
  289. /* Fall out for sums. */
  290. break;
  291. }
  292. case IDC_DEV: { /* Calculate deviations. */
  293. DECLARE_HNUMOBJ(hnoTemp);
  294. DECLARE_HNUMOBJ(hnoX);
  295. DECLARE_HNUMOBJ( hno );
  296. if (lStatNum <=1) /* 1 item or less, NO deviation. */
  297. {
  298. NumObjAssign( &ghnoNum, HNO_ZERO );
  299. return;
  300. }
  301. /* Get sum and sum of squares. */
  302. lphnoStatNum=(HNUMOBJ *)GlobalLock(hgMem);
  303. NumObjAssign( &ghnoNum, HNO_ZERO );
  304. NumObjAssign( &hnoTemp, HNO_ZERO );
  305. for (lIndex=0L; lIndex < lStatNum; lIndex++)
  306. {
  307. NumObjAssign(&hnoX, lphnoStatNum[lIndex]);
  308. addrat( &hnoTemp, hnoX );
  309. NumObjAssign( &hno, hnoX );
  310. mulrat( &hno, hnoX );
  311. addrat( &ghnoNum, hno );
  312. }
  313. /* x�- nx�/n� */
  314. /* fpTemp=fpNum-(fpTemp*fpTemp/(double)lStatNum);*/
  315. /* */
  316. NumObjSetIntValue( &hno, lStatNum );
  317. NumObjAssign( &hnoX, hnoTemp );
  318. mulrat( &hnoX, hnoTemp );
  319. divrat( &hnoX, hno );
  320. NumObjAssign( &hnoTemp, ghnoNum );
  321. subrat( &hnoTemp, hnoX );
  322. /* All numbers are identical if fpTemp==0 */
  323. if (NumObjIsZero( hnoTemp))
  324. NumObjAssign( &ghnoNum, HNO_ZERO); /* No deviation. */
  325. else {
  326. /* If bInv=TRUE, divide by n (number of data) otherwise */
  327. /* divide by n-1. */
  328. /* fpNum=sqrt(fpTemp/(lStatNum-1+(LONG)bInv)); */
  329. //
  330. // hno still equals lStatNum
  331. if (!bInv) {
  332. subrat( &hno, HNO_ONE );
  333. }
  334. divrat( &hnoTemp, hno );
  335. rootrat( &hnoTemp, HNO_TWO );
  336. NumObjAssign( &ghnoNum, hnoTemp );
  337. }
  338. NumObjDestroy( &hno );
  339. NumObjDestroy( &hnoX );
  340. NumObjDestroy( &hnoTemp );
  341. break;
  342. }
  343. }
  344. GlobalUnlock(hgMem); /* Da memwry is fwee to move as Findows fishes. */
  345. return;
  346. }
  347. LONG NEAR StatAlloc (WORD wType, DWORD dwSize)
  348. {
  349. LONG lRet=FALSE;
  350. if (wType==1)
  351. {
  352. if ((hMem=GlobalReAlloc(hgMem, dwSize+GMEMCHUNK, GMEM_ZEROINIT)))
  353. return 0L;
  354. }
  355. else
  356. {
  357. lRet=(LONG)SendMessage(hListBox, LB_ADDSTRING, 0, (LONG_PTR)(LPTSTR)gpszNum);
  358. return lRet;
  359. }
  360. return 1L;
  361. }
  362. VOID NEAR StatError (VOID)
  363. {
  364. TCHAR szFoo[50]; /* This comes locally. Gets the Stat Box Caption. */
  365. MessageBeep(0);
  366. /* Error if out of room. */
  367. GetWindowText(hStatBox, szFoo, 49);
  368. MessageBox(hStatBox, rgpsz[IDS_STATMEM], szFoo, MB_OK);
  369. return;
  370. }