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
acoxia 26
- 5 Contributors
- forum12 Replies
- 13 Views
- 10 Years Discussion Span
- comment Latest Post by acoxia
skatamatic 371
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) ?
gregorynoob 2
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)...
acoxia 26
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
Sky Diploma 571
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.
acoxia 26
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
#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";
/*}*/
}
Sky Diploma 571
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.
Sky Diploma 571
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);
Freaky_Chris 299
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
Sky Diploma 571
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 .......
gregorynoob 2
i still don't really get it... if the input is in shape of (ax+b)(cx+d) you can easily solve this to acx^2+x(ad+bc)+bd, which you can simply extract from your string looking at them as being just sequences of digits. i still think the problem is not defined well, for example can the input be (1+7)(15-3)?
if the input is in the above style you don't have to implement fancy parsing.
i guess this: acx^2+x(ad+bc)+bd solves 'adding the x'es'.
Sky Diploma 571
Well yes, That is easy. But i am trying to implement a function that will give me the output as a string and secondly will work for any sequence taking String as a parameter.
in your way , if i input (x-a)(x-b)(x-c)
You will not be getting the right answer right.
That is what i am willing to do.
Write a program that will multiply each of its term with the other terms and then generate a string as the answer.
acoxia 26
well thanks for the advice all the atoi() came in really handy and sky yours bit more complicated then mine needs to be, hope it worked out well