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;}
}
}``````

Do you really need to give the 'x' treatment as a character?
It seems like the requirement would be to pass x to a function and return the calculated value.

...meaning: if you have a function that computes x:

``````int Solve2xMinusxPlus4x(int x)
{
return ((2*x) - x + (4*x));
}``````

...you could loop a call to that until you receive the answer you want.
If you test it by looping from 0 to 10, you would eventually …

## All 6 Replies

Do you really need to give the 'x' treatment as a character?
It seems like the requirement would be to pass x to a function and return the calculated value.

...meaning: if you have a function that computes x:

``````int Solve2xMinusxPlus4x(int x)
{
return ((2*x) - x + (4*x));
}``````

...you could loop a call to that until you receive the answer you want.
If you test it by looping from 0 to 10, you would eventually (at 1) receive the answer 5.
The x would drop off of the equation because you are solving FOR x.

So, the user is going to type the full equation on the command-line?
If so, will there be an equal sign?
With a formula, you'll either be expecting a result (by providing x) or
you'll be expecting x (by providing the expected result).

no there is no = sign, just x's. and also we dont know that user will enter 2x-x+4x. ever time user will enter different stuff. for ex user
can enter -2x+x, or 4x-2x-3-x-x+4x or -x+2x.

the code that enter above it works if there are no x's. and i dont know how to do it will x's

my think was if i remove all the x's by using for loop and seting x to \0. but it dont really work if user enter -x-2x.

traverse the array using a loop
when a character x is found convert the digit before the x using atoi() then save it temporarily to a variable then check the operation after the "x" after that check the next elements for the x and do the same

for example:
2x+3x
traverse the loop
when an x is found
convert and save 2 (the element before x)
check the operation (+) (make a condition for all operations)
check the set after the operator
check if the there is another x
convert and save 3
solve the current operation (3+2) and save the answer
continue traversing the array until the end of the array is found

anything else you need to be aware of are digits that doesn't have an x with them or x that is not preceded by a digit so the default should be 1 and finally negative numbers

note: I once did a similar program like this with algebraic equations but I used linked lists instead of arrays so I can have a better control of the elements but I believe the concept should be about similar...also coding this would be longer than the code you posted so good luck :)

You can basically treat the appearance of x as command 'add previous number to x-count' and any other not-number as 'add previous number to constant value'. Only thing is that if number was not there, you must default to one for x case, 0 for constant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.