mardi 3 mars 2015

creating simple calculator using recursive function in C programming



i am trying to make a simple calculator that has 3 functions


s_exp(float sub_exp,char op): this function receives sub_exp(floating number) and op(an operator) and calculates


Num op(+ or - ) recursive(num)


Here is what i did but i am keep getting errors



#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());



}



Aucun commentaire:

Enregistrer un commentaire