factoring binomials

Thread Solved
Reply

Join Date: Sep 2008
Posts: 13
Reputation: acoxia is an unknown quantity at this point 
Solved Threads: 0
acoxia's Avatar
acoxia acoxia is offline Offline
Newbie Poster

factoring binomials

 
0
  #1
Oct 2nd, 2008
Hello i have a project to write a program that takes 2 binomials from a text file then factor them and output it into another text file. I'm curious on how to begin going about this do i take the 2 binomials such as (x+5)(x+9) and put them in a string or is there a function to multiply with () any advice on how to get started would be appreciated
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: factoring binomials

 
0
  #2
Oct 2nd, 2008
Is that how the binomials are saved to the file? If so, then you will need load it into a string, then parse the string. If you aren't looking for robustness and user usability, you could just say the 4th and 9th letter in the string are to be used (and therefore converted to an int) for the calculation. I'm not too sure which library/function converts a string to a number, but I know there is one (probably in iostream or iomanip).

However, if you want to be able to take trinomials, multiple digit ints, different variable names etc... You will need to parse it a bit more dynamically. ie traverse the string and once it finds a (, the next letter is used as the variable name, then look for an operator (+ or -) next (probably ignoring whitespace) and finally look for the number, which may be several digits long so you must add them to a temp string until a ) is found. Then do it all over again until all of the factors are loaded. Is this making sense/helping?

Although...that form of binomial is already factored... Perhaps you are importing an unfactored binomial (such as (x^2) + 4) ?
Last edited by skatamatic; Oct 2nd, 2008 at 2:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: factoring binomials

 
0
  #3
Oct 2nd, 2008
if you could post a more detailed description of the problem... if you're only working with square binomials it should be really simple.
for example, if the shape of the input is
ax^2 + bx + c, all you have to do is isolate a, b, and c, solve the equation, and use the equation solutions to form the result.
x^2 - 2x - 3 gives solutions 3 and -1, so the factoring result would be (x - 3)(x + 1)...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 13
Reputation: acoxia is an unknown quantity at this point 
Solved Threads: 0
acoxia's Avatar
acoxia acoxia is offline Offline
Newbie Poster

Re: factoring binomials

 
0
  #4
Oct 3rd, 2008
Thank you for both your replys id liek to post a more accurate discription but all was told is take (x+3)(x+4) types of binomials from a text and output it factored into another ..

thank you for the position string idea . is that under the find function to select parts of a string? or somewhere else

thank you again for replys
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 98
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: factoring binomials

 
0
  #5
Oct 3rd, 2008
Well i dont get it , You are given two Binomails as input and then you are required to split them ..

This is very simple. I GUESS you could just search for "(" start and ")" and give out the value in between as a factor.

Unless You have Squares Involved.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 13
Reputation: acoxia is an unknown quantity at this point 
Solved Threads: 0
acoxia's Avatar
acoxia acoxia is offline Offline
Newbie Poster

Re: factoring binomials

 
0
  #6
Oct 6th, 2008
Hello below is what i have so far it basically just takes the 2 numbers from the binomial and outputs them trying to plug them into the x^2 + bx + c format but having a few problems mainly adding the two numbers i get like 110 not sure if im able to even add them or if i have to change it to a different type. any suggestions on how to add them or make this work easier would be appreciated

also the text file has (x+2)(x+3) in it




  1. #include <iostream>
  2. #include <fstream>
  3. #include <time.h>
  4. #include <string>
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10. void guessword(string binomial);
  11. int main ()
  12. {
  13. // insert code here...
  14. std::cout << "Hello, World!\n";
  15.  
  16.  
  17. string binomial;
  18. ifstream infile;
  19. infile.open("desktop/input.txt");
  20.  
  21. // get binomial
  22. getline (infile, binomial);
  23.  
  24.  
  25. guessword(binomial);
  26.  
  27.  
  28. // test make sure inputing binomial
  29. cout <<binomial;
  30. return 0;
  31. }
  32.  
  33. void guessword(string binomial)
  34. {
  35. int x; //x equals first number
  36. int z; //y equals 2nd number
  37. int a; // a equals z+x
  38. string y ("X");
  39. string w ("X^2");
  40.  
  41. /*if (binomial.find_first_of("-") != 3||7);{*/ // attempting to do if no negative in number
  42. x=binomial.find_first_of("0123456789"); //find a number
  43. cout<<binomial[x];
  44. cout<<"\n";
  45.  
  46.  
  47. z=binomial.find_last_of("0123456789");
  48. cout<<binomial[z];
  49. cout<<"\n";
  50.  
  51. a=binomial[x] + binomial[z] ;
  52.  
  53. cout << w << "+ " << binomial[x] << y << "+ " << a;
  54. /*}*/
  55. /*if (binomial.find_first_of("-") == 3||7);{*/ // atempting to run if a negative
  56. cout << "negatives";
  57.  
  58. /*}*/
  59. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 98
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: factoring binomials

 
0
  #7
Oct 6th, 2008
Well You will need a pretty complicated program to do so .

This will involve a lot of playing with strings .

First you will need to segregate both of the functions .

therefore you will need a function which will search for "(" and store the contents into a string until ")" arises .

Then the same with the second factor.

After that you take the first string and search for the "+" or"-" character . So you will again get 2 more strings.

Do the same with the next one also and you will get 4 different strings.

After this you use the "+ Operator" for strings and join up string 3 and string 4 to string 1 and will do the same to string 2 ;

After which you can search if any characters are repeating in the strings.

if x repeats n times replace all the x's with x^n;

Then using the "+Operator " Join up all the strings and then you will end up with the equation .

This is a little challenging and i am after doing this too have fun with it.
Last edited by Sky Diploma; Oct 6th, 2008 at 2:22 pm.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 98
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: factoring binomials

 
0
  #8
Oct 6th, 2008
Hey I have completed the total coding. However i will be needing a parser to add in all the numbers for them to make sense.

Sending in

(x+2) and (x+3)

as input i currently get

x^2+2 x +x 3+2(3);
Last edited by Sky Diploma; Oct 6th, 2008 at 4:42 pm.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: factoring binomials

 
0
  #9
Oct 6th, 2008
Btw, atoi() will convert it to a number, this may prove helpful for you to multiply your numbers. It will also convert it back to a char from a number.

Chris
Last edited by Freaky_Chris; Oct 6th, 2008 at 5:24 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 98
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: factoring binomials

 
0
  #10
Oct 6th, 2008
Well i get that part .. But how do i add 2x +3x? is my question.

I must first remove x and then add convert and add them up after which i can again get back to add the "x"


Going nowhere on the coeffecients of X^2 and X .......
Last edited by Sky Diploma; Oct 6th, 2008 at 6:00 pm.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC