Help Foiling

Hey! I've been working on making a program to foil two sets of numbers. If you don't remember from Algebra 1 xD, foiling is used to multiply two binomials, and stands for:
First
Outer
Inner
Last

I have the main part of the program done, but for some reason it keeps getting caught at index 7. Please help me out here!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

const int max = 30;

class CFoil
{  
public:
	CFoil(char* pequ);		// constructor				
	void foil();			// foil
private:
	int coefficient1, coefficient2, number1, number2;
	bool co1, co2, num1, num2;
	int index;
	char* str;

	void evaluate();		// evaluate the equation and output it
};

CFoil::CFoil(char* pequ):
	co1(true), co2(false), num1(true), num2(false), index(0), coefficient1(0), coefficient2(0), number1(0), number2(0)
{
	str = &(*pequ);			// make str point to the address of the value pointed to by pequ
}

void CFoil::foil()
{
	for(;;)
	{
		cout << index << endl;
		switch(*(str+index++))
		{
		case '(':
			if(*(str+index) == 'x')
			{
				if(co1)
				{
					coefficient1 = 1;
					co1 = false;
					co2 = true;
					break;
				}
				if(co2)
					coefficient2 = 1;
			}
			
			while(*(str + index) != 'x')
			{		   
				if(co1)
				{
					coefficient1 *= 10;
					coefficient1 += *(str+index++) - '0';
				}
			}

			co1 = false;
			co2 = true;
			break;

		case '+':	
			while(*(str+index) != ')')
			{
				number1 *= 10;
				number1 += *(str+index++) - '0';
			}
			
			num1 = false;
			num2 = true;
			break;

		case '-':
			while(*(str+index) != ')')
			{
				number1 *= 10;
				number1 -= *(str+index++) - '0';
			}
			
			num1 = false;
			num2 = true;
			break;

		case '\0':
			this->evaluate();
			break;
		}
	}
}

void CFoil::evaluate()
{
	int first = coefficient1 * coefficient2;
	int outer = coefficient1 * number2;
	int inner = coefficient2 * number1;
	int last = number1 * number2;

	cout << "\t\t\t=" << first << "+" << outer + inner << "+" << last
		<< endl;
	
	return;
}

int main(void)
{
	char equation[max] = "(3x+2)(5x-1)";	  
	char* pequation = equation;
	
	CFoil solve(pequation);
	solve.foil();

	cout << endl;
	return 0;
}
Wouldn't it have been much easier to parse "3 2 5 -1" than "(3x+2)(5x-1)"? Your code breaks if I try to get even a little bit creative with the expression. E.g. "(x/5--1)((lim z->0 (sin(z)/z))x)".
well yes, but for this assignment my teacher said to allow the user to enter an expression such as:
(3x+2)(5x-1)

this is what you will have a good deal of the time when foiling, at least when factoring quadratic equations that are perfect square trinomials in either
ax^2+bx+c
x^2+bx+c

My suggestion is that you simply divide the input string into digits and non-digits: "_3__2__5_-1_". As long as you don't need to validate user input, this is the simplest approach.
good idea! working on it now :)
thank you! it works now, here the functioning code :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;
using std::endl;

const int max = 30;

void eatspaces(char* str)
{
	int i = 0;
	int j = 0;

	while((*(str + i) = *(str + j++)) != '\0')
		if (*(str + i) != ' ')
			i++;
	return;
}

class CFoil
{
private:
	int coefficient1;
	int coefficient2;
	int number1;
	int number2;
	bool co1;
	bool co2;
	bool num1; 
	bool num2;
	char* str;

	void calculate();
public:
	CFoil(char* pstr);
	~CFoil();
	void evaluate();
};

CFoil::~CFoil()
{
	str = NULL;
}

CFoil::CFoil(char* pstr):
	coefficient1(0), coefficient2(0), number1(0), number2(0), co1(true), co2(false), num1(false), num2(false)
{
	str = &(*pstr);
}

void CFoil::calculate()
{
	cout << coefficient1 * coefficient2 << "x^2+" 
		<< (coefficient1 * number2) + (coefficient2 * number1) 
		<< "x+" << number1 * number2 << endl;

	return;
}
void CFoil::evaluate()
{
	for(size_t i = 0; *(str+i) != '\0'; i++)
	{
		while(isdigit(*(str + i)))		// advance to the next number
			i++;

		if(num2)	 // grab the second number
		{
			if(*(str+i) == '-')
			{
				while(isdigit(*(str + ++i)))
				{
					number2 *= 10;
					number2 -= *(str+i) - '0';
				}
			}
			else
			{
				while(isdigit(*(str + ++i)))
				{
					number2 *= 10;
					number2 += *(str+i) - '0';
				}
			}
			num2 = false;
			calculate();
		}
		if(co2)		// grab the second coefficient
		{	
			if(*(str+i) == '-')
			{
				while(isdigit(*(str + ++i)))
				{
					coefficient2 *= 10;
					coefficient2 -= *(str+i) - '0';
				}
			}
			else
			{
				while(isdigit(*(str + ++i)))
				{
					coefficient2 *= 10;
					coefficient2 += *(str+i) - '0';
				}
			} 
			co2 = false;
			num2 = true;
		}
			
		if(num1)	// grab the first number
		{
			if(*(str+i) == '-')
			{
				while(isdigit(*(str + ++i)))
				{
					number1 *= 10;
					number1 -= *(str+i) - '0';
				}
			}
			else
			{
				while(isdigit(*(str + ++i)))
				{
					number1 *= 10;
					number1 += *(str+i) - '0';
				}
			}

			num1 = false;
			co2 = true;
		}
		if(co1)		// grab the first coefficient
		{
			if(*(str+i) == '-')
			{
				while(isdigit(*(str + ++i)))
				{
					coefficient1 *= 10;
					coefficient1 -= *(str+i) - '0';
				}
			}
			else
			{
				while(isdigit(*(str + ++i)))
				{
					coefficient1 *= 10;
					coefficient1 += *(str+i) - '0';
				}
			}

			co1 = false;
			num1 = true;
		}
	}

	return;
}

int main(void)
{
	char equation[max];  
	char* pequation = equation;
	cout << "Enter two binomials in parantheses, or an empty line to quit." << endl;
	for(;;)
	{
		CFoil foil(pequation);
		cin.getline(equation, max, '\n');
		eatspaces(pequation);
		if(!equation[0])
			exit(1);

		for(size_t i = 0; i < strlen(equation); i++)
		{
			if(!isdigit(*(pequation+i)) && *(pequation+i) != '-')		// turn all no numeric values into *
				*(pequation+i) = '*';
		}
		foil.evaluate();
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.