I am trying to compute 2x-x+4x, so the answer should be 5x. but i am not sure how to get started. The "x"'s are messing me up. I am thinking of using for loop and replace x with '\0'. and than add or sub starting from left to right. but the problem i get is the middle x is 1 not '\0'. any through?
char ar[] = {'3','x','-','x','+','4','x'};
if i didnt had the x's i would do some thing like this:
printf("Enter Expression: ");
num1 = getchar();
if((num1 >= '0' && num1 <= '9'))
{
total = (int)num1 - '0';
}
// get rest of the input and do cal
while((ch = getchar()) != '\n')
{
//get rid of spaces
if(ch == ' ')
{
ch = '\0';
}
//get + , - , *, / sign and store it to operaer
if(ch == '+' || ch == '-' || ch == '*' || ch =='/')
{
operater = ch;
}
//get next int and convert to int
if((ch >= '0' && ch <= '9'))
{
num2 = (int)ch - '0';
if(operater == '+') { total = total + num2; num2=0;}
if(operater == '-') { total = total - num2; num2=0;}
if(operater == '*') { total = total * num2; num2=0;}
if(operater == '/') { total = total / num2; num2=0;}
}
}