本帖最后由 jinhai_lucky 于 2014-12-15 10:42 编辑
#include<intrins.h>
#include<reg51.h>
#include“calc.h”
static data LONG lvalue;
static data LONG rvalue;
static data CHAR currtoken;
static data CHAR lasttoken;
static data GHAR lastpress;
static xdata CHAR outputbuffer[MAX_DISPLAY_CHAR];
VOID main(VOID).
{lvalue =0;
rvalue =0;
currtoken = ‘=’;
lasttoken = ‘0’;
Initialise();
Calc_output(OK);
Calc_evaluate();
}
VOID calc_cvaluate()
{CHAR data key;
INT data i ;
CHAR xdata number[MAX_DISPLAY_CHAR];
CHAR xdata *bufferptr;
//清除冲区
For(i=0;i<=MAX_DISPLAY_CHAR; i++)
{ number = ‘’;
}
buffer = number;
for(;;)
{key = calc_getkey();
If(calc_testkey(key))
//输入数据正数或负数 并LCD上显示
{if (bufferptr !=&number[MAX_DISPLAY_CHAR-2])
{*bufferptr = key:
calc_display(number);
Bufferptr++;
}
}
else
{
//输入第一个数.
If(lasttoken == ‘0’)
{lvalue = calc_asciidec (number);}
else
{rvalue = calc_asciidec(number);}
Bufferptr = number;
for (i = 0;i<=MAX_DISPLAY_CHAR; i++)
{number = ‘’}
//处理输入数据r
currtoken = key;
If (currtoken == ‘C’)
{ calc_opfunctions(currtoken);}
Else
{calc_opfunctions(lasttoken);}
//输入第二个数
For (i= 0;i<= MAX_DISPLAY_CHAR;i++)
{ outputbuffer =number;
If (currtoken !=0×3D)lasttoken=currtoken;
}
Lastpress = key;
}
}
VOID calc_opfunctions (CHAR token)
{
CHAR data result;
Switch(token)
//加法
{ case ‘+’; if ((currtoken ==‘=’) if ((lastpress>=0×30)&&(lastpress<=0×39)))
{ lvalue +=rvalue;
result = calc_chkerror(lvalue);
}
Else
{ result= SLEEP;} break;
// 减法.
Case∵ :if ((currtoken ==‘=’)|| ((lastpress>=0×30)&&(lastpress<=0×39)))
{lvalue -=rvalue;
result = calc_chkerror(lvalue);
}
Else
{result = SLEEP;) break;
//乘法
Case‘*’: if ((currtoken == ‘=’) || (lastpress>= 0×30)&&(lastpress<=0×39)))
{lvalue * =rvalue;
Result =calc_chkerror(lvalue):
}
Else
{result = SLEEP;} break;
//除法.
Case ‘/’: if ((currtoken == ‘=’)|| (lastpress>= 0×30)&&(lastpress<=0×39)))
{ if (rvalue)
{lvalue /=rvalue:
Result =calc_chkerror(lvalue):
}
Else
{result = ERROR:}
}
Else
{result =SLEEP:} break:
//删除
Case‘C’: lvalue =0;
Rvalue = 0;
Currtoken =‘0’;
Result= ‘OK’ break;
Defult: result = SLEEP;
}
/**********************************************************Utility Routines
******************************************************/
INT calc_cheerror(LONG num)
// LED显示范围 -999999 —999999
{
If((num>=-999999)&&(num<=999999))
Return OK;
Else
return REEOR;
}
VOID calc_output (INT status)
{
Switch(status)
{ case OK :calc_display(calc_decascii(lvalue)): break;
case SLEEP : break;
case ERROR :calc_display(“Exception”); break;
defult :calc_display(“Exception”); break;
}
LONG calc_asciidec(CHAR *buffer) //ASCII 码转换
{
LONG data value;
LONG data digit;
value = 0;
While(*buffer!= ‘’)
{ digit =*buffer - 48;
value = value *10+digit;
buffer++;
}
return value;
}
CHAR*calc_decascii(LONG num )
{
LONG data temp =num;
CHAR xdata*arrayptr = &outputbuffer[MAX_DISPLAY_CHAR];
LONG data division = 10;
LONG data result;
CHAR data remainder,asciival;
INT data i;
If(!temp)
{ *arrayptr = 48;
Goto done;
}
If(temp<0) //负数处理
{
outputbuffer[0]= ‘-’;
temp -= 2*temp;
}
For (i=0;i< sizeof(outputbuffer); i++)
{ remainder =temp % divisior;
Result = temp/divisior;
If ((!remainder)&&(!result))
{*arrayptr = ‘’;}
Else
{ asciival = remainder + 48;
*arrayptr = asciival;
}
temp/=10;
If(arrayptr !=&outputbuffer[]arrayptr--;
}
Done: return outputbuffer;
}
CHAR calc_testkey(CHAR key)
{ if ((key>=0×30)&& (key<=0×39))
{return 1 :}
else
{ return 0 :}
}
/********************************************************** I/O routines
*************************************************/
CHAR calc_getkey(VOID)
// 键盘扫描
{CHAR data mykey;
do mykey =input();
while (mykey == 0);
return mykey;
}
VOID calc_display (CHAR but[MAX_DISPLAY_CHAR])
//LCD显示
{ INT data i = 0;
Clearscreen();
for(i;i<=MAX_DISPLAY_ CHAR; i++)
{ if (but != ‘’)
{ output(but);}
}
}
|