| | |
factoring binomials
Thread Solved
![]() |
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
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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) ?
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.
•
•
Join Date: Jun 2008
Posts: 86
Reputation:
Solved Threads: 5
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)...
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)...
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
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
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.
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.
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
also the text file has (x+2)(x+3) in it
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <time.h> #include <string> using namespace std; void guessword(string binomial); int main () { // insert code here... std::cout << "Hello, World!\n"; string binomial; ifstream infile; infile.open("desktop/input.txt"); // get binomial getline (infile, binomial); guessword(binomial); // test make sure inputing binomial cout <<binomial; return 0; } void guessword(string binomial) { int x; //x equals first number int z; //y equals 2nd number int a; // a equals z+x string y ("X"); string w ("X^2"); /*if (binomial.find_first_of("-") != 3||7);{*/ // attempting to do if no negative in number x=binomial.find_first_of("0123456789"); //find a number cout<<binomial[x]; cout<<"\n"; z=binomial.find_last_of("0123456789"); cout<<binomial[z]; cout<<"\n"; a=binomial[x] + binomial[z] ; cout << w << "+ " << binomial[x] << y << "+ " << a; /*}*/ /*if (binomial.find_first_of("-") == 3||7);{*/ // atempting to run if a negative cout << "negatives"; /*}*/ }
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.
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.
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);
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: getfilepathname() doesnot return correct path
- Next Thread: Preprocessor #warning Command Question
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets





