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;
}
|