Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino Uno在计算时出错_C++_Arduino Uno - Fatal编程技术网

C++ Arduino Uno在计算时出错

C++ Arduino Uno在计算时出错,c++,arduino-uno,C++,Arduino Uno,所以我一直在和我的Arduino一起做一个计算器,我就是这么做的。尽管如此,它并没有像我预期的那样起作用。当我输入简单的计算时,它会很好地输出,但当我输入复杂的计算时,它会变得疯狂!它告诉我9999*9大约是-14554或者类似的数字。代码如下: #include <LiquidCrystal_I2C.h> #include <LCD.h> #include <Keypad.h> LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4,

所以我一直在和我的Arduino一起做一个计算器,我就是这么做的。尽管如此,它并没有像我预期的那样起作用。当我输入简单的计算时,它会很好地输出,但当我输入复杂的计算时,它会变得疯狂!它告诉我9999*9大约是-14554或者类似的数字。代码如下:

#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'C', '0', '=', '/'}
};
byte rowPins[ROWS] = {13,12,11,10};
byte colPins[COLS] = {9,8,7,6};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
boolean ansPresent = false;
boolean opSelected = false;
boolean final = false;
String num1, num2;
int answer;
char op;
void setup() {
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Arduino");
  lcd.setCursor(0,1);
  lcd.print("Calculator");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
}
void loop(){
  char key = myKeypad.getKey();
  if (ansPresent == false && key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')) {
    if (opSelected == false) {
      num1 = num1 + key;
      lcd.setCursor(0, 0);;
      lcd.print(num1);
    }
    else {
      num2 = num2 + key;
      lcd.setCursor(0, 1);
      lcd.print(num2);
      final = true;
    }
  }
  else if (ansPresent == false && opSelected == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')) {
    opSelected = true;
    op = key;
    lcd.setCursor(15, 0);
    lcd.print(op);
    lcd.setCursor(15, 1);
    lcd.print("=");
  }
  else if (ansPresent == false && final == true && key != NO_KEY && key == '=') {
    if (op == '+'){
      answer = num1.toInt() + num2.toInt();
    }
    else if (op == '-') {
      answer = num1.toInt() - num2.toInt();
    }
    else if (op == '*') {
      answer = num1.toInt() * num2.toInt();
    }
    else if (op == '/') {
      answer = num1.toInt() / num2.toInt();
    }     
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(answer);
      ansPresent = true;
  }
  else if (key != NO_KEY && key == 'C') {
    lcd.clear();
    ansPresent = false;
    opSelected = false;
    final = false;
    num1 = "";
    num2 = "";
    answer = 0;
    op = ' ';
  }
}
#包括
#包括
#包括
液晶I2C液晶显示器(0x27,2,1,0,4,5,6,7,3,正极);
常量字节行=4;
常量字节COLS=4;
字符键[行][列]={
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{C',0','=','/'}
};
字节rowPins[行]={13,12,11,10};
字节colPins[COLS]={9,8,7,6};
小键盘myKeypad=小键盘(makeyMap(键)、行PIN、列PIN、行、列);
布尔值=假;
所选布尔值=false;
布尔final=false;
字符串num1,num2;
int答案;
char op;
无效设置(){
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.打印(“Arduino”);
lcd.setCursor(0,1);
lcd.打印(“计算器”);
延迟(2000年);
lcd.clear();
lcd.setCursor(0,0);
}
void循环(){
char key=myKeypad.getKey();
如果(ansPresent==false&&key!=NO|u key&&(key='1'| key='2'| key='3'| key='4'| key='5'| key='6'| key='7'| key='8'| key='9'| key='0')){
如果(opSelected==false){
num1=num1+键;
lcd.setCursor(0,0);;
lcd.print(num1);
}
否则{
num2=num2+键;
lcd.setCursor(0,1);
lcd.打印(num2);
最终=正确;
}
}
else如果(ansPresent==false&&opSelected==false&&key!=NO|key&&(key='/'| | key='*'| | key='-'| | key='+')){
opSelected=true;
op=键;
lcd.setCursor(15,0);
液晶显示打印(op);
lcd.setCursor(15,1);
lcd.打印(“=”);
}
否则如果(ansPresent==false&&final==true&&key!=NO_key&&key=='='='=')){
如果(op=='+'){
答案=num1.toInt()+num2.toInt();
}
else if(op='-'){
答案=num1.toInt()-num2.toInt();
}
else if(op=='*'){
答案=num1.toInt()*num2.toInt();
}
else if(op=='/')){
答案=num1.toInt()/num2.toInt();
}     
lcd.clear();
lcd.setCursor(0,0);
lcd.打印(应答);
ansPresent=正确;
}
否则如果(键!=无键&&key='C'){
lcd.clear();
ansPresent=假;
opSelected=false;
最终=假;
num1=“”;
num2=“”;
答案=0;
op='';
}
}

它这样做是有原因的吗?

这看起来确实像是一个16位有符号整数上的溢出,这个整数是。大于32767的数字不能用此符号表示。这不是一个错误,这是硬件的限制,它只有16位


您需要使用多个
int
值来保存较大的值。

Arduino Uno使用ATmega。在该电路板上,int是一个16位有符号值,范围为-32768到32767

这意味着您的乘法溢出,并且您看到打印出来的内容——根据文档——“不可预测”。所以一定不要这样做

有符号变量超出其有效值时的注意事项和警告 它们溢出的最大或最小容量。溢出的结果 是不可预测的,因此应避免这种情况。疾病的典型症状 溢出是变量“滚动”从其最大容量到 最小值,反之亦然,但情况并非总是如此。如果你 要实现此行为,请使用unsigned int


这很奇怪。不应该是
-14554
。我本来希望它是
-24455
。是时候在调试器中单步执行了?
#包括
,然后对您的操作数使用
std::int64\t
。我理解,但我如何在将数字字符串转换为整数之前将其分解?如果您需要挑战,可以尝试将其作为十进制字符进行计算,或者为Uno找到一个“bigint”库。这很酷,但是如果小数超过一定数量的数字,这有关系吗?看看
long
的限制,然后看看你需要做的事情的限制。如果您想超出这些界限,您将无法使用
long
,因此这是任意精度时间。好的,我将执行任意精度。