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