I am trying to determine the asymptotic complexity of my program which takes in an input and determines if it's a polynomial or not.
"If the length of the input expression is m chars, what is the big-Oh complexity of your program with respect to m?"
My guess is O(m*log m) in which the first m is the for loop that iterates m times and log m is the while loop that counts exponents greater than 1 digit.
Also, I'm trying to save a "largest" value which holds the largest exponent in order to calculate the polynomials runtime complexity. However, I am having confusion with storing the exponent correctly. Can anyone recommend an easier way?
example input: "n^23 + 4n^10 - 3" should have 23 as the largest exponent
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
int pcount = 1; //count for # of variables( min 3 to be poly)
int largest = 0; // used for complexity
int temp=0;
cout << "Enter equation below. " << endl;
getline(cin,input); //to input polynomial
cout << endl << input << endl;
if (input[0] == '-' || input[0] == '+') //to check for '-' as first char
pcount--;
bool pcheck = true;
for (unsigned i = 0; i < input.size(); i++)
{
temp = 0;
cout << input[i] << endl;
if ( input[i] == '+' || input[i] == '-' )
pcount++;
if ( input[i] == 'n') //checks for integer
{
if ( input[i+1] && input[i+1] == '^' )
{
if (input[i+2] == 46 || input[i+2] == 43 || input[i+2] == 45)
{
cout << "fail" << endl;
pcheck = false;
}
temp = input[i+2]-48; // to check for largest exp
while ( input[i+2] != 43 && input[i+2] != 45)
{
if ( i+3 == input.size())
{
cout << "break";
break;
}
if( input[i+2] < 48 || input[i+2] >57) // 48-57 is ascii
{
cout << "fail" << endl;
pcheck = false;
}
i++;
temp = temp * 10;
temp = temp + (input[i+2]-48);
if (temp > largest)
largest = temp;
}
}
}
}
if ( pcheck == true && pcount >= 3) //valid ints and at least 3 variables
{
cout << "polynomial detected!" << endl << "The big-Oh complexity is O(n^" <<
largest << ")." << endl;
}
else
cout << "not a poly" << endl;
return 0;
}