SGWndCal.c
上传用户:zhongmeidz
上传日期:2013-07-04
资源大小:102k
文件大小:13k
源码类别:

破解

开发平台:

Visual C++

  1. #include <sgwindow.h> /*SGWINDOW 程序必须的头文件*/
  2. #include <math.h>
  3. #define ID_ABOUT 10231
  4. #define ID_ZERO  10000
  5. #define ID_ONE   10001
  6. #define ID_TWO   10002
  7. #define ID_THREE 10003
  8. #define ID_FOUR  10004
  9. #define ID_FIVE  10005
  10. #define ID_SIX   10006
  11. #define ID_SEVEN 10007
  12. #define ID_EIGHT 10008
  13. #define ID_NINE  10009
  14. #define ID_DOT   10010
  15. #define ID_SIGN  10011
  16. #define ID_ADD   20001
  17. #define ID_MINUS 20002
  18. #define ID_MUTIPLE 20003
  19. #define ID_DIVIDE  20004
  20. #define ID_EQUAL   20006
  21. #define ID_SIN     20010
  22. #define ID_COS     20011
  23. #define ID_TAN     20012
  24. #define ID_LOG     20013
  25. #define ID_LN      20014
  26. #define ID_EXP     20015
  27. #define ID_POWER   20016
  28. #define ID_SQRT    20017
  29. #define ID_REV      20018
  30. #define ID_PERCENT  20019
  31. #define ID_ONOFF   30001
  32. #define ID_CLEAR   30002
  33. #define ID_RESULT  32000
  34. #define ID_STATU   31000
  35. #define ID_FRAME   31110
  36. #define VERSIONSTRING   "Calculate 1.0 For SGWINDOW 2.0 n Copyright (C) 2005-8 SGPROnn江西师范大学计算机02软件 殷圣鸽"
  37. #define XINTERVAL       40
  38. #define YINTERVAL       30
  39. void Dispatch_Message(WinMessage WinMsg); /*消息处理函数定义*/
  40. gennum(wndhnd);
  41. char *trim(char*);
  42. wndhnd  window;  /*主窗体句柄*/
  43. wndhnd  about; /* '关于' 按扭句柄*/
  44. wndhnd  digit[10],dot,sign;
  45. wndhnd  normaloperate[4];
  46. wndhnd  onoff,clear,equal;
  47. wndhnd  result;
  48. wndhnd  extraoperate[15];
  49. wndhnd  statu;
  50. wndhnd  fra;
  51. wndhnd  tip;
  52. double  operand1 = 0,operand2 = 0,res = 0;
  53. bool    opdready = false, opdready2 = false,resready =false;
  54. bool    enable = true;
  55. int     operate =0;
  56. char  *captions[] =
  57. {
  58. " 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 ",
  59. " . ","-/+",
  60. " + "," - "," * "," / "," =  ",
  61. " ON "," CE ",
  62. "sin","cos","tan","log"," ln ","exp","pow","sqr","1/x"," %  "
  63. };
  64. void main()
  65. {
  66. WinMessage msg; /*主消息*/
  67. winfont    wft; /*字体*/
  68. int    dcnt = 0;
  69. SGWINDOWinit(); /*加载SGWINDOW*/
  70. changewincircletime(0);
  71. setfont(&wft,1,4,WIN12,0); /*设置字体*/
  72. window = new_form(100,100,300,290,1,wft,6,"计算器 1.0 For SGWINDOW 2.0","exe.bmp",true,true,true,40); /*设置窗体属性*/
  73. result = new_Textbox(Get_FormUserarealeft(FORM(window))+6,
  74. Get_FormUserareatop(FORM(window))+10,
  75. 280,18,15,wft,"0",50,false,true,true,true);
  76. fra = new_frame(Get_FormUserarealeft(FORM(window))+6,
  77. Get_FormUserareatop(FORM(window))+40,
  78. 220,190,wft,"",true);
  79. for ( dcnt = 0; dcnt < 12; dcnt++)
  80. {
  81. if ( dcnt < 10 )
  82. {
  83. digit[dcnt] = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+(dcnt%3)*XINTERVAL,
  84. Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL, captions[dcnt]);
  85. }
  86. else if (dcnt == 10 )
  87. {
  88. dot = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+(dcnt%3)*XINTERVAL,
  89. Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL, captions[dcnt]);
  90. }
  91. else if (dcnt == 11 )
  92. {
  93. sign = new_Defaultbutton(Get_FormUserarealeft(FORM(window)) +
  94. 20+(dcnt%3)*XINTERVAL,
  95. Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL,
  96. captions[dcnt]);
  97. }
  98. }
  99. for ( dcnt = 0; dcnt < 4; dcnt++ )
  100. {
  101. normaloperate[dcnt] = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+3*XINTERVAL,
  102. Get_FormUserareatop(FORM(window))+50+dcnt*YINTERVAL, captions[dcnt+12]);
  103. }
  104. for (dcnt = 0; dcnt < 10; dcnt++)
  105. {
  106. extraoperate[dcnt] =
  107. new_Defaultbutton(Get_FormUserarealeft(FORM(window))+
  108. 20+(dcnt%5)*XINTERVAL,
  109. Get_FormUserareatop(FORM(window))+50+(4+dcnt/5)*YINTERVAL,
  110. captions[dcnt+19]);
  111. }
  112. statu = new_Defaultstatubar(Get_FormUserarealeft(FORM(window))+2,
  113. Get_FormUserareatop(FORM(window))+50+6*YINTERVAL+10,290,"就绪");
  114. onoff = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
  115. Get_FormUserareatop(FORM(window))+50, captions[17]);
  116. clear = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
  117. Get_FormUserareatop(FORM(window))+50+YINTERVAL, captions[18]);
  118. equal = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
  119. Get_FormUserareatop(FORM(window))+50+2*YINTERVAL, captions[16]);
  120. about = new_Defaultbutton(340,325," 关于 ");
  121. Register_Windowhandletoform(window,about,SGWNDBUTTON,ID_ABOUT);/*在主窗体中 注册 '关于' 按扭句柄 */
  122. Register_Windowhandletoform(window,result,SGWNDTEXTBOX,ID_RESULT);
  123. Register_Windowhandletoform(window,fra,SGWNDFRAME,ID_FRAME);
  124. for ( dcnt = 0; dcnt < 10; dcnt++)
  125. {
  126. Register_Windowhandletoform(window,digit[dcnt],SGWNDBUTTON,ID_ZERO+dcnt);
  127. }
  128. for ( dcnt = 0; dcnt < 4; dcnt++)
  129. {
  130. Register_Windowhandletoform(window,normaloperate[dcnt],SGWNDBUTTON,ID_ADD+dcnt);
  131. }
  132. for (dcnt = 0; dcnt < 10; dcnt++)
  133. {
  134. Register_Windowhandletoform(window,extraoperate[dcnt],
  135. SGWNDBUTTON,ID_SIN+dcnt);
  136. }
  137. Register_Windowhandletoform(window,dot,SGWNDBUTTON,ID_DOT);
  138. Register_Windowhandletoform(window,sign,SGWNDBUTTON,ID_SIGN);
  139. Register_Windowhandletoform(window,onoff,SGWNDBUTTON,ID_ONOFF);
  140. Register_Windowhandletoform(window,clear,SGWNDBUTTON,ID_CLEAR);
  141. Register_Windowhandletoform(window,equal,SGWNDBUTTON,ID_EQUAL);
  142. Register_Windowhandletoform(window,statu,SGWNDSTATUBAR,ID_STATU);
  143. Change_ButtonSize(equal,Get_ButtonSizewidth(BUTTON(equal)),
  144. Get_ButtonSizeheight(BUTTON(equal)) +YINTERVAL);
  145. while (TestMessageSource(window))/*当消息源存在时,进入消息循环*/
  146. {
  147. msg = WindowSend_Message(GetMessageSource(window));/*提取并发送消息*/
  148. Dispatch_Message(msg);  /*处理消息*/
  149. }
  150. }
  151. void Dispatch_Message(WinMessage msg) /*消息处理函数*/
  152. {
  153. int ObjectID = Get_MessagescrID(msg);      /*获取发送消息的对象的ID号*/
  154. int MessageType = Get_MessagescrType(msg); /*获取该消息的类型*/
  155. switch (ObjectID)
  156. {
  157. case ID_FORMCLOSE:
  158. if (MessageType==WE_MOUSEMOVE)
  159. {
  160. Set_StatubarText(statu,"关闭程序 ALT+F4");
  161. }
  162. break;
  163. case ID_MAINFORM:/*主窗体对象*/
  164. if (MessageType == WE_EXIT)/*主窗体发送退出消息*/
  165. {
  166. delete_form(&FORMPTR(window));/* 销毁主窗体*/
  167. }
  168. else if (MessageType == WE_FORMLOAD)/*主窗体发送加载消息*/
  169. {
  170. Show_Form(window);/*显示主窗体*/
  171. }
  172. else if (MessageType==WE_MOUSEMOVE)
  173. {
  174. Set_StatubarText(statu,"就绪");
  175. }
  176. break;
  177. case ID_STATU:
  178. if (MessageType==WE_MOUSEMOVE)
  179. {
  180. Set_StatubarText(statu,"计算器状态与帮助信息");
  181. }
  182. break;
  183. case ID_FRAME:
  184. if (MessageType==WE_MOUSEMOVE)
  185. {
  186. Set_StatubarText(statu,"就绪");
  187. }
  188. break;
  189. case ID_RESULT:
  190. if (MessageType==WE_MOUSEMOVE)
  191. {
  192. Set_StatubarText(statu,"结果显示屏, 数字超长时将分页显示");
  193. }
  194. break;
  195. case ID_ABOUT:/* '关于'按扭对象*/
  196. if (MessageType == WE_CLICK) /*关于按扭发送鼠标单击消息*/
  197. {
  198. Messagebox("Calculate For SGWINDOW",VERSIONSTRING,
  199. SGMSGBOX_OKONLY,SGMSGBOXICON_information);/*缺省形式的消息框,显示当前SGWINDOW的版本字符串*/
  200. }
  201. else if (MessageType==WE_MOUSEMOVE)
  202. {
  203. Set_StatubarText(statu,"关于本软件...");
  204. }
  205. break;
  206. case ID_ZERO:
  207. case ID_ONE:
  208. case ID_TWO:
  209. case ID_THREE:
  210. case ID_FOUR:
  211. case ID_FIVE:
  212. case ID_SIX:
  213. case ID_SEVEN:
  214. case ID_EIGHT:
  215. case ID_NINE:
  216. case ID_DOT:
  217. case ID_SIGN:
  218. if (MessageType==WE_CLICK && enable)
  219. {
  220. if ((opdready == true &&  opdready2 == false) ||
  221. resready == true)
  222. {
  223. if (ObjectID!=ID_SIGN)
  224. Change_TextboxText(result,"");
  225. }
  226. gennum(Get_WindowItemhandle(*GetMessageSource(window),ObjectID));
  227. }
  228. break;
  229. case ID_ADD:
  230. if (MessageType==WE_MOUSEMOVE)
  231. {
  232. Set_StatubarText(statu,"加法运算");
  233. goto NC;
  234. }
  235. case ID_MINUS:
  236. if (MessageType==WE_MOUSEMOVE)
  237. {
  238. Set_StatubarText(statu,"减法运算");
  239. goto NC;
  240. }
  241. case ID_MUTIPLE:
  242. if (MessageType==WE_MOUSEMOVE)
  243. {
  244. Set_StatubarText(statu,"乘法运算");
  245. goto NC;
  246. }
  247. case ID_DIVIDE:
  248. if (MessageType==WE_MOUSEMOVE)
  249. {
  250. Set_StatubarText(statu,"除法运算");
  251. goto NC;
  252. }
  253. NC:if (MessageType == WE_CLICK && enable)
  254. {
  255. if ( opdready && opdready2)
  256. {
  257. calculate();
  258. }
  259. if (resready)
  260. {
  261. operand1 = res;
  262. res = 0;
  263. resready = false;
  264. }
  265. operate = ObjectID -20000;
  266. opdready = true;
  267. }
  268. break;
  269. case ID_SIN:
  270. if (MessageType==WE_MOUSEMOVE)
  271. {
  272. Set_StatubarText(statu,"计算弧度正弦");
  273. goto EC;
  274. }
  275. case ID_COS:
  276. if (MessageType==WE_MOUSEMOVE)
  277. {
  278. Set_StatubarText(statu,"计算弧度余弦");
  279. goto EC;
  280. }
  281. case ID_TAN:
  282. if (MessageType==WE_MOUSEMOVE)
  283. {
  284. Set_StatubarText(statu,"计算弧度正切");
  285. goto EC;
  286. }
  287. case ID_LOG:
  288. if (MessageType==WE_MOUSEMOVE)
  289. {
  290. Set_StatubarText(statu,"计算10为底的对数");
  291. goto EC;
  292. }
  293. case ID_LN:
  294. if (MessageType==WE_MOUSEMOVE)
  295. {
  296. Set_StatubarText(statu,"计算e为底的对数");
  297. goto EC;
  298. }
  299. case ID_POWER:
  300. if (MessageType==WE_MOUSEMOVE)
  301. {
  302. Set_StatubarText(statu,"求平方");
  303. goto EC;
  304. }
  305. case ID_SQRT:
  306. if (MessageType==WE_MOUSEMOVE)
  307. {
  308. Set_StatubarText(statu,"求开方");
  309. goto EC;
  310. }
  311. case ID_REV:
  312. if (MessageType==WE_MOUSEMOVE)
  313. {
  314. Set_StatubarText(statu,"求倒数");
  315. goto EC;
  316. }
  317. case ID_EXP:
  318. if (MessageType==WE_MOUSEMOVE)
  319. {
  320. Set_StatubarText(statu,"求e的幂");
  321. goto EC;
  322. }
  323. case ID_PERCENT:
  324. if (MessageType==WE_MOUSEMOVE)
  325. {
  326. Set_StatubarText(statu,"求百分点");
  327. goto EC;
  328. }
  329. EC:if (MessageType== WE_CLICK &&enable)
  330. {
  331. singlecal(ObjectID);
  332. }
  333. break;
  334. case ID_EQUAL:
  335. if (MessageType == WE_CLICK &&enable)
  336. {
  337. calculate();
  338. }
  339. break;
  340. case ID_CLEAR:
  341. if (MessageType==WE_MOUSEMOVE)
  342. {
  343. Set_StatubarText(statu,"清除显示屏");
  344. }
  345. if (MessageType == WE_CLICK && enable)
  346. {
  347. operand1 = 0;
  348. res = 0;
  349. operand2 = 0;
  350. Change_TextboxText(result,"0");
  351. opdready = false;
  352. }
  353. break;
  354. case ID_ONOFF:
  355. if (MessageType==WE_MOUSEMOVE)
  356. {
  357. Set_StatubarText(statu,"计算器开关");
  358. }
  359. if (MessageType == WE_CLICK)
  360. {
  361. enable = !enable;
  362. enable? Change_TextboxText(result,"0"):
  363. Change_TextboxText(result,"");
  364. operand1 = operand2 = res = 0;
  365. opdready = false;
  366. }
  367. }
  368. }
  369. calculate()
  370. {
  371. char temp[128];
  372. if (resready)
  373. {
  374. return;
  375. }
  376. if (!opdready || !opdready2)
  377. {
  378. return;
  379. }
  380. switch ( operate)
  381. {
  382. case 1:
  383. res = operand1+operand2;
  384. break;
  385. case 2:
  386. res = operand1-operand2;
  387. break;
  388. case 3:
  389. res = operand1*operand2;
  390. break;
  391. case 4:
  392. if (operand2!=0)
  393. res= operand1/operand2;
  394. else
  395. {
  396. Set_StatubarText(statu,"除法错误:除数为0");
  397. Messagebox("计算器","除法错误:除数为0",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
  398. }
  399. break;
  400. }
  401. sprintf(temp,"%lf",res);
  402. Change_TextboxText(result,temp);
  403. opdready = false;
  404. opdready2 = false;
  405. resready = true;
  406. operand2 = 0;
  407. operate = 0;
  408. }
  409. gennum(wndhnd handle)
  410. {
  411. char numstring[64]="";
  412. double temp;
  413. if (handle != sign)
  414. {
  415. if (resready == true)
  416. {
  417. resready = false;
  418. }
  419. if ( !strcmp(Get_TextboxText(TEXTBOX(result)),"0"))
  420. {
  421. if (handle == digit[0])
  422. return;
  423. else
  424. {
  425. if (handle!=dot) Change_TextboxText(result,"");
  426. }
  427. }
  428. strcpy(numstring,Get_ButtonCaption(BUTTON(handle)));
  429. strcpy(numstring, trimstring(numstring));
  430. Appendtext_Textbox(result,numstring);
  431. sscanf(Get_TextboxText(TEXTBOX(result)),"%lf",opdready? &operand2:&operand1);
  432. if (opdready==true)
  433. {
  434. opdready2 = true;
  435. }
  436. }
  437. else
  438. {
  439. if (resready == false)
  440. {
  441. opdready?  (operand2 = -1*operand2) : (operand1 = -1*operand1);
  442. sprintf(numstring,"%lf",opdready? operand2:operand1);
  443. Change_TextboxText(result,numstring);
  444. }
  445. }
  446. }
  447. char *trimstring(char *s)
  448. {
  449. char b[128]="" ;
  450. int i = 0;
  451. int j = 0;
  452. for( ;*(s+i); i++)
  453. {
  454. if (s[i] !=' ')
  455. {
  456. b[j++] = s[i];
  457. }
  458. }
  459. return b;
  460. }
  461. singlecal(int ID)
  462. {
  463. double tmp;
  464. sscanf(Get_TextboxText(TEXTBOX(result)),"%lf",&tmp);
  465. switch(ID)
  466. {
  467. case ID_SIN:
  468. tmp = sin(tmp);
  469. break;
  470. case ID_COS:
  471. tmp = cos(tmp);
  472. break;
  473. case ID_TAN:
  474. tmp = tan(tmp);
  475. break;
  476. case ID_LOG:
  477. if (tmp>0)
  478. tmp = log10(tmp);
  479. else
  480. {
  481. Set_StatubarText(statu,"计算对数错误:底数非正数");
  482. Messagebox("计算器","计算对数错误:底数非正数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
  483. }
  484. break;
  485. case ID_LN:
  486. if (tmp>0)
  487. tmp = log(tmp);
  488. else{
  489. Set_StatubarText(statu,"计算对数错误:底数非正数");
  490. Messagebox("计算器","计算对数错误:底数非正数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
  491. }
  492. break;
  493. case ID_POWER:
  494. tmp = pow(tmp,2);
  495. break;
  496. case ID_SQRT:
  497. if (tmp>=0)
  498. tmp = sqrt(tmp);
  499. else
  500. {
  501. Set_StatubarText(statu,"开方错误:被开方数为负数");
  502. Messagebox("计算器","错误:被开方数为负数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
  503. }
  504. break;
  505. case ID_REV:
  506. if (tmp!=0)
  507. tmp = 1/tmp;
  508. else
  509. {
  510. Set_StatubarText(statu,"除法错误:除数为0");
  511. Messagebox("计算器","除法错误:除数为0",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
  512. }
  513. break;
  514. case ID_PERCENT:
  515. tmp = tmp*100;
  516. break;
  517. case ID_EXP:
  518. tmp =exp(tmp);
  519. break;
  520. }
  521. if (resready == true)
  522. {
  523. res = tmp;
  524. }
  525. else
  526. {
  527. if (opdready == false)
  528. {
  529. operand1= tmp;
  530. opdready = true;
  531. }
  532. else if (opdready2==true)
  533. {
  534. operand2 = tmp;
  535. }
  536. }
  537. Formats_Textbox(result,"%lf",tmp);
  538. }