简体   繁体   English

如何在计算器 C 中检查表达式是否被 0 除? 浮点异常

[英]How to check if the expression has division by 0 in calculator C? Floating point exception

I tried to make simple calculator supporting brackets and classic operators +,-,*,/ and I also need to eliminate division by zero in it.我试图制作支持括号和经典运算符 +、-、*、/ 的简单计算器,我还需要在其中消除被零除。 I may not be able to cope with it, I need to check if there is division by zero in the expression and if so function return 0. Now it returns Floating point exception .我可能无法应付它,我需要检查表达式中是否有被零除,如果有则 function 返回 0。现在它返回Floating point exception Can someone help?有人可以帮忙吗?

My code:我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 1024

int insert_operand(int *operand, int * top_num, int num) /* data is pushed into the data stack*/
{
     (*top_num) ++;
     operand[*top_num] = num; /*save data*/

     return 0; /*Exit normally*/
}

int insert_oper (char * oper , int *top_oper , char ch)
{
     (*top_oper)++;
     oper[*top_oper] = ch; /*save operator*/

     return 0; /*Exit normally*/
}

int compare(char *oper , int *top_oper , char ch) /* compare the priority of the operating server*/
{

     if((oper[*top_oper] == '-' || oper[*top_oper] == '+') /*Determine whether the current priority is higher than the priority of the operator at the top of the stack*/
             && (ch == '*' || ch == '/'))
     {
         return 0;
     }

     else if(*top_oper == -1 || ch == '('
             || (oper[*top_oper] == '(' && ch != ')')) /*Determine whether the operator stack is empty; whether the top operator is '('*/
     {
         return 0;
     }

     else if (oper[*top_oper] =='(' && ch == ')')
     {
         (*top_oper)--;
         return 1;
     }

     else
     {
         return -1; /*Operate the operator*/
     }

}

int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*perform data operation*/
{
     int num_1 = operand[*top_num]; /*Take out two data from the data stack*/
     int num_2 = operand[*top_num - 1];

     int value = 0;

     if(oper[*top_oper] == '+')
     {
         value = num_1 + num_2;
     }

     else if(oper[*top_oper] == '-')
     {
         value = num_2 - num_1;
     }

     else if(oper[*top_oper] == '*')
     {
         value = num_2 * num_1;
     }

     else if(oper[*top_oper] == '/')
     {
         value = num_2 / num_1;
     }

     (*top_num) --; /*Move the top of the data stack down one bit*/
     operand[*top_num] = value; /*Push the obtained value into the data stack*/
     (*top_oper) --; /*Move the top of the operator stack down one bit*/

    return value;
}

void compress(char *stx)    /* The additional function */
{
    char work[101];
    int i = strlen(stx);

    strcpy(work, stx);

    for (int j = 0; j < i; j++)
    {
        stx[j] = 0;
    }

    i = 0;

    for (int j = 0; j < (int)strlen(work); j++)
    {
        if (work[j] != ' ')
        {
            stx[i] = work[j];
            i++;
        }
    }
}

int main()
{
     int operand[MAX_SIZE] = {0}; /*data stack, initialize*/
     int top_num = -1;

     char oper[MAX_SIZE] = {0}; /*operator stack, initialize*/
     int top_oper = -1;

     char *str = (char *) malloc (sizeof(char) * 100); /*get expression (without =)*/
     //scanf("%s", str);
     scanf("%[^\n]", str);  /* Refined the scanf call to receive all characters prior to the newline/return character */

     compress(str);         /* Added this function to remove spaces */

     char* temp;
     char dest[MAX_SIZE];
     int num = 0;

     int i = 0;
     while(*str != '\0')
     {
         temp = dest;

         while(*str >= '0' && *str <= '9') /*judging whether it is data*/
         {
             *temp = *str;
             str++;
             temp++;
         } /*Encounter a symbol to exit*/

         if(*str != '(' && *(temp - 1) != '\0') /*Determine whether the symbol is '('*/
         {
             *temp = '\0';

             num = atoi(dest); /*convert string to number*/
             insert_operand(operand, &top_num,num); /*Push data into the data stack*/
         }

          while(1)
          {
              i = compare(oper,&top_oper,*str); /*judgment operator priority*/

             if(i == 0)
             {
                 insert_oper(oper,&top_oper,*str); /*press operator*/
                 break;
             }

             else if(i == 1) /*judging whether the expression in brackets ends*/
             {
                 str++;
             }

             else if(i == -1) /* data processing */
             {
                 deal_date(operand,oper,&top_num,&top_oper);
             }

          }

         str ++; /* point to the next character of the expression */
     }

     printf("%d\n",operand[0]); /*output result*/

     return 0;

}

Thanks for all answers感谢所有答案

I would detect if the divisor is zero along these lines:我会沿着这些线检测除数是否为零:

    else if(oper[*top_oper] == '/')
    {
        value = num_1 ? num_2 / num_1 : 0;
    }

or perhaps more verbose:或者更详细:

    else if(oper[*top_oper] == '/')
    {
        if(num_1)
            value = num_2 / num_1;
        else
            value = 0;
    }

and example session:和示例 session:

1 / 2
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM