#include <sgwindow.h> /*SGWINDOW 程序必须的头文件*/
#include <math.h>
#define ID_ABOUT 10231
#define ID_ZERO 10000
#define ID_ONE 10001
#define ID_TWO 10002
#define ID_THREE 10003
#define ID_FOUR 10004
#define ID_FIVE 10005
#define ID_SIX 10006
#define ID_SEVEN 10007
#define ID_EIGHT 10008
#define ID_NINE 10009
#define ID_DOT 10010
#define ID_SIGN 10011
#define ID_ADD 20001
#define ID_MINUS 20002
#define ID_MUTIPLE 20003
#define ID_DIVIDE 20004
#define ID_EQUAL 20006
#define ID_SIN 20010
#define ID_COS 20011
#define ID_TAN 20012
#define ID_LOG 20013
#define ID_LN 20014
#define ID_EXP 20015
#define ID_POWER 20016
#define ID_SQRT 20017
#define ID_REV 20018
#define ID_PERCENT 20019
#define ID_ONOFF 30001
#define ID_CLEAR 30002
#define ID_RESULT 32000
#define ID_STATU 31000
#define ID_FRAME 31110
#define VERSIONSTRING "Calculate 1.0 For SGWINDOW 2.0 \n Copyright (C) 2005-8 SGPRO\n\n江西师范大学计算机02软件 殷圣鸽"
#define XINTERVAL 40
#define YINTERVAL 30
void Dispatch_Message(WinMessage WinMsg); /*消息处理函数定义*/
gennum(wndhnd);
char *trim(char*);
wndhnd window; /*主窗体句柄*/
wndhnd about; /* '关于' 按扭句柄*/
wndhnd digit[10],dot,sign;
wndhnd normaloperate[4];
wndhnd onoff,clear,equal;
wndhnd result;
wndhnd extraoperate[15];
wndhnd statu;
wndhnd fra;
wndhnd tip;
double operand1 = 0,operand2 = 0,res = 0;
bool opdready = false, opdready2 = false,resready =false;
bool enable = true;
int operate =0;
char *captions[] =
{
" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 ",
" . ","-/+",
" + "," - "," * "," / "," = ",
" ON "," CE ",
"sin","cos","tan","log"," ln ","exp","pow","sqr","1/x"," % "
};
void main()
{
WinMessage msg; /*主消息*/
winfont wft; /*字体*/
int dcnt = 0;
SGWINDOWinit(); /*加载SGWINDOW*/
changewincircletime(0);
setfont(&wft,1,4,WIN12,0); /*设置字体*/
window = new_form(100,100,300,290,1,wft,6,"计算器 1.0 For SGWINDOW 2.0","exe.bmp",true,true,true,40); /*设置窗体属性*/
result = new_Textbox(Get_FormUserarealeft(FORM(window))+6,
Get_FormUserareatop(FORM(window))+10,
280,18,15,wft,"0",50,false,true,true,true);
fra = new_frame(Get_FormUserarealeft(FORM(window))+6,
Get_FormUserareatop(FORM(window))+40,
220,190,wft,"",true);
for ( dcnt = 0; dcnt < 12; dcnt++)
{
if ( dcnt < 10 )
{
digit[dcnt] = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+(dcnt%3)*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL, captions[dcnt]);
}
else if (dcnt == 10 )
{
dot = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+(dcnt%3)*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL, captions[dcnt]);
}
else if (dcnt == 11 )
{
sign = new_Defaultbutton(Get_FormUserarealeft(FORM(window)) +
20+(dcnt%3)*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+(dcnt/3)*YINTERVAL,
captions[dcnt]);
}
}
for ( dcnt = 0; dcnt < 4; dcnt++ )
{
normaloperate[dcnt] = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+3*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+dcnt*YINTERVAL, captions[dcnt+12]);
}
for (dcnt = 0; dcnt < 10; dcnt++)
{
extraoperate[dcnt] =
new_Defaultbutton(Get_FormUserarealeft(FORM(window))+
20+(dcnt%5)*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+(4+dcnt/5)*YINTERVAL,
captions[dcnt+19]);
}
statu = new_Defaultstatubar(Get_FormUserarealeft(FORM(window))+2,
Get_FormUserareatop(FORM(window))+50+6*YINTERVAL+10,290,"就绪");
onoff = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
Get_FormUserareatop(FORM(window))+50, captions[17]);
clear = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+YINTERVAL, captions[18]);
equal = new_Defaultbutton(Get_FormUserarealeft(FORM(window))+20+4*XINTERVAL,
Get_FormUserareatop(FORM(window))+50+2*YINTERVAL, captions[16]);
about = new_Defaultbutton(340,325," 关于 ");
Register_Windowhandletoform(window,about,SGWNDBUTTON,ID_ABOUT);/*在主窗体中 注册 '关于' 按扭句柄 */
Register_Windowhandletoform(window,result,SGWNDTEXTBOX,ID_RESULT);
Register_Windowhandletoform(window,fra,SGWNDFRAME,ID_FRAME);
for ( dcnt = 0; dcnt < 10; dcnt++)
{
Register_Windowhandletoform(window,digit[dcnt],SGWNDBUTTON,ID_ZERO+dcnt);
}
for ( dcnt = 0; dcnt < 4; dcnt++)
{
Register_Windowhandletoform(window,normaloperate[dcnt],SGWNDBUTTON,ID_ADD+dcnt);
}
for (dcnt = 0; dcnt < 10; dcnt++)
{
Register_Windowhandletoform(window,extraoperate[dcnt],
SGWNDBUTTON,ID_SIN+dcnt);
}
Register_Windowhandletoform(window,dot,SGWNDBUTTON,ID_DOT);
Register_Windowhandletoform(window,sign,SGWNDBUTTON,ID_SIGN);
Register_Windowhandletoform(window,onoff,SGWNDBUTTON,ID_ONOFF);
Register_Windowhandletoform(window,clear,SGWNDBUTTON,ID_CLEAR);
Register_Windowhandletoform(window,equal,SGWNDBUTTON,ID_EQUAL);
Register_Windowhandletoform(window,statu,SGWNDSTATUBAR,ID_STATU);
Change_ButtonSize(equal,Get_ButtonSizewidth(BUTTON(equal)),
Get_ButtonSizeheight(BUTTON(equal)) +YINTERVAL);
while (TestMessageSource(window))/*当消息源存在时,进入消息循环*/
{
msg = WindowSend_Message(GetMessageSource(window));/*提取并发送消息*/
Dispatch_Message(msg); /*处理消息*/
}
}
void Dispatch_Message(WinMessage msg) /*消息处理函数*/
{
int ObjectID = Get_MessagescrID(msg); /*获取发送消息的对象的ID号*/
int MessageType = Get_MessagescrType(msg); /*获取该消息的类型*/
switch (ObjectID)
{
case ID_FORMCLOSE:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"关闭程序 ALT+F4");
}
break;
case ID_MAINFORM:/*主窗体对象*/
if (MessageType == WE_EXIT)/*主窗体发送退出消息*/
{
delete_form(&FORMPTR(window));/* 销毁主窗体*/
}
else if (MessageType == WE_FORMLOAD)/*主窗体发送加载消息*/
{
Show_Form(window);/*显示主窗体*/
}
else if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"就绪");
}
break;
case ID_STATU:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算器状态与帮助信息");
}
break;
case ID_FRAME:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"就绪");
}
break;
case ID_RESULT:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"结果显示屏, 数字超长时将分页显示");
}
break;
case ID_ABOUT:/* '关于'按扭对象*/
if (MessageType == WE_CLICK) /*关于按扭发送鼠标单击消息*/
{
Messagebox("Calculate For SGWINDOW",VERSIONSTRING,
SGMSGBOX_OKONLY,SGMSGBOXICON_information);/*缺省形式的消息框,显示当前SGWINDOW的版本字符串*/
}
else if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"关于本软件...");
}
break;
case ID_ZERO:
case ID_ONE:
case ID_TWO:
case ID_THREE:
case ID_FOUR:
case ID_FIVE:
case ID_SIX:
case ID_SEVEN:
case ID_EIGHT:
case ID_NINE:
case ID_DOT:
case ID_SIGN:
if (MessageType==WE_CLICK && enable)
{
if ((opdready == true && opdready2 == false) ||
resready == true)
{
if (ObjectID!=ID_SIGN)
Change_TextboxText(result,"");
}
gennum(Get_WindowItemhandle(*GetMessageSource(window),ObjectID));
}
break;
case ID_ADD:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"加法运算");
goto NC;
}
case ID_MINUS:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"减法运算");
goto NC;
}
case ID_MUTIPLE:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"乘法运算");
goto NC;
}
case ID_DIVIDE:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"除法运算");
goto NC;
}
NC:if (MessageType == WE_CLICK && enable)
{
if ( opdready && opdready2)
{
calculate();
}
if (resready)
{
operand1 = res;
res = 0;
resready = false;
}
operate = ObjectID -20000;
opdready = true;
}
break;
case ID_SIN:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算弧度正弦");
goto EC;
}
case ID_COS:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算弧度余弦");
goto EC;
}
case ID_TAN:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算弧度正切");
goto EC;
}
case ID_LOG:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算10为底的对数");
goto EC;
}
case ID_LN:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算e为底的对数");
goto EC;
}
case ID_POWER:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"求平方");
goto EC;
}
case ID_SQRT:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"求开方");
goto EC;
}
case ID_REV:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"求倒数");
goto EC;
}
case ID_EXP:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"求e的幂");
goto EC;
}
case ID_PERCENT:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"求百分点");
goto EC;
}
EC:if (MessageType== WE_CLICK &&enable)
{
singlecal(ObjectID);
}
break;
case ID_EQUAL:
if (MessageType == WE_CLICK &&enable)
{
calculate();
}
break;
case ID_CLEAR:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"清除显示屏");
}
if (MessageType == WE_CLICK && enable)
{
operand1 = 0;
res = 0;
operand2 = 0;
Change_TextboxText(result,"0");
opdready = false;
}
break;
case ID_ONOFF:
if (MessageType==WE_MOUSEMOVE)
{
Set_StatubarText(statu,"计算器开关");
}
if (MessageType == WE_CLICK)
{
enable = !enable;
enable? Change_TextboxText(result,"0"):
Change_TextboxText(result,"");
operand1 = operand2 = res = 0;
opdready = false;
}
}
}
calculate()
{
char temp[128];
if (resready)
{
return;
}
if (!opdready || !opdready2)
{
return;
}
switch ( operate)
{
case 1:
res = operand1+operand2;
break;
case 2:
res = operand1-operand2;
break;
case 3:
res = operand1*operand2;
break;
case 4:
if (operand2!=0)
res= operand1/operand2;
else
{
Set_StatubarText(statu,"除法错误:除数为0");
Messagebox("计算器","除法错误:除数为0",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
}
break;
}
sprintf(temp,"%lf",res);
Change_TextboxText(result,temp);
opdready = false;
opdready2 = false;
resready = true;
operand2 = 0;
operate = 0;
}
gennum(wndhnd handle)
{
char numstring[64]="";
double temp;
if (handle != sign)
{
if (resready == true)
{
resready = false;
}
if ( !strcmp(Get_TextboxText(TEXTBOX(result)),"0"))
{
if (handle == digit[0])
return;
else
{
if (handle!=dot) Change_TextboxText(result,"");
}
}
strcpy(numstring,Get_ButtonCaption(BUTTON(handle)));
strcpy(numstring, trimstring(numstring));
Appendtext_Textbox(result,numstring);
sscanf(Get_TextboxText(TEXTBOX(result)),"%lf",opdready? &operand2:&operand1);
if (opdready==true)
{
opdready2 = true;
}
}
else
{
if (resready == false)
{
opdready? (operand2 = -1*operand2) : (operand1 = -1*operand1);
sprintf(numstring,"%lf",opdready? operand2:operand1);
Change_TextboxText(result,numstring);
}
}
}
char *trimstring(char *s)
{
char b[128]="" ;
int i = 0;
int j = 0;
for( ;*(s+i); i++)
{
if (s[i] !=' ')
{
b[j++] = s[i];
}
}
return b;
}
singlecal(int ID)
{
double tmp;
sscanf(Get_TextboxText(TEXTBOX(result)),"%lf",&tmp);
switch(ID)
{
case ID_SIN:
tmp = sin(tmp);
break;
case ID_COS:
tmp = cos(tmp);
break;
case ID_TAN:
tmp = tan(tmp);
break;
case ID_LOG:
if (tmp>0)
tmp = log10(tmp);
else
{
Set_StatubarText(statu,"计算对数错误:底数非正数");
Messagebox("计算器","计算对数错误:底数非正数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
}
break;
case ID_LN:
if (tmp>0)
tmp = log(tmp);
else{
Set_StatubarText(statu,"计算对数错误:底数非正数");
Messagebox("计算器","计算对数错误:底数非正数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
}
break;
case ID_POWER:
tmp = pow(tmp,2);
break;
case ID_SQRT:
if (tmp>=0)
tmp = sqrt(tmp);
else
{
Set_StatubarText(statu,"开方错误:被开方数为负数");
Messagebox("计算器","错误:被开方数为负数",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
}
break;
case ID_REV:
if (tmp!=0)
tmp = 1/tmp;
else
{
Set_StatubarText(statu,"除法错误:除数为0");
Messagebox("计算器","除法错误:除数为0",SGMSGBOX_OKONLY,SGMSGBOXICON_critical);
}
break;
case ID_PERCENT:
tmp = tmp*100;
break;
case ID_EXP:
tmp =exp(tmp);
break;
}
if (resready == true)
{
res = tmp;
}
else
{
if (opdready == false)
{
operand1= tmp;
opdready = true;
}
else if (opdready2==true)
{
operand2 = tmp;
}
}
Formats_Textbox(result,"%lf",tmp);
}
评论