1,105,578 Community Members

how to compute 2x-x+4x?

Member Avatar
hwoarang69
Posting Pro
572 posts since Feb 2012
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

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;}
        }
    }
Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
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.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

...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.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

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).

Member Avatar
hwoarang69
Posting Pro
572 posts since Feb 2012
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

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.

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

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 :)

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: