Thread: desperately need help for my 9-function continuous calculator

1. desperately need help for my 9-function continuous calculator

i've been working on this for 2 days now and i can't seem to get it done. my brain's really drained and i've to submit this in 1 and a half hour that's why i decided to seek help.

the calculator should be able to calculate the following: 4 basic operations, power (^), factorial (!), sin (s), cos (c), and nth root of a number (r).

examples of what the user can enter are: (those in bold are user inputs)
5+5
10^2
100-50
50r2
7.0710 and so on

it can also be

5!
120-115
5s
0.087 and so on

but all i've accomplished after 2 days is only 1 operation! say 5+5 or 68^2 and so on.. there's no continuity in the calculator.

i can't seem to figure out the scanf for 3 inputs (say 5^2 in first calculation), 2 inputs (say5+5 in either initial or succeeding calculation) or 1 input (say 5! in succeeding calculation).

i know the codes for the 9-functions and ran them individually, but i can't seem to put them together!

i hope someone would help me on this. thanks.
Code:
```#include <stdio.h>
double sum(double x,double y);
double diff(double x,double y);
double prod(double x,double y);
double quo(double x,double y);
double power(double b,double e);
double factorial(double x);
double sine(double x);
double cosine(double x);
int main(){
double a,b,result;
char o;
printf("Legend\n + - * / ^ ! s c r\n");

for(;1;){
scanf("&#37;lf%c%lf",&a,&o,&b);
getchar();
switch(o)
{
case '+':
result=sum(a,b);
printf("%.2lf",result);

break;
case '-':
result=diff(a,b);
printf("%.2lf",result);

break;
case '*':
result=prod(a,b);
printf("%.2lf",result);
break;
case '/':
result=quo(a,b);
printf("%.2lf",result);

break;
case '^':
result=power(a,b);
printf("%lf",result);

break;
case '!':
result=factorial(a);
printf("%.0lf",result);

break;
case 's':
result=sine(a);
printf("%.4lf",result);

break;
case 'c':
result=cosine(a);
printf("%.4lf",result);

break;
case 'r':

result=root(a,b);
printf("%.6lf",result);

break;
default:
main();
}
}

scanf("%c%lf",&o,&a);

switch(o){
case '+':
result=sum(result,a);
printf("%.2lf",result);

break;
case '-':
result=diff(result,a);
printf("%.2lf",result);

break;
case '*':
result=prod(result,a);
printf("%.2lf",result);

break;
case '/':
result=quo(result,a);
printf("%.2lf",result);

break;
case '^':
result=power(result,a);
printf("%lf",result);

break;
case '!':
result=factorial(result);
printf("%.0lf",result);

break;
case 's':
result=sine(result);
printf("%.4lf",result);

break;
case 'c':
result=cosine(result);
printf("%.4lf",result);

break;
case 'r':
result=root(result,a);
printf("%.6lf",result);

break;
default:
main();
}

getchar();
getchar();

}
double sum(double x, double y){
double sum;
sum=x+y;
return sum;
}
double diff(double x, double y){
double diff;
diff=x-y;
return diff;
}
double prod(double x, double y){
double prod;
prod=x*y;
return prod;
}
double quo(double x,double y){
double quo;
quo=x/y;
return quo;
}
double power(double b,double e){
if(b==0 && e==0){
printf("Invalid.\n");
main();
}
else if(e==1){
return b;
}
else if(e==0){
return 1;
}
else{
return b*power(b,e-1);
}
}
double factorial(double x){
int ctr;
double fact;
if(fact<0){
printf("Invalid.\n");
main();
}
else{
fact=1;
for(ctr=1;ctr<=x;ctr++){
fact*=ctr;
}
return fact;
}
}
double sine(double x){
int n;
double sin,final_sin=0;
for(n=0;n<=200;n++){
sin=(power(-1,n)*power(x,(2*n)+1))/factorial((2*n)+1);
final_sin+=sin;
}
return final_sin;
}
double cosine(double x){
int n;
double cos,final_cos=0;
for(n=0;n<=200;n++){
cos=(power(-1,n)*power(x,2*n))/factorial(2*n);
final_cos+=cos;
}
return final_cos;
}
double up,low,fu,fl,m,fm;
printf("\nEnter upper limit: ");
scanf("%lf",&up);
printf("enter lower limit: ");
scanf("%lf",&low);
printf("\nfu=%lf and fl=%lf.",fu,fl);
if((fu*fl)<0){

while((up-low)>0.00001)
{
m=(up+low)/2;
if((fu*fm)>0){
up=m;
}
if((fu*fm)<0){
low=m;
}
}
}
else{
printf("\nInvalid guesses.\n");
}
return m;
}```

2. Perhaps using a string parser? That is, keep reading characters, in stages, until a certain character is encountered. If numbers are encountered, treat them as numbers using atoi and atof to convert the string into a number. When a special character is encountered that isn't a number, it is processed until numbers start again and whatever is found, the related operation/function is used (logged at first). The string is continued to be parsed until either the end or another operator is encountered. Once everything is logged and processed, the numbers are applied and the operator function used based on that. This is just one method but may be a poor method.

3. we haven't been introduced to strings so we're not allowed to do it.
also, math.h shouldn't be used.

4. > and i've to submit this in 1 and a half hour that's why i decided to seek help.
You're sunk then on this assignment, it seems that you've just handed it in.

Given a day, we could have done something meaningful about it.

5. A grammar, a lexical scanner, a syntactical parser, some evaluation here and there ... and maybe 4-5 hours and you could really impress your teacher.

6. Why oh why are you calling the main function from within the code? Never do this. Main is the starting point for program execution and should never be called.

main();

7. Also, better indentation could work. In addition, why are there many calls to "getchar", two consecutive ones at the end even. Your logic also seems a bit faulty. Take the factorial function for one.

Code:
```double factorial(double x){
int ctr;
double fact;
if(fact<0){
printf("Invalid.\n");
main();
}
else{
fact=1;
for(ctr=1;ctr<=x;ctr++){
fact*=ctr;
}
return fact;
}
}```
Flagged in a reddish orange color is one of the problems. Fact, in this case, is uninitialized and in your if statement, it should reference the parameter passed to the function.