cal.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "lib.h"
  2. #include <math.h>
  3. INT store = 0, read = 0; WCHAR op; BOOL once_op = FALSE, once_op_temp = FALSE;
  4. extern HWND hPutMain, hPutLite;
  5. BOOL divZero = FALSE;
  6. static INT cal(INT a, INT b, WCHAR op)
  7. {
  8. switch (op)
  9. {
  10. case '+':
  11. return a + b;
  12. case '-':
  13. return a - b;
  14. case '*':
  15. return a * b;
  16. case '/':
  17. if (b == 0)
  18. {
  19. divZero = TRUE;
  20. return 0;
  21. }
  22. return a / b;
  23. case '^':
  24. return pow(a, b);
  25. }
  26. }
  27. static VOID update_cal()
  28. {
  29. if (once_op_temp || !once_op)
  30. {
  31. std::wstring str = std::to_wstring(read);
  32. LPCWSTR output = str.c_str();
  33. SendMessage(hPutLite, WM_SETTEXT, FALSE, (LPARAM)output);
  34. }
  35. else
  36. {
  37. std::wstring str = std::to_wstring(cal(store, read, op));
  38. LPCWSTR output = str.c_str();
  39. SendMessage(hPutLite, WM_SETTEXT, FALSE, (LPARAM)output);
  40. }
  41. }
  42. VOID clear()
  43. {
  44. store = 0; read = 0;
  45. once_op = FALSE; once_op_temp = FALSE;
  46. LPCWSTR output = L"0";
  47. SendMessage(hPutMain, WM_SETTEXT, FALSE, (LPARAM)output);
  48. SendMessage(hPutLite, WM_SETTEXT, FALSE, (LPARAM)output);
  49. divZero = FALSE;
  50. }
  51. VOID update_equal()
  52. {
  53. if (divZero)
  54. {
  55. MessageBox(
  56. hWndMain, L"cannot divide zero",
  57. MB_OK, NULL
  58. );
  59. clear();
  60. return;
  61. }
  62. update_cal();
  63. read = cal(store, read, op);
  64. std::wstring str = std::to_wstring(read);
  65. LPCWSTR output = str.c_str();
  66. SendMessage(hPutMain, WM_SETTEXT, FALSE, (LPARAM)output);
  67. once_op = FALSE;
  68. once_op_temp = FALSE;
  69. }
  70. static BOOL update_equal_fromOP()
  71. {
  72. if (divZero)
  73. {
  74. MessageBox(
  75. hWndMain, L"cannot divide zero",
  76. MB_OK, NULL
  77. );
  78. clear();
  79. return FALSE;
  80. }
  81. update_cal();
  82. store = cal(store, read, op);
  83. std::wstring str = std::to_wstring(store);
  84. LPCWSTR output = str.c_str();
  85. SendMessage(hPutMain, WM_SETTEXT, FALSE, (LPARAM)output);
  86. read = 0;
  87. return TRUE;
  88. }
  89. static BOOL update_input(LPCWSTR x)
  90. {
  91. INT length = GetWindowTextLength(hPutMain);
  92. LPWSTR buffer = (LPWSTR)malloc((length + 1) * sizeof(WCHAR));
  93. SendMessage(hPutMain, WM_GETTEXT, 100, (LPARAM)buffer);
  94. BOOL is_op = !(x[0] != '+' && x[0] != '-' && x[0] != '*' && x[0] != '/' && x[0] != '^');
  95. if (is_op)
  96. {
  97. if (!once_op)
  98. {
  99. store = read;
  100. read = 0;
  101. }
  102. else if (once_op_temp)
  103. return FALSE;
  104. else
  105. if (!update_equal_fromOP())
  106. return FALSE;
  107. op = x[0];
  108. once_op = TRUE;
  109. once_op_temp = TRUE;
  110. }
  111. else
  112. {
  113. read = read * 10 + _wtoi(x);
  114. once_op_temp = FALSE;
  115. }
  116. length = GetWindowTextLength(hPutMain);
  117. if(buffer != NULL && length == 1 && buffer[0] == L'0' && !is_op)
  118. SendMessage(hPutMain, WM_SETTEXT, FALSE, (LPARAM)x);
  119. else
  120. {
  121. SendMessage(hPutMain, EM_SETSEL, length, length);
  122. SendMessage(hPutMain, EM_REPLACESEL, FALSE, (LPARAM)x);
  123. }
  124. return TRUE;
  125. }
  126. VOID update(LPCWSTR x)
  127. {
  128. divZero = FALSE;
  129. if(update_input(x))
  130. update_cal();
  131. }
  132. VOID del()
  133. {
  134. if (read == 0&& !once_op_temp)
  135. return;
  136. INT length = GetWindowTextLength(hPutMain);
  137. SendMessage(hPutMain, EM_SETSEL, length-1, length);
  138. SendMessage(hPutMain, EM_REPLACESEL, FALSE, NULL);
  139. LPWSTR buffer = (LPWSTR)malloc((length + 1) * sizeof(WCHAR));
  140. SendMessage(hPutMain, WM_GETTEXT, 100, (LPARAM)buffer);
  141. if (once_op_temp)
  142. {
  143. read = store;
  144. once_op = FALSE;
  145. once_op_temp = FALSE;
  146. }
  147. else
  148. read /= 10;
  149. update_cal();
  150. }