Sounds like your trying to parse a function. You'll have to enter the function as a string (either char * or string class), then use a loop to figuire out its contents. Depending on how robust/useful you want it to be, this could be a bit challenging for a beginner. Here's a start:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string sFormula = "";
strin sBuffer = "";
bool numStream(false);
int iVal[255] = {0};
int iCount(0);
cout << "Enter formula: ";
cin >> sFormula
for (int i(0); i < strlen(sFormula); ++i)
{
if (isdigit(sFormula[i]))
{
numStream = true;
sBuffer += sFormula[i];
}
if (numStream && !isdigit(sFormula[i])
{
numStream = false;
iVal[iCount++] = atoi(sBuffer);
}
}
return 0;
}
This will parse the formula and extract all the numbers into an array. This isn't really useful for solving anything, but it's a start. I would sugguest creating a class for holding/handling formulas and their operations. As you can see, it's a bit more complex an issue than it seems
Last edited by skatamatic; Nov 5th, 2008 at 3:15 pm.