Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.70 KB | None | 0 0
  1. // a very simple and only mildly scientific calculator
  2. // original BCX basic by Kevin Diggins
  3. // modified from BCX generated C code for Dev C++
  4. // allows only one operation at a time (need to improve this):
  5. // 4 * 5 + 3 has to solved via 4 * 5 = then + 3 =
  6. // a Dev-C++ tested Windows Application by vegaseat 03oct2004
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <string>
  10. #include <windows.h>
  11. #define SPACE " "
  12. static HINSTANCE BCX_hInstance;
  13. static int BCX_ScaleX;
  14. static int BCX_ScaleY;
  15. static char BCX_ClassName[2048];
  16. static HWND Form1;
  17. static HWND Edit1;
  18. static double Op1;
  19. static double Op2;
  20. static long OpFlag;
  21. static long EqFlag;
  22. static long Code;
  23. // some macros used
  24. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  25. #define VAL(a) (double)atof(a)
  26. int str_cmp(char*, char*);
  27. HWND BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
  28. HWND BCX_Edit(char*,HWND,int,int,int,int,int,int=0,int=-1);
  29. HWND BCX_Button(char*,HWND,int=0,int=0,int=0,int=0,int=0,int=0,int=-1);
  30. char* BCX_Get_Text(HWND);
  31. int BCX_Set_Text(HWND,char*);
  32. HFONT BCX_Set_Font (char *,int,int=0,int=0,int=0,int=0);
  33. LRESULT Set_Color (int,int,int,int);
  34. char* BCX_TmpStr(size_t);
  35. char* trim (char*);
  36. char* str (double);
  37. int instr(char*,char*,int=0,int=0);
  38. char *_stristr_(char*,char*);
  39. void FormLoad (void);
  40. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  41. void ProcessNumber (int);
  42. void ProcessOperator (int);
  43. // standard main for windows GUI
  44. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
  45. {
  46. WNDCLASS Wc;
  47. MSG Msg;
  48. // *****************************
  49. strcpy(BCX_ClassName,"CALC1");
  50. // ************************************
  51. // Scale Dialog Units To Screen Units
  52. // ************************************
  53. RECT rc = {0,0,4,8};
  54. MapDialogRect (NULL,&rc);
  55. BCX_ScaleX = rc.right/2;
  56. BCX_ScaleY = rc.bottom/4;
  57. BCX_hInstance = hInst;
  58. // ******************************************************
  59. Wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  60. Wc.lpfnWndProc = WndProc;
  61. Wc.cbClsExtra = 0;
  62. Wc.cbWndExtra = 0;
  63. Wc.hInstance = hInst;
  64. Wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
  65. Wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  66. Wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  67. Wc.lpszMenuName = NULL;
  68. Wc.lpszClassName = BCX_ClassName;
  69. RegisterClass(&Wc);
  70. FormLoad();
  71. // message loop, windows GUI functions via messages
  72. while(GetMessage(&Msg,NULL,0,0))
  73. {
  74. HWND hActiveWindow = GetActiveWindow();
  75. if(!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
  76. {
  77. TranslateMessage(&Msg);
  78. DispatchMessage(&Msg);
  79. }
  80. }
  81. return Msg.wParam;
  82. }
  83. // circular storage to prevent memory leaks
  84. char *BCX_TmpStr (size_t Bites)
  85. {
  86. static int StrCnt;
  87. static char *StrFunc[2048];
  88. StrCnt=(StrCnt + 1) & 2047;
  89. if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
  90. return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
  91. }
  92. // fancy string compare
  93. int str_cmp (char *a, char *b)
  94. {
  95. register int counter;
  96. counter=counter^counter;
  97. while(1)
  98. {
  99. if((a[counter]^b[counter]))
  100. {
  101. if((UINT) a[counter]>= (UINT) b[counter])
  102. {
  103. return 1;
  104. }
  105. return -1;
  106. }
  107. if(!a[counter]) return 0;
  108. counter++;
  109. }
  110. return 0;
  111. }
  112. // remove certain leading and trailing characters
  113. char *trim (char *S)
  114. {
  115. register int i = strlen(S);
  116. char *strtmp = BCX_TmpStr(i);
  117. strcpy(strtmp,S);
  118. while(i--)
  119. {
  120. if(S[i]!=0 &&
  121. S[i]!=9 &&
  122. S[i]!=10 &&
  123. S[i]!=11 &&
  124. S[i]!=13 &&
  125. S[i]!=32) break;
  126. }
  127. strtmp[++i]=0;
  128. if (strtmp)
  129. {
  130. while(*(strtmp+1) &&
  131. *strtmp=='\x00' ||
  132. *strtmp=='\x09' ||
  133. *strtmp=='\x0A' ||
  134. *strtmp=='\x0B' ||
  135. *strtmp=='\x0D' ||
  136. (*strtmp=='\x20' ))
  137. strtmp++;
  138. }
  139. return strtmp;
  140. }
  141. // sets accuracy
  142. char *str (double d)
  143. {
  144. register char *strtmp = BCX_TmpStr(16);
  145. sprintf(strtmp,"% .15G",d);
  146. return strtmp;
  147. }
  148. // returns the position where a substring is located in a string
  149. int instr(char* mane,char* match,int offset,int sensflag)
  150. {
  151. register char *s;
  152. if (!mane || !match || ! *match || offset>(int)strlen(mane)) return 0;
  153. if (sensflag)
  154. s = _stristr_(offset>0 ? mane+offset-1 : mane,match);
  155. else
  156. s = strstr (offset>0 ? mane+offset-1 : mane,match);
  157. return s ? (int)(s-mane)+1 : 0;
  158. }
  159. // supports instr()
  160. char *_stristr_(char *String, char *Pattern)
  161. {
  162. register char *pptr, *sptr, *start;
  163. register UINT slen, plen;
  164. for (start = (char *)String,
  165. pptr = (char *)Pattern,
  166. slen = strlen(String),
  167. plen = strlen(Pattern);
  168. slen >= plen;
  169. start++, slen--)
  170. {
  171. while (toupper(*start) != toupper(*Pattern))
  172. {
  173. start++;
  174. slen--;
  175. if (slen < plen) return(0);
  176. }
  177. sptr = start;
  178. pptr = (char *)Pattern;
  179. while (toupper(*sptr) == toupper(*pptr))
  180. {
  181. sptr++;
  182. pptr++;
  183. if (!*pptr) return (start);
  184. }
  185. }
  186. return(0);
  187. }
  188. // created the form
  189. HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
  190. {
  191. HWND A;
  192. // assign default style
  193. if (!Style)
  194. {
  195. Style = WS_MINIMIZEBOX |
  196. WS_SIZEBOX |
  197. WS_CAPTION |
  198. WS_MAXIMIZEBOX |
  199. WS_POPUP |
  200. WS_SYSMENU;
  201. }
  202. A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
  203. Style,
  204. X*BCX_ScaleX,
  205. Y*BCX_ScaleY,
  206. (4+W)*BCX_ScaleX,
  207. (12+H)*BCX_ScaleY,
  208. NULL,(HMENU)NULL,BCX_hInstance,NULL);
  209. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  210. (LPARAM)MAKELPARAM(FALSE,0));
  211. return A;
  212. }
  213. // create the various calculator buttons
  214. HWND BCX_Button
  215. (char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
  216. {
  217. HWND A;
  218. // assign default style
  219. if(!Style)
  220. {
  221. Style=WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_PUSHBUTTON | WS_TABSTOP;
  222. }
  223. if(Exstyle==-1)
  224. {
  225. Exstyle=WS_EX_STATICEDGE;
  226. }
  227. A = CreateWindowEx(Exstyle,"button",Text,Style,
  228. X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
  229. hWnd,(HMENU)id,BCX_hInstance,NULL);
  230. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  231. (LPARAM)MAKELPARAM(FALSE,0));
  232. if (W==0)
  233. {
  234. HDC hdc=GetDC(A);
  235. SIZE size;
  236. GetTextExtentPoint32(hdc,Text,strlen(Text),&size);
  237. ReleaseDC(A,hdc);
  238. MoveWindow(A,X*BCX_ScaleX,Y*BCX_ScaleY,(int)(size.cx+(size.cx*0.5)),(int)(size.cy+(size.cy*0.32)),TRUE);
  239. }
  240. return A;
  241. }
  242. // create the edit box to display entry and result
  243. HWND BCX_Edit
  244. (char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
  245. {
  246. HWND A;
  247. // assign default style
  248. if (!Style)
  249. {
  250. Style = WS_CHILD | WS_VISIBLE | ES_WANTRETURN |
  251. WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL;
  252. }
  253. if (Exstyle==-1)
  254. {
  255. Exstyle = WS_EX_CLIENTEDGE;
  256. }
  257. A = CreateWindowEx(Exstyle,"edit",Text, Style,
  258. X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
  259. hWnd,(HMENU)id,BCX_hInstance,NULL);
  260.  
  261. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  262. (LPARAM)MAKELPARAM(FALSE,0));
  263. return A;
  264. }
  265. char *BCX_Get_Text(HWND hWnd)
  266. {
  267. int tmpint;
  268. tmpint = 1 + GetWindowTextLength(hWnd);
  269. char *strtmp = BCX_TmpStr(tmpint);
  270. GetWindowText(hWnd,strtmp,tmpint);
  271. return strtmp;
  272. }
  273. int BCX_Set_Text(HWND hWnd, char *Text)
  274. {
  275. return SetWindowText(hWnd,Text);
  276. }
  277. LRESULT Set_Color (int TxtColr,int BkColr,int wParam,int lParam)
  278. {
  279. static HBRUSH ReUsableBrush;
  280. DeleteObject(ReUsableBrush);
  281. ReUsableBrush=CreateSolidBrush(BkColr);
  282. SetTextColor((HDC)wParam,TxtColr);
  283. SetBkColor((HDC)wParam,BkColr);
  284. return (LRESULT)ReUsableBrush;
  285. }
  286. HFONT BCX_Set_Font (char *Font,int Size,int Bold,int Italic,int Underline,int StrikeThru)
  287. {
  288. HDC hDC = GetDC(HWND_DESKTOP);
  289. int CyPixels = GetDeviceCaps(hDC,LOGPIXELSY);
  290. ReleaseDC(HWND_DESKTOP,hDC);
  291. return CreateFont(0-(Size*CyPixels)/72,0,0,0,Bold,Italic,Underline,StrikeThru,
  292. 0,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,Font);
  293. }
  294. // details like corner coordinates,width,length,text,font
  295. void FormLoad (void)
  296. {
  297. Form1 = BCX_Form("One op calculator ...",67,42,185,130);
  298. Edit1 = BCX_Edit(SPACE,Form1,99,-1,3,200,20);
  299. SendMessage(Edit1,(UINT)WM_SETFONT,(WPARAM)BCX_Set_Font("Verdana",14),1);
  300. BCX_Button("0",Form1,100,6,97,50,20);
  301. BCX_Button("1",Form1,101,6,74,20,20);
  302. BCX_Button("2",Form1,102,36,74,20,20);
  303. BCX_Button("3",Form1,103,66,74,20,20);
  304. BCX_Button("4",Form1,104,6,52,20,20);
  305. BCX_Button("5",Form1,105,36,52,20,20);
  306. BCX_Button("6",Form1,106,66,52,20,20);
  307. BCX_Button("7",Form1,107,6,29,20,20);
  308. BCX_Button("8",Form1,108,36,29,20,20);
  309. BCX_Button("9",Form1,109,66,29,20,20);
  310. BCX_Button(".",Form1,110,66,96,20,20);
  311. BCX_Button("CE",Form1,112,132,29,20,20);
  312. BCX_Button("-",Form1,113,132,52,20,20);
  313. BCX_Button("/",Form1,114,132,74,20,20);
  314. BCX_Button("%",Form1,115,132,96,20,20);
  315. BCX_Button("C",Form1,116,103,29,20,20);
  316. BCX_Button("+",Form1,117,103,52,20,20);
  317.  
  318. BCX_Button("X",Form1,118,103,74,20,20);
  319. BCX_Button("=",Form1,119,103,96,20,20);
  320. BCX_Button("1/X",Form1,120,162,29,20,20);
  321. BCX_Button("X^2",Form1,121,162,52,20,20);
  322. BCX_Button("SQR",Form1,122,162,74,20,20);
  323. BCX_Button("neg",Form1,123,162,96,20,20);
  324. Show(Form1);
  325. }
  326. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  327. {
  328. while(1)
  329. {
  330. if(LOWORD(wParam)>99&&LOWORD(wParam)<110)
  331. {
  332. ProcessNumber(LOWORD(wParam)-100);
  333. break;
  334. }
  335. if(LOWORD(wParam)>109)
  336. {
  337. ProcessOperator(LOWORD(wParam));
  338. }
  339. break;
  340. }
  341. if((HWND)lParam==Edit1 && Msg==WM_CTLCOLOREDIT)
  342. return Set_Color(RGB(0,225,0),RGB(0,0,0),wParam,lParam);
  343. // tidy up and exit program
  344. if(Msg==WM_DESTROY)
  345. {
  346. UnregisterClass(BCX_ClassName,BCX_hInstance);
  347. PostQuitMessage(0);
  348. }
  349. return DefWindowProc(hWnd,Msg,wParam,lParam);
  350. }
  351. void ProcessNumber (int Digit)
  352. {
  353. char A[2048];
  354. strcpy(A,(char*)BCX_Get_Text(Edit1));
  355. if(str_cmp(A,SPACE)==0)
  356. {
  357. *A=0;
  358. }
  359. if(EqFlag)
  360. {
  361. *A=0;
  362. EqFlag=FALSE;
  363. }
  364. if(OpFlag)
  365. {
  366. *A=0;
  367. BCX_Set_Text(Edit1,"");
  368. OpFlag=FALSE;
  369. }
  370. sprintf(A,"%s%s",A,trim(str(Digit)));
  371. BCX_Set_Text(Edit1,A);
  372. }
  373. void ProcessOperator (int Operator)
  374. {
  375. char A[2048];
  376. strcpy(A,(char*)BCX_Get_Text(Edit1));
  377. if(str_cmp(A,SPACE)==0)
  378. {
  379. *A=0;
  380. }
  381. while(1)
  382. {
  383. if(Operator==110) // . period
  384. {
  385. if(EqFlag)
  386. {
  387. *A=0;
  388. EqFlag=FALSE;
  389. }
  390. if(OpFlag)
  391. {
  392. *A=0;
  393. BCX_Set_Text(Edit1,"");
  394. OpFlag=FALSE;
  395. }
  396. if(instr(A,".")==0)
  397. {
  398. sprintf(A,"%s%s",A,".");
  399. }
  400. BCX_Set_Text(Edit1,A);
  401. break;
  402. }
  403. if(Operator==112) // CE clear last entry
  404. {
  405. BCX_Set_Text(Edit1,SPACE);
  406. EqFlag=FALSE;
  407. break;
  408. }
  409. if(Operator==113) // - subtract
  410. {
  411. Op1=VAL(BCX_Get_Text(Edit1));
  412. Code=4;
  413. OpFlag=TRUE;
  414. EqFlag=FALSE;
  415. break;
  416. }
  417. if(Operator==114) // / divide
  418. {
  419. Op1=VAL(BCX_Get_Text(Edit1));
  420. Code=2;
  421. OpFlag=TRUE;
  422. EqFlag=FALSE;
  423. break;
  424. }
  425. if(Operator==115) // % percent to decimal
  426. {
  427. Op2=VAL(BCX_Get_Text(Edit1))/100;
  428. BCX_Set_Text(Edit1,trim(str(Op2)));
  429. EqFlag=FALSE;
  430. break;
  431. }
  432. if(Operator==116) // C clear all
  433. {
  434. BCX_Set_Text(Edit1,SPACE);
  435. Op1=0;
  436. Op2=0;
  437. Code=0;
  438. EqFlag=FALSE;
  439. break;
  440. }
  441. if(Operator==117) // + add
  442. {
  443. Op1=VAL(BCX_Get_Text(Edit1));
  444. Code=3;
  445. OpFlag=TRUE;
  446. EqFlag=FALSE;
  447. break;
  448. }
  449. if(Operator==118) // x multiply
  450. {
  451. Op1=VAL(BCX_Get_Text(Edit1));
  452. Code=1;
  453. OpFlag=TRUE;
  454. EqFlag=FALSE;
  455. break;
  456. }
  457. if(Operator==119) // = equals
  458. {
  459. Op2=VAL(BCX_Get_Text(Edit1));
  460. OpFlag=FALSE;
  461. EqFlag=TRUE;
  462. while(1)
  463. {
  464. if(Code==1)
  465. {
  466. Op1*=Op2;
  467. break;
  468. }
  469. if(Code==2)
  470. {
  471. Op1/=Op2;
  472. break;
  473. }
  474. if(Code==3)
  475. {
  476. Op1+=Op2;
  477. break;
  478. }
  479. if(Code==4)
  480. {
  481. Op1-=Op2;
  482. }
  483. break;
  484. }
  485. strcpy(A,(char*)trim(str(Op1)));
  486. if(instr(A,"INF"))
  487. {
  488. strcpy(A,"Error: Divison by zero");
  489. }
  490. BCX_Set_Text(Edit1,A);
  491. Op2=0;
  492. break;
  493. }
  494. if(Operator==120) // 1/x invert
  495. {
  496. BCX_Set_Text(Edit1,str(1/VAL(BCX_Get_Text(Edit1))));
  497. OpFlag=TRUE;
  498. EqFlag=FALSE;
  499. break;
  500. }
  501. if(Operator==121) // x^2 square
  502. {
  503. BCX_Set_Text(Edit1,str(VAL(BCX_Get_Text(Edit1))*VAL(BCX_Get_Text(Edit1))));
  504. OpFlag=TRUE;
  505. EqFlag=FALSE;
  506. break;
  507. }
  508. if(Operator==122) // sqrt square-root
  509. {
  510. BCX_Set_Text(Edit1,str(sqrt(VAL(BCX_Get_Text(Edit1)))));
  511. OpFlag=TRUE;
  512. EqFlag=FALSE;
  513. break;
  514. }
  515. if(Operator==123) // neg = negate
  516. {
  517. BCX_Set_Text(Edit1,str(-1*VAL(BCX_Get_Text(Edit1))));
  518. OpFlag=TRUE;
  519. EqFlag=FALSE;
  520. }
  521. break;
  522. }
  523. }
  524. // **************** credit to Kevin **************************
  525. // Created with BCX -- The BASIC To C Translator (ver 5.02)
  526. // BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
  527. // *************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement