So i've done a little messing arounds..................so can someone tell me why this doesn't work?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int input, number;
float currentnumber;
while (1)
{
input = currentnumber = number = 0;
while(1)
{
input = getchar();
switch(input)
{
//Handle Numeric values
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
number = number*10 + input;
break;
//Handle operations
case '^':
currentnumber = pow(currentnumber,number);
break;
case '*':
currentnumber *= number;
break;
case '/':
currentnumber /= number;
break;
case '+':
currentnumber += number;
break;
case '-':
currentnumber -= number;
break;
//when "=" or newline output results
case '=': case '\n':
printf("%f\n", currentnumber);
break;
//Default handler
default:
number = 0;
break;
}
}
}
system("PAUSE");
return 0;
}
I'm basing this off of my friends code, which does work, that looks like this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int c,numcur,num,op,i,temp;
while (1)
{
num=0; /*num is the number currently being determined*/
numcur=0; /*numcur is the evaluation, up to the most recent operator,
stored while the calculator determines the number after the operator*/
op=1; /* so that the first number is added*/
c=0;
while (1)
{
c=getchar();
if (c >= '0' && c <= '9') num= num*10 + (c-'0'); /*adds a digit to the end if c is a digit*/
else if (c == '+' || c == '-' || c == '^' || c == '*' || c == '/' || c == '=' || c == '\n')
{ /*performs the previous operation, then either stores the operator for the next one, or
breaks from the loop if the next operator is equals or enter*/
if (op == 1)
numcur=numcur+num;
else if (op == 2)
numcur=numcur-num;
else if (op == 3)
{ /*my power function*/
temp=numcur;
for (i=1; i<num; ++i)
{
numcur=numcur*temp;
}
}
else if (op == 4)
numcur=numcur*num;
else if (op == 5)
numcur=numcur/num;
/*now to store the operation*/
if (c == '+')
op=1;
else if (c == '-')
op=2;
else if (c == '^')
op=3;
else if (c == '*')
op=4;
else if (c == '/')
op=5;
else break; /*placed after the final assignments so that the last calculation
is made before the answer is displayed*/
num=0; /*after completing a calculation, the equation up to the current character
is evaluated and stored as numcur, and num is the next number to be determined*/
}
}
printf("%d\n\n", numcur); /*displays answer and then skips two lines*/
} /*infinite loop*/
}