【KEIL编写计算器程序】,老是出错,求帮助!【新手】
#include<reg51.h>#include<math.h>
#define unit unsigned int
#define unchar unsigned char
#define unlong unsigned long
#define ZERO 0.0001
sbit rs=P2^0;
sbit en=P2^1;
sbit q1=P2^2;
sbit sqr=P2^3;
sbit xy=P2^4;
sbit fg=P2^5;
sbit hs=P2^6;
sbit rw=P2^7;
unchar flag,s,ws,ws1,xs,eq;//ws1,ws 表示等于键是否按下
int cur=0;
enum STATE{operand,sign};
double sum,aa;
long double out,out1,k;
signed long hh,11;
void delay(uint z) //延时程序 1ms
{
unit x,y;
for(x=0;x<z;x++);
for(y=0;y<110;y++);
}
void write_com(unchar com) //写指令进入LCD模块
{
rw=0;
rs=0;
P1=com;
delay(5);
en=0;
}
void write_data(uchar date) //写数据进入LCD模块
{
rw=0;
rs=1;
P1=date;
delay(5);
en=1;
delay(5);
en=0;
cur++;
}
void equalfun(double b) //等于计算最后的结果
{
uchar p,f,bz=0;//bz 表示从末尾开始,当找到第一个不为零的书时,不走为1
uchar d; //d,当俩个数都没小数时为0,否则为1
write_com(0x0c); //写入一个字符 AC自动加一 左移
write_com(0x0c); //开显示和光标显示
if((ws==0)&&(ws1==0))
d=0;
else
d=1;
for(ws1;ws1>0;ws1--)
{
a=a/(double) 10;
}
for(ws;ws>0;ws--)
{
b=b/(double) 10;
}
if(flag==0)
{
out=b;
}
if(flag==1)
{
out=a+b;
}
else if(flag==2)
{
out=a-b;
if(out<0)
{k=out;out=0-out;}
}
else if(flag==3)
out=a*b;
else if(flag==4)
{
out=a/b;
}
else if(flag==5)
{
out=(double)sqrt(a);
}
else if(6==flag)
{
if(0==d)
{
unit i;
out =1.0;
for(i=0;i<b;i++)
{
out*=a;
}
}
else
out= pow(a,b); //计算a的b次方
}
else if(7==flag)
{
out=pow(a,(double)1/b);
}
hh=(signed long)out;
if(fabs(out-hh-1)<ZERO)
{
11=0;
hh+=1;
out+=1;
}
else if(fabs(out-hh)<ZERO)
11=0;
else
11=(signed long)((out-hh)10*10*10*10*10*10);
/*if(0==d)
{
if(fabs(out-hh-1)<ZERO)
{
11=0;
hh+=1;
out+=1;
}
else if(fabs(out-hh)<ZERO)
11=0;
else
11=(signeed long)((out-hh)*10*10*10*10*10*10);
}
else
11=((signed long)(out*10*10*10*10*10*10))%1000000;
*/
write_com(0x80+15); //DDRAM 光标地址设置(表示第一行的末尾)
if(11!=0)
{
for(p=6;P>0;P--)
{
f=11%10;
if(0!f|1==bz)//当遇到第一个部位0的数的时候,之前的0都不会被写入
{
write_data(0x30+f);
bz=1;
}
11=11/10;
//if(11==0)
//break; 此处如果加这句, 只会打印有效数字部分
}
write_data(0x20+0x0e);
}
fo(p=0;p<10;p++))
{
f=hh%10;
write_data(0x30+f);
hh=hh/10;
if(hh==0)
break;
}
if(k<0)
write_data('-');
write_com(0x06); //光标左移
}
void qingling()//清零
{
flag=0;
s=0;
sum=0;
aa=0;
out=0;
out1=0;
k=0;
ws1=0;
ws=0;
eq=0;
write_com(0x01);
write_com(0xc0);
write_com(0x0f);
}
void init() //初始化
{
flag=0;
write_com(0x38); //设置工作方式 数据总线为8为 字符行数为2 行
write_com(0x0f); //开显示 光标不显示 不闪烁
write_com(0x06); //写入的时候 AC自动减1,画面不移动 右移
write_com(0x01);//清屏
write_com(0xc0); //AC地址设置 从第2行开头开始
}
void scan() //扫描LCD 键盘
{
enum STATE state;
uchar temp;
P3=0xfe;
temp=P3;
temp=temp&0xfe;
if(temp!=0xfe)
{
delay(5)
temp=P3;
temp=temp&0xfe;
if(temp!=0xfe)
{
switch(temp)
{
case 0xee:if(1==eq){qingling();break; }
s++;
if(s<8)
{
write_data(0x30+1);
sum=sum*10+1;
if(1==xs)
ws++;
}
state=operand;
break;
case 0xde:if(1==eq){qingling();break;}
if(s<8)
write_data(0x30+5);
sum=sum*10+5;
if(1==xs)
ws++;
}
state=operand;
break;
case 0xbe:if(1==eq){qingling();break;}
s++;
if(s<8)
{
{
write_data(0x30+9);
sum=sum*10+9;
if(1==xs)
ws++;
}
}
state=operand;
break;
case 0x7e:if(1==eq){qingling() ;break;}
if(flag==0)
{
{aa=sum;sum=0;ws1=ws;ws=0;}
write_data('+');
s=0;
xs=0;
flag=1; //加函数
}
state=sign;
break;
}
while(temp!=0xfe)
{temp=P3;}
}
else if ((temp!=0xfe)&(1==eq))
qingling();
}
P3=0xfd;
temp=P3;
temp=temp&0xfd;
if(temp!=0xfd)
{
delay(5);
temp=P3;
temp=temp&0xfe;
if(temp!=0xfd)
{
switch(temp)
case 0xed:if(1==eq){qingling();break;}
s++;
if(s<8)
{
write_data(0x30+2);
sum=sum*10+2;
if(1==xs)
ws++;
}
state=operand;
break;
case 0xdd:if(1==eq){qingling();break;}
if(s<8)
write_data(0x30+6);
sum=sum*10+6;
if(1==xs)
ws++;
}
state=operand;
break;
case 0xbd:if(1==eq){qingling();break;}
s++;
if(s<8)
{
{
write_data(0x30+0);
sum=sum*10+0;
if(1==xs)
ws++;
}
}
state= operand;
break;
case 0x7d:if(1==eq){qingling();break;
if(flag==0)
{
{aa=sum;sum=0;ws1=ws;ws=0;}
write_data('-');
s=0;
xs=0;
flag=2; //减函数
}
state=sign;
break;
}
while(temp!=0xfd)
{temp=P3;}
}
else if((temp!=0xfe)&(1==eq))
qingling();
}
P3=0xfb;temp=P3;
temp=temp&0xfb;
if(temp!=0xfb)
{
delay(5);
temp=P3;
temp=temp&0xfb;
if(temp!=0xfb)
{
switch(temp)
{
case 0xeb:if(1==eq){qingling();break;}
s++;
if(s<8)
{write_data(0x30+3);
sum=sum*10+3;
if(1==xs)
ws++;
}
state=operand;
break;
case 0xdb:if(1==eq){qingling();break;}
s++;
if(s<8)
{
{
write_data(0x30+7);
sum=sum*10+7;
if91==(xs)
ws++;
}
}
state=operand;
break
case 0xbb:if(1=eq){qingling();break;}
if(xs==0)
{
write_data(0x20+0x0e);
xs=1; //小数点
s=0;
state=sperand;
}
break;
case 0x7b:if(1==eq){qingling();break;}
if(flag==0)
{
{aa=sum;sum=0;ws1=ws;ws=0;}
write_data('*');
s=0;
xs=0;
flag=3; //乘函数
}
state=sign;
break;
}
while(temp!=0xfb)
{temp=P3;}
}
else if((temp!=0xfb)&(1==eq))
qingling();
}
P3=0xf7;
temp=P3;
temp=temp&0xf7;
if(temp!=0xf7)
{
delay(5);
temp=P3;
temp=temp&0xf7;
if(temp!=0xf7)
{
switch(temp)
{
case 0xe7:if(1==eq){qingling();break;}
s++;
if(s<8)
{write_data(0x30+4);
sum=sum*10+4;
if(1==xx)
ws++;
}
state=operand;
break;
case 0xd7:if(1==eq){qingling();break;}
s++;
if(s<8)
{
{
write_data(0x30+8);
sum=sum*10+8;
if(1==xs)
ws++;
}
}
state=operand;
break;
case 0xb7:if(1==eq){qingling();break;}
equalfun(aa,sum); //等于函数
flag=0;
xs=0;
eq=1;
break;
case 0x77:if(1==eq){qingling();break;}
if(flag==0)
{
{aa=sum;sum=0;ws1=ws;ws=0;}
write_data('/');
s=0;
xs=0;
flag=4;// 除函数
state=operand;
}
break;
}
while(temp!=0xf7)
{temp=P3;}
}
else if((temp!=0xf7)&(1++eq))
qingling();
}
if(q1=0)
{
dela(5);
if(q1==0)
{qingling();
while(!q1);
}
}
if(sqr==0)
{
delay(5);
if(sqr==0)
while(!sqr);
\if(1==eq)
{
qingling();
return;
}
if(flag==0)
{
flag=5;
aa=sum;
sum=0;
ws1=ws;
equalfun(aa,sum);
flag=0;
xs=0;
eq=1;
}
}
}
if(xy==0)
{
delay(5);
if(xy==0)
{
while(!xy);
if(1==eq)
{
qingling();
return;
}
if(flag++0)
{write_data(0x5e);
aa=sum;
sum=0;
ws1=ws;
ws=0;
flag=6;
xs=0;
state=sign;
}
}
}
if(fg==0)
{
delay(5);
if(fg==0)
{
while(!fg);
if(1==eq)
{
qingling();
return;
}
if(flag==0)
{
write_data(0x20);
write_data(0x79);
write_data(0x72);
write_data(0x6f);
write_data(0x6f);
write_data(0x74);
write_data(0x20);
aa=sum;
sum=0;
ws1=ws;
ws=0;
flag=7;
xs=0;
state=sign;
}
}
}
if(hs==0&&cur>0)
{
delay(5);
if(hs==0)
{
while(!hs);
if(1==eq)
{
qingling();
return;
}
if(state==operand)
{
if(1==xs)
{
if(ws==0)
{xs=0;
}
else
{
sum=(int)(sum/10);
if(sum==0)
state=sign;
ws--;
}
}
else
{
sum=(int)(sum/10);
if(sum==0)
state=sign;
}
}
else
{
sum=aa;
ws=ws1;
if(0==ws)
xs=0;
else
xs=1;
flag=0;
state=operand;
}
write_com(0x10); //
cur--;
write_data(0x20); //写入空格
write_com(0x10);
cur--;
}
}
}
viod main() //主函数
{
init();
while(1)
{
scan();
}
}