我需要一个问题,我使用PIC模拟器通过串口导入2个数字。首先我发送byte,例如5,然后发送发送2,在第三步我发送char例如*,结果是5 * 2 = 10。一切都很好,直到我发送超过255的更大数字。它说错误输入,我知道我只能发送255个字节。但是如何导入更大的数字呢?应该在代码中更改哪些内容适用于更大的数字。一些想法???????非常感谢
//================= konfigure LCD display
// port for data PORTB
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//=========================================================
//===========variables
char operation;
int nbr1=0,nbr2=0,result=0,rest=0;
char txt[16],br[7];
//=========================================================
//======== delete empty spaces
void empty_spaces(char array[]){
int j=0,i=0,n=0;
n=strlen(array);
while(i<n){
if(array[i]==' '){
j=i;
while(j<n){
array[j]=array[j+1];
++j;
}
--n;
}else
++i;
}
if(n>15)
n=15;
array[n]='\0';
}
//=========================================================
//========function back int from the imported char
int back_char(char operation ){
if(operation=='+')
return 1;
if(operation=='-')
return 2;
if(operation=='*')
return 3;
if(operation=='/')
return 4;
if(operation=='%')
return 5;
return 0;
}
//=========================================================
//============= init lcd display and serial port
void inicijalizacija(){
PORTB = 0xFF;
TRISB = 0x00;
ANSEL = 0x00;
ANSELH = 0x00;
C1ON_bit = 0;
C2ON_bit = 0;
UART1_Init(9600);
Delay_ms(100);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
}
//=========================================================
void main(){
inicijalizacija();
//============= enter first number
UART1_Write_Text("first num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr1=UART1_Read();
IntToStr(nbr1,br);
strcpy(txt,"Num1:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,1,txt);
Delay_ms(1);
//=============enter second number
UART1_Write_Text("second num:");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
nbr2=UART1_Read();
IntToStr(nbr2,br);
strcpy(txt,"Num2:");
strcat(txt,br);
empty_spaces(txt);
Lcd_Out(1,10,txt);
Delay_ms(1);
//==============================================================
//enter operation
UART1_Write_Text("operation(+,-,/,*,%):");
UART1_Write(10);
UART1_Write(13);
do{
}while(!UART1_Data_Ready());
operation=UART1_Read();
strcpy(txt,"oper:");
switch(back_char(operation)){
case 0: strcat(txt," ");break;
case 1:
strcat(txt,"+");
result=nbr1+nbr2;
break;
case 2:
strcat(txt,"-");
result=nbr1-nbr2;
break;
case 3:
strcat(txt,"*");
result=nbr1*nbr2;
if(nbr2!=result/nbr1)
operation=' ';
break;
case 4:
strcat(txt,"/");
if(nbr2==0)
operation=' ';
else{
result=nbr1/nbr2;
rest=nbr1%nbr2;
}
break;
case 5:
strcat(txt,"%");
if(nbr2==0)
operation=' ';
else
result=nbr1%nbr2;
break;
}
empty_spaces(txt);
Lcd_Out(2,1,txt);
Delay_ms(1);
//==============================================================
//============= Print result
if(back_char(operation)!=0){
IntToStr(result,br);
strcpy(txt,"Rez:");
strcat(txt,br);
empty_spaces(txt);
if(back_char(operation)!=4)
Lcd_Out(2,7,txt);
else{ // Dokolku vrednosta od funkcijata vrati_znak(operacija) e 4
IntToStr(rest,br); // se raboti za delenje
empty_spaces(br);
strcat(txt,"~");
strcat(txt,br);
Lcd_Out(2,5,txt);
}
}else
Lcd_Out(2,7,"error!");
Delay_ms(1);
//==============================================================
}
答案 0 :(得分:1)
您可以使用
这一事实(a + b) * c == (a * c) + (b * c)
将你的乘法分解成更小的块,但你仍然必须处理这样一个事实,即最终结果可能比你正在使用的任何类型都要大。