如何在C语言计算器中检测表达式除零错误?解决浮点异常
解决方案
要解决除零触发浮点异常的问题,只需在除法运算前添加除数为零的检测逻辑,一旦检测到除零操作,让程序返回0即可。具体修改如下:
1. 修改deal_date函数,添加除零检测
在除法分支中,先判断取出的除数是否为0,若为0则输出错误信息并返回错误码:
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] == '/') { // 新增:检测除数是否为0 if (num_1 == 0) { printf("除零错误\n"); return -1; // 返回错误码,通知上层处理 } 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 0; // 正常执行返回0 }
2. 在main函数中处理错误码
调用deal_date时检查返回值,若返回-1则直接终止程序并返回0:
else if(i == -1) /* data processing */ { int ret = deal_date(operand,oper,&top_num,&top_oper); if (ret == -1) { return 0; // 触发除零,程序返回0 } }
修改后的完整代码
#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] == '/') { if (num_1 == 0) { printf("除零错误\n"); return -1; } 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 0; } 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 */ { int ret = deal_date(operand,oper,&top_num,&top_oper); if (ret == -1) { return 0; } } } str ++; /* point to the next character of the expression */ } printf("%d\n",operand[0]); /*output result*/ return 0; }
修改后,当表达式中出现除零操作(比如10/0、(5-5)/2等),程序会输出"除零错误"并返回0,避免浮点异常。
内容的提问来源于stack exchange,提问作者p0vi3
相关产品推荐
相关产品推荐

