查看: 1358|回复: 6
|
如何让我的calculator 有decimal
[复制链接]
|
|
我是使用mikroc compiler。以下的program都ok,但有decimal的答案是不正确的。各位大大可以帮找出问题吗?
file:///C:/DOCUME%7E1/teikhong/LOCALS%7E1/Temp/moz-screenshot.jpg//********************************************************
//define pins
#define one PORTA.F0
#define two PORTA.F1
#define three PORTA.F2
#define four PORTA.F3
#define five PORTA.F5
#define six PORTE.F0
#define seven PORTE.F1
#define eight PORTE.F2
#define nine PORTC.F0
#define zero PORTC.F1
;****************************************************************
//define variables
char txt[12];
char in1[12];
char in2[12];
char i;
char x;
long result;
double result1;
unsigned long input1,input2;
char mode;
unsigned short kp;
//**************************************************************
//define prototype
char get_num(void);
char read_key(void);
//main routine
//setup I/O port
//setup LCD pins
void main()
{
ADCON1 = 0x06; // Configure analog inputs and Vref
TRISA = 0b11111111; // PORTA is input
TRISB = 0b11110000; //
TRISC = 0b11111111;
TRISD = 0b00000000;
TRISE = 0b00000111;
OPTION_REG = 0b00000001;
LCD_Config(&PORTD,4,5,6,3,2,1,0);
Lcd_Init(&PORTD); //
LCD_Cmd(LCD_CURSOR_OFF); // send command to LCD (cursor off)
LCD_Cmd(LCD_CLEAR); // send command to LCD (clear LCD)
Lcd_Out(1,1, " Smart Calulator");
Lcd_Out(2,1, " System ");
Delay_ms(2000);
LCD_Cmd(LCD_CLEAR);
//*******************************************************************888
//get key & save in in1 array
do
{
for(i=0;i<=12;i++)
in1=0;
i=0;
while(1)
{
x=read_key(); //get key press
if(i<=8)
{
in1=x;
if(i==0)
Lcd_chr(1,1,in1); //display key at lcd
else
lcd_chr_cp(in1);
i++;
}
if(x=='*' || x=='/' || x=='+' || x=='-') //if key found then break
{
mode=x;
LCD_Cmd(LCD_CLEAR);
Lcd_chr(1,12,x);
break;
}
}
input1 = atol(in1); //convert string to long & save in input1
Delay_ms(10);
for(i=0;i<=12;i++)
in2=0;
i=0;
while(1)
{
x=read_key(); //get key press
if(i<=8)
{
in1=x;
if(i==0)
Lcd_chr(1,1,in1); //display key at lcd
else
lcd_chr_cp(in1);
i++;
}
if(x=='=') //if key found then break
{
LCD_Cmd(LCD_CLEAR);
break;
}
}
input2 = atol(in1); //convert string to long & save in input2
LCD_Cmd(LCD_CLEAR); //clear all arrays
lcd_out(1,1,"Result:");
for(i=0;i<=12;i++)
txt=0;
result=0;
if(mode=='+') //if + key
{ //do calulation
result= input1+input2;
longtostr(result,txt); //convert result to string
lcd_chr(2,1,'=');
for(i=0;i<=11;i++)
{
if(txt==' ')
continue;
if (isdigit(txt) || txt=='-') //display result in LCD
lcd_chr_cp(txt);
}
Delay_ms(5000);
LCD_Cmd(LCD_CLEAR);
}
else if(mode=='-') //if - key
{
result= input1-input2; //do calulation
longtostr(result,txt); //convert result to string
lcd_chr(2,1,'=');
for(i=0;i<=11;i++)
{
if(txt==' ')
continue;
if (isdigit(txt) || txt=='-') //display result in LCD
lcd_chr_cp(txt);
}
Delay_ms(5000);
LCD_Cmd(LCD_CLEAR);
}
else if(mode=='*') //if * key
{
result= input1*input2; //do calulation
longtostr(result,txt); //convert result to string
lcd_chr(2,1,'=');
for(i=0;i<=11;i++)
{
if(txt==' ' || txt=='-') //display result in LCD
continue;
if (isdigit(txt))
lcd_chr_cp(txt);
}
Delay_ms(5000);
LCD_Cmd(LCD_CLEAR);
}
else if(mode=='/') //if / key
{
result1= input1/input2; //do calulation
floattostr(result,txt); //convert result to string
lcd_chr(2,1,'=');
for(i=0;i<=11;i++)
{
if(txt==' ')
continue;
if (isdigit(txt)|| txt=='-' ||txt=='.') //display result in LCD
lcd_chr_cp(txt);
}
Delay_ms(5000);
LCD_Cmd(LCD_CLEAR);
}
}while(1);
}
//********************************************************************
//check PORTC pins
//if math return value save in num
char get_num(void)
{
char num;
while(1)
{
if (PORTC==0b00000001)
{
num='1';
break;
}
else if (PORTC==0b00000010)
{
num='2';
break;
}
else if (PORTC==0b00000100)
{
num='3';
break;
}
else if (PORTC==0b00001000)
{
num='4';
break;
}
else if (PORTC==0b00010000)
{
num='5';
break;
}
else if (PORTC==0b00100000)
{
num='6';
break;
}
else if (PORTC==0b01000000)
{
num='7';
break;
}
else if (PORTC==0b10000000)
{
num='8';
break;
}
else if (PORTA==0b00000001)
{
num='0';
break;
}
else if (PORTA==0b00000010)
{
num='+';
break;
}
else if (PORTA==0b00000100)
{
num='-';
break;
}
else if (PORTA==0b00001000)
{
num='*';
break;
}
else if (PORTA==0b00010000)
{
num='/';
break;
}
}
return num;
}
//*****************************************************************************
//scan keypad
//scan row 1,2,3,4
//detect column
//loop until key found
char read_key(void){
unsigned short row,col;
char key;
row = 1;
do{
key = 0;
if (row==1){
PORTB = 0b00001110;
col = PORTB;
switch(col){
case 0xE0: key = '1'; break;
case 0xD0: key = '2'; break;
case 0xB0: key = '3'; break;
case 0x70: key = '+'; break;
}
row =2 ;
}
else if (row ==2){
PORTB = 0b00001101;
col = PORTB;
switch(col){
case 0xE0: key = '4'; break;
case 0xD0: key = '5'; break;
case 0xB0: key = '6'; break;
case 0x70: key = '-'; break;
}
row = 3;
}
else if (row==3){
PORTB = 0b00001011;
col = PORTB;
switch(col){
case 0xE0: key = '7'; break;
case 0xD0: key = '8'; break;
case 0xB0: key = '9'; break;
case 0x70: key = '*'; break;
}
row = 4;
}
else if (row==4){
PORTB = 0b00000111;
col = PORTB;
switch(col){
case 0xE0: key = '*'; break;
case 0xD0: key = '0'; break;
case 0xB0: key = '='; break;
case 0x70: key = '/'; break;
}
row = 1;
}
if (one)
{
key='1';
while(one);
Delay_ms(100);
}
else if (two)
{
key='2';
while(two);
Delay_ms(100);
}
else if (three)
{
key='3';
while(three);
Delay_ms(100);
}
else if (four)
{
key='4';
while(four);
Delay_ms(100);
}
else if (five)
{
key='5';
while(five);
Delay_ms(100);
}
else if (six)
{
key='6';
while(six);
Delay_ms(100);
}
else if (seven)
{
key='7';
while(seven);
Delay_ms(100);
}
else if (eight)
{
key='8';
while(eight);
Delay_ms(100);
}
else if (nine)
{
key='9';
while(nine);
Delay_ms(100);
}
else if (zero)
{
key='0';
while(zero);
Delay_ms(100);
}
}while(key);
do{
col = PORTB;
col &= 0b11110000;
delay_ms(10);
} while(col != 0b11110000) ;
return key;
} |
|
|
|
|
|
|
|
发表于 3-4-2009 10:04 PM
|
显示全部楼层
原帖由 ooi99 于 3-4-2009 09:46 PM 发表
我是使用mikroc compiler。以下的program都ok,但有decimal的答案是不正确的。各位大大可以帮找出问题吗?
code是从哪里来的, 就去哪里请教原作者吧。。。 |
|
|
|
|
|
|
|
发表于 3-4-2009 11:30 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 4-4-2009 01:09 AM
|
显示全部楼层
code是楼主自己写的吗?
如果不是, 还请搂主先了解code,
然后再来发问。 |
|
|
|
|
|
|
|
楼主 |
发表于 5-4-2009 07:51 AM
|
显示全部楼层
如何让division有小数点
我的calculator只要让两个整数+,-,*,/. 其他是没有问题,但除法不能show小数点。input 1 和2是用keypad打的。keypad 上没有小数点的。我改了program如下,但答案是错的。
result1= input1/input2; //do calulation
floattostr(result1,txt); //convert result to string
lcd_chr(2,1,'=');
input1 和 input2 是 int.如何让input2变成double?大大有解决方法吗? |
|
|
|
|
|
|
|
发表于 5-4-2009 08:48 AM
|
显示全部楼层
原帖由 ooi99 于 5-4-2009 07:51 AM 发表
我的calculator只要让两个整数+,-,*,/. 其他是没有问题,但除法不能show小数点。input 1 和2是用keypad打的。keypad 上没有小数点的。我改了program如下,但答案是错的。
result1= input1/input2; //do calu ...
请楼主不要发一样问题的新帖, 我已经把你新发的帖合并。
你上面发了一堆MicroC 的sample code, 然后你要人花时间帮你看帮你改, 是不可能的事, 没有人会这样帮你改。。所以没有人回复你, 你应该要知道问题所在。
input1 和 input2 是 int.如何让input2变成double?大大有解决方法吗?
double input1,input2;
这样不可以吗?
请各位FYP同学, 在发帖前也尽量的先爬帖
参考:用PIC做scientific calculator
请问楼主是哪里的学校? 如果得不到精确答案, 我不会继续讨论。 |
|
|
|
|
|
|
|
发表于 5-4-2009 08:54 AM
|
显示全部楼层
integer
1 =0.......01
2= 0.......10
1.2=0........01
10/2.5 = 4
如果是integer, 那么它会收到 10/2 =5 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|