#include #include using namespace std; const int MAX_POST_LENGTH = 1000; const int MAX_NAME_LENGTH = 50; const int MAX_VALUE_LENGTH = 50; bool getValue(const char queryString[], const char name[], char value[]); void fixQueryString(char queryString[]); void showFirstRequest(); void showWin( int lValue, int rValue, char operation ); void showLoss( int lValue, int rValue, char operation ); int main() { cout << "content-Type:text/html\n\n"; char* pContentLength = getenv( "CONTENT_LENGTH" ); if(pContentLength != NULL) { int postLength = atoi( pContentLength ); char post[MAX_POST_LENGTH]; cin.get(post, postLength + 1); fixQueryString(post); char lValueText[MAX_VALUE_LENGTH] = ""; char rValueText[MAX_VALUE_LENGTH] = ""; char guessText[MAX_VALUE_LENGTH] = ""; char operationText[MAX_VALUE_LENGTH] = ""; getValue(post, "lvalue", lValueText); getValue(post, "rvalue", rValueText); getValue(post, "guess", guessText); getValue(post, "operation", operationText); int lValue = atoi( lValueText ); int rValue = atoi( rValueText ); int guess = atoi( guessText ); if(operationText[0] == '+'&& lValue + rValue == guess ) { showWin( lValue, rValue, operationText[0] ); } else if(operationText[0] == '-'&& lValue - rValue == guess ) { showWin( lValue, rValue, operationText[0] ); } else { showLoss( lValue, rValue, operationText[0] ); } } else { showFirstRequest(); } return 0; } void showWin( int lValue, int rValue, char operation ) { cout << "
\n"; cout << "

Correct!!!

\n"; if( operation == '+' ) { cout << lValue << " " << operation << " " << rValue << " = " << lValue + rValue; } else { cout << lValue << " " << operation << " " << rValue << " = " << lValue - rValue; } cout << "\n"; cout << "
\n"; } void showLoss( int lValue, int rValue, char operation ) { cout << "
\n"; cout << "

Incorrect :(

\n"; if( operation == '+' ) { cout << lValue << " " << operation << " " << rValue << " = " << lValue + rValue; } else { cout << lValue << " " << operation << " " << rValue << " = " << lValue - rValue; } cout << "\n"; cout << "
\n"; } void showFirstRequest() { srand(time(0)); int lvalue = rand()% 10 + 1; int rvalue = rand()% 10 + 1; char operation = '+'; int oprerationRandomness = rand() % 2; if( oprerationRandomness == 0 ) { operation = '-'; } cout << "
\n"; cout << ""; cout << "

Math Practice

\n"; cout << "\n"; cout << "\n"; cout << "\n"; cout << lvalue << " " << operation << " " << rvalue << " = "; cout << "\n"; cout << "\n"; cout << "
\n"; } bool getValue(const char queryString[], const char name[], char value[]) { bool result = false; char* p = strstr(queryString, name); bool foundNameInQuery = (p != NULL); bool foundEqualAfterName = ( *(p + strlen(name)) == '=' ); bool foundNameAtBeginingOfQuery = (p == queryString); bool foundAmpersanBeforeName = (*(p - 1) == '&'); if( foundNameInQuery && foundEqualAfterName && (foundNameAtBeginingOfQuery || foundAmpersanBeforeName)) { int indexOfName = (int)(p - queryString); result = true; int queryIndex = indexOfName + (int)strlen(name) + 1; int valueIndex = 0; while(queryString[queryIndex] != '&' && queryString[queryIndex] != '\0') { value[valueIndex] = queryString[queryIndex]; queryIndex++; valueIndex++; } value[valueIndex] = '\0'; } return result; } void fixQueryString(char queryString[]) { for(int i = 0; i < strlen(queryString); i++) { if( queryString[i] == '+') { queryString[i] = ' '; } if( queryString[i] == '%') { char firstHexValue = queryString[i+1]; char secondHexValue = queryString[i+2]; int asciiValue = 0; switch( firstHexValue ) { case 'F': asciiValue += 16; case 'E': asciiValue += 16; case 'D': asciiValue += 16; case 'C': asciiValue += 16; case 'B': asciiValue += 16; case 'A': asciiValue += 16; case '9': asciiValue += 16; case '8': asciiValue += 16; case '7': asciiValue += 16; case '6': asciiValue += 16; case '5': asciiValue += 16; case '4': asciiValue += 16; case '3': asciiValue += 16; case '2': asciiValue += 16; case '1': asciiValue += 16; } switch( secondHexValue ) { case 'F': asciiValue += 1; case 'E': asciiValue += 1; case 'D': asciiValue += 1; case 'C': asciiValue += 1; case 'B': asciiValue += 1; case 'A': asciiValue += 1; case '9': asciiValue += 1; case '8': asciiValue += 1; case '7': asciiValue += 1; case '6': asciiValue += 1; case '5': asciiValue += 1; case '4': asciiValue += 1; case '3': asciiValue += 1; case '2': asciiValue += 1; case '1': asciiValue += 1; } queryString[i] = (char)asciiValue; strcpy(queryString + i + 1, queryString + i + 3); } } }