Code:
#include"calc.h"
void main(void)
{
init(); //¼üÅ̶¨Ê±É¨Ãè³õʼ»¯
status=0;
display(0);
//getkey();
while(1)
{
Refresh(ledbuf);
}
}
void display(signed long x)
{
signed long data t;
unsigned long data tc;
int data i,p,tp;
int fst=0;
for(i=0;i<MAX_DISPLAY_CHAR;i++)
{
ledbuf[i]=0xff;
}
t=x;
if(x>=100000000||x<=-10000000){return;}//ÕûÊýÒç³ö
else if(x>=10000000||x<=-1000000){i=8;}
else if(x>=1000000||x<=-100000){i=7;}
else if(x>=100000||x<=-10000){i=6;}
else if(x>=10000||x<=-1000){i=5;}
else if(x>=1000||x<=-100){i=4;}
else if(x>=100||x<=-10){i=3;}
else if(x>=10||x<0){i=2;}
else{i=1;}
if(x<0)
{
ledbuf[--i]=0x2d;//¸ººÅ´¦Àí
t*=-1;
}
for(p=i-1;p>=0;p--)
{
tc=1;
for(tp=p;tp>0;tp--) tc*=10;
ledbuf[p]=t/tc;
t=t-(ledbuf[p]*tc);
}
for(i=MAX_DISPLAY_CHAR-1;i>=0;i--)
{
if(fst!=0||ledbuf[i]!=0||i==0)
{
ledbuf[i]=value2led(ledbuf[i]);
fst=1;
}
else
{
ledbuf[i]=0xff;
}
}
return;
}
char value2led(char ch)
{
switch(ch)
{
case 0x00: return 0xc0;
case 0x01: return 0xf9;
case 0x02: return 0xa4;
case 0x03: return 0xb0;
case 0x04: return 0x99;
case 0x05: return 0x92;
case 0x06: return 0x82;
case 0x07: return 0xf8;
case 0x08: return 0x80;
case 0x09: return 0x90;
case 0x2d: return 0xbf;
}
}
void getkey (void) interrupt 1 using 0
//¶¨Ê±É¨Ãè¼üÅÌ
{
char data mykey;
mykey = input();
if(mykey==0)
{
return;
}
else
{
calc(mykey);
}
return;
}
void calc(char cur)
{
if(cur=='0')cur=0;
if(cur>=0x00&&cur<=0x09)
digits(cur);
else
operator(cur);
return;
}
void digits(char dt)
{
if(status==0)
{
if(lvalue>=10000000) return;
lvalue*=10;
lvalue+=dt;
display(lvalue);
}
else if(status==1)
{
if(rvalue>=10000000) return;
rvalue*=10;
rvalue+=dt;
display(rvalue);
}
return;
}
void operator(char op)
{
status++;
switch(op)
{
case '+':
case '-':
case '*':
case '/':
if(status==1||status==0)
{
lasttoken=op;
}
else if(status==2)
{
deal();
}
status=1;
break;
case '=':
if(lasttoken==0) return;
deal();
status=0;
break;
case 'C':
lasttoken=op;
deal();
status=0;
break;
case 'S':
lasttoken=op;
deal();
status=0;
}
return;
}
void deal()
{
long result=0;
long num;
switch(lasttoken)
{
case '+':
result=lvalue+rvalue;
break;
case '-':
result=lvalue-rvalue;
break;
case '*':
result=lvalue*rvalue;
break;
case '/':
if(rvalue==0) return;
result=lvalue/rvalue;
break;
case 'C':
lasttoken=0;
break;
case 'S':
if(status==2&&rvalue!=0) num=rvalue;
else num=lvalue;
result=square((double)num);
lasttoken=0;
break;
}
if(result>=100000000)result=0;
lvalue=result;
display(lvalue);
rvalue=0;
return;
}
long absc(long dt)
{
if(dt>=0) return dt;
else return dt*(-1);
}
int square(double dt)
{
double result;
result=dt/2;
while (absc(result*result-dt)>1)
result=(result+dt/result)/2;
return result;
}
( Full Code C + Protues)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Đăng nhận xét