More Help With Foiling

A few weeks ago I wrote a program to foil two binomials, and its functioning, but I now want to allow the user to enter their own exponents using a caret ^ and then the exponent. It isn't grabbing either of the exponents, however. Please help! Thanks!
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;
using std::endl;

const int max = 50;

class CFoil		// CFoil class to foil the equation
{
private:
	int coefficient1, coefficient2;		// store the coefficients, exponents, and numbers
	int number1, number2;
	int exponent1, exponent2;
	bool  co1, co2;		// keep track of which number the program needs to get next
	bool num1, num2;
	char* str;

	void calculate();		// calculate and output the answer

public:
	CFoil(char* pstr);		// constructor
	~CFoil();				// deconstructor
	void evaluate();		// evaluate the expression
};

CFoil::~CFoil()
{
	str = NULL;		/* clear the str pointer at the end of each calculation so new data can be entered by the user when
				   the object goes out of scope at the end of each forloop */
}

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

void CFoil::calculate()
{
	cout << "EXPONENT 1: " << exponent1 
		<< endl << "EXPONENT 2: " << exponent2 
		<< endl;
	cout << coefficient1 * coefficient2 << "x^" << exponent1 + exponent2 << " + "  
		<< (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 past the '*' 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;
			this->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';
				}
			}
			
			if(*(str+i) == '^')
			{
				i++;
				while(isdigit(*(str+i)))
				{
					exponent2 *= 10;
					exponent2 += *(str+i) - '0';
				}
			}
			else
				exponent2 = 1;

			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';
				}
			}
			
			if(*(str+i) == '^')
			{
				i++;
				while(isdigit(*(str+i)))
				{
					exponent1 *= 10;
					exponent1 += *(str+i) - '0';
				}
			}
			else
				exponent1 = 1;
		 
			co1 = false;
			num1 = true;
		}
	}

	return;
}	// end of CFoil

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

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

	return 0;
}


Last edited on
Fixed it, nevermind!
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
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
using std::cin;

const int max = 50;

class CFoil
{
private:
	char* str;
	int index;
	bool num1, co1, ex1;
	int number1, number2, coefficient1, coefficient2, exponent1, exponent2;

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

CFoil::CFoil(char* pstr):
		index(0), co1(true), num1(true), ex1(true), 
		number1(0), number2(0), coefficient1(0),
		coefficient2(0), exponent1(0), exponent2(0)
{
	str = &(*(pstr));
}

void CFoil::evaluate()
{
	while(1)
	{
		switch(*(str+index))
		{
		case '(':		// if there is a opening parantheses get the next number		
			while(isdigit(*(str + ++index)))	
			{
				if(co1)
				{
					coefficient1 *= 10;
					coefficient1 += *(str+index) - '0';		// change from ascii to decimal			
				}
				else
				{
					coefficient2 *= 10;
					coefficient2 += *(str+index) - '0';
				}
			}

			co1 = false;
			break;
		case '+':		// if there is a + sign get the next number
			while(isdigit(*(str + ++index)))
			{
				if(num1)
				{
					number1 *= 10;
					number1 += *(str+index) - '0';
				}
				else
				{
					number2 *= 10;
					number2 += *(str+index) - '0';
				}
			}

			num1 = false;
			break;
		case '-':
			while(isdigit(*(str + ++index)))
			{
				if(num1)
				{
					number1 *= 10;
					number1 -= *(str+index) - '0';
				}
				else
				{
					number2 *= 10;
					number2 -= *(str+index) - '0';
				}
			}
			num1 = false;
			break;
		case '^':			 

			while(isdigit(*(str + ++index)))
			{
				if(ex1)
				{
					exponent1 *= 10;
					exponent1 += *(str+index) - '0';
				}
				else
				{
					exponent2 *= 10;
					exponent2 += *(str+index) - '0';
				}
			}
			ex1 = false;
			break;
		case 'x':
			index++;
			break;
		case ')':		// if there is a ')' or 'x' increment index by 1
			index++;
			break;
		case '\0':		// if at the end of the string
			this->calculate();
			exit(1);
			break;
		default:
			cout << "Syntax error at position: " << index << endl;
			cout << str << endl;
			for(; index > 0; index--)
				cout << " ";
			cout << "^";
			cin.getline(0, 0, '\n');
			exit(1);
		}
	}
}

void CFoil::calculate()
{
	if(exponent1 == 0)
		exponent1 = 1;
	if(exponent2 == 0)
		exponent2 = 1;
	cout << coefficient1 * coefficient2 << "x^" 
		<< exponent1 + exponent2 << " + " 
		<< (coefficient1 * number2) + (coefficient2 * number1)
		<< "x + " << number1 * number2 
		<< endl;

	return;
}

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

int main(void)
{
	cout << "Enter two binomials in parantheses, or an empty line to quit.  \nUse a caret ^ for exponents." 
		<< endl;
	char str[50] = { 0 };
	char* pstr = str;

	for(;;)
	{
		cin.getline(str, max, '\n');
		
		if(!str[0])
			exit(1);
		for(size_t i = 0; i < strlen(str); i++)
			if(isalpha(*(str+i)))
				*(str+i) = 'x';

		CFoil foil(pstr);
		foil.evaluate();
	}

	return 0;
}
Topic archived. No new replies allowed.