好的我读了评论并试图通过添加if语句来停止递归来改进代码,但我仍然收到此错误:
0xC00000FD:堆栈溢出(参数:0x00000001,0x00322FB0)。 (我正在使用韩文版的编译器,所以我翻译了错误信息)
我正在尝试使用递归
创建一个具有3个函数的简单计算器数字+或 - 功能(数字,操作)
我有两个接收运算符和数字的函数
和一个用于执行递归的递归函数。
这是我做的,但我一直在收到错误。你能和我分享一些智慧吗?
#include <stdio.h>
#include <string.h>
//Input: 'sub_exp' the value of the sub expression to the left of'op'
//op: an operator: + or -
//Effect: the whole expression is evaluated using recursion
//output: this ufnctino returns the value of the expression
float s_exp(float sub_exp, char op)
{
switch (op)
{
case '+':
return sub_exp + s_exp(sub_exp, op) ; //add the function recursively
break;
case '-':
return sub_exp- s_exp(sub_exp,op); //subtract the function recursively
break;
}
}
float get_num() //this function receives number
{
static float num;
printf("please input the numerical value \n");
scanf_s("%f", &num);
printf("%5.5f", num);
return num;
}
char get_op() //this function receives an operator
{
static char oper;
//receives the variables and returns the variable
printf("please input the operator");
scanf_s("%c",&oper);
printf("\n %c", oper);
return oper;
}
void main(void){
//run the recursive function
s_exp(get_num(), get_op());
}