Data structures Arduino:中缀到后缀

Data structures Arduino:中缀到后缀,data-structures,arduino,calculator,postfix-notation,infix-notation,Data Structures,Arduino,Calculator,Postfix Notation,Infix Notation,我想在Arduino上制作计算器,所以,这个计算器应该进行“操作顺序” 我相信我的“将中缀转换为后缀”代码是正确的,但可执行程序停止工作,我得到了无效的结果。我在其他arduinos上尝试了这个代码,但没有得到任何结果。在lcd上写入有时有效,但有时代码会重置arduino。我不明白为什么会发生这个错误 #include <Keypad.h> #include <LiquidCrystal.h> const byte SATIR = 4; const byte SUTU

我想在Arduino上制作计算器,所以,这个计算器应该进行“操作顺序”

我相信我的“将中缀转换为后缀”代码是正确的,但可执行程序停止工作,我得到了无效的结果。我在其他arduinos上尝试了这个代码,但没有得到任何结果。在lcd上写入有时有效,但有时代码会重置arduino。我不明白为什么会发生这个错误

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte SATIR = 4;
const byte SUTUN= 4;
char keys[SATIR][SUTUN] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};

byte rowPins[SATIR] = { 13, 10, 9, 8 };
byte colPins[SUTUN] = { 7, 6, 1, 0 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, SATIR, SUTUN );

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int SIZE = 20;
char stack[20];
int top = -1;
String num = "";
int number;

void setup() {
  lcd.begin(16, 2); //16x2 kullaniyoruz
  lcd.print("fatay"); 
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
}

int p = 0;

void loop(){

  char infix[20];
  char postfix[20]; 

  char key = kpd.getKey();

  if (key != NO_KEY){
    if(key == '=') {
      for(int z = 0; z<num.length(); z++){
        infix[z] = num.charAt(z);
      }
      number = num.length();
      delay (100);
      InfixToPostfix(infix,postfix); 

    } else {
      lcd.print(key);
      num+=key;
      delay(400);
    }
  }

}



////////////////////////////////////////////////////////////////////////////////////////

void push(char item) {
  if(top >= SIZE-1)
  {
    printf("\nStack Overflow.");
  }
  else
  {
    top = top+1;
    stack[top] = item;
  }
}

/* define pop operation */
char pop() {
  char item ;

  if(top <0)
  {
    printf("stack under flow: invalid infix expression");
    getchar();
    /* underflow may occur for invalid expression */
    /* where ( and ) are not matched */
    exit(1);
  }
  else
  {
    item = stack[top];
    top = top-1;
    return(item);
  }
}

////////////////////////////////////////////////////////////////////////////////////////

int is_operator(char symbol) {
  if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
  {
    return 1;
  }
  else
  {
  return 0;
  }
}



int precedence(char symbol) {
  if(symbol == '^')/* exponent operator, highest precedence*/
  {
    return(3);
  }
  else if(symbol == '*' || symbol == '/')
  {
    return(2);
  }
  else if(symbol == '+' || symbol == '-')          /* lowest precedence */
  {
    return(1);
  }
  else
  {
    return(0);
  }
}

void InfixToPostfix(char infix_exp[], char postfix_exp[])
{ 
  int i, j;
  char item;
  char x;

  push('(');                               /* push '(' onto stack */
  strcat(infix_exp,")");                  /* add ')' to infix expression */

  i=0;
  j=0;
  item=infix_exp[i];         /* initialize before loop*/

  while(item != '\0')        /* run loop till end of infix expression */
  {
    if(item == '(')
    {
      push(item);
    }
    else if( isdigit(item) || isalpha(item))
    {
      postfix_exp[j] = item;              /* add operand symbol to postfix expr */
      j++;
    }
    else if(is_operator(item) == 1)        /* means symbol is operator */
    {
      x=pop();
      while(is_operator(x) == 1 && precedence(x)>= precedence(item))
      {
        postfix_exp[j] = x;                  /* so pop all higher precendence operator and */
        j++;
        x = pop();                       /* add them to postfix expresion */
      }
      push(x);
      /* because just above while loop will terminate we have
      oppped one extra item
      for which condition fails and loop terminates, so that one*/

      push(item);                 /* push current oprerator symbol onto stack */
    }
    else if(item == ')')         /* if current symbol is ')' then */
    {
      x = pop();                   /* pop and keep popping until */
      while(x != '(')                /* '(' encounterd */
      {
        postfix_exp[j] = x;
        j++;
        x = pop();
      }
    }
    else
    { /* if current symbol is neither operand not '(' nor ')' and nor
      operator */
      printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */
      getchar();
      exit(1);
    }
    i++;


    item = infix_exp[i]; /* go to next symbol of infix expression */
  } /* while loop ends here */
  if(top>0)
  {
    printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */
    getchar();
    exit(1);
  }
  if(top>0)
  {
    printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */
    getchar();
    exit(1);
  }

delay(100);
lcd.setCursor(0,1);
delay (100);
  for(int k = 0; k<number; k++) {
      lcd.print(postfix_exp[k]);  

  }




}
#包括
#包括
常量字节SATIR=4;
常量字节SUTUN=4;
字符键[SATIR][SUTUN]={
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C'、'0'、'='、'/'}
};
字节行pins[SATIR]={13,10,9,8};
字节colPins[SUTUN]={7,6,1,0};
小键盘kpd=小键盘(makeyMap(键)、行PIN、列PIN、SATIR、SUTUN);
液晶显示器(12,11,5,4,3,2);
int SIZE=20;
字符堆栈[20];
int top=-1;
字符串num=“”;
整数;
无效设置(){
lcd.begin(16,2);//16x2 kullaniyoruz
液晶显示打印(“fatay”);
延迟(3000);
lcd.clear();
lcd.setCursor(0,0);
}
int p=0;
void循环(){
字符中缀[20];
字符后缀[20];
char key=kpd.getKey();
如果(键!=无键){
如果(键=='='){
用于(int z=0;z=SIZE-1)
{
printf(“\n堆栈溢出”);
}
其他的
{
顶部=顶部+1;
堆栈[顶部]=项目;
}
}
/*定义pop操作*/
char pop(){
字符项;
如果(顶部=优先级(项目))
{
postfix_exp[j]=x;/*所以弹出所有更高的进位运算符并*/
j++;
x=pop();/*将它们添加到后缀表达式*/
}
推(x);
/*因为就在上面,while循环将终止
多了一个项目
如果条件失败,循环终止,那么*/
推送(项目);/*将当前操作员符号推送到堆栈上*/
}
否则,如果(项==')/*如果当前符号为'),则*/
{
x=pop();/*弹出并持续弹出直到*/
而(x!='(')/*'('遇到*/
{
后缀_exp[j]=x;
j++;
x=pop();
}
}
其他的
{/*如果当前符号既不是操作数,也不是“(“nor”)”和“nor”
接线员*/
printf(“\n无效的中缀表达式。\n”);/*它是非法符号*/
getchar();
出口(1);
}
i++;
item=infix_exp[i];/*转到中缀表达式的下一个符号*/
}/*而循环在此结束*/
如果(顶部>0)
{
printf(“\n无效的中缀表达式。\n”);/*它是非法符号*/
getchar();
出口(1);
}
如果(顶部>0)
{
printf(“\n无效的中缀表达式。\n”);/*它是非法符号*/
getchar();
出口(1);
}
延迟(100);
lcd.setCursor(0,1);
延迟(100);

对于(int k=0;kw)来说,为什么不在真正的调试器中调试它,然后将正常工作的代码移植到Arduino?这就是专业人士(比如我自己)的方式为微控制器编写类似这样的代码。单独的代码块工作得很好。但问题是完整的代码不能有效工作;有时lcd停止工作,有时程序自动复位。“代码不能有效工作”是什么意思?任何错误?顺便说一句,您需要将所有
printf
转换为
Serial。将格式化字符串打印为stdout,而在Arduino上,输出字符串需要通过
Serial.print
Serial.println
发送到串行监视器。此外,尝试将延迟(400)更改为延迟(100)因为如果你按键盘太快,它可能会太长。