| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with project
how would i replace all c style strings with the string class i would it look.
I will show u my code double g_dblLastResult; double EvaluateExpression ( Expression* pExpr ) { switch(pExpr->op) { case OP_ADD : g_dblLastResult = pExpr->leftValue + pExpr->rightValue; break; case OP_SUB : g_dblLastResult = pExpr->leftValue - pExpr->rightValue; break; case OP_MULT: g_dblLastResult = pExpr->leftValue * pExpr->rightValue; break; case OP_DIV : g_dblLastResult = pExpr->leftValue / pExpr->rightValue; break; }; return g_dblLastResult; }; int GetOperator ( char op ) { switch(op) { case '+' : return OP_ADD; case '-' : return OP_SUB; case '*' : return OP_MULT; case '/' : return OP_DIV; }; return 0; }; Expression* ParseExpression ( LPCTSTR lpstrExpression ) { //Temporary cache for values char szBuffer[100] = {0}; LPTSTR pBuffer = szBuffer; Expression* pExpr = new Expression; char ch; enum STATE { FIRST_VALUE, OP, SECOND_VALUE, DONE } state = FIRST_VALUE; //State machine - either we are looking for values or we are looking for operations //Skip spaces and tabs size_t nIdx = 0; size_t nLen = strlen(lpstrExpression); while (nIdx < nLen) { ch = lpstrExpression[nIdx]; switch(state) { case FIRST_VALUE : case SECOND_VALUE : { //Store the value as needed if (isdigit(ch)) { *pBuffer = ch; ++pBuffer; ++nIdx; //Next character } else if (((ch == 'A') || (ch == 'a')) && (nIdx == 0)) { //Special of the special value of ANS char chN = lpstrExpression[nIdx+1]; char chS = lpstrExpression[nIdx+2]; if (((chN == 'N') || (chN == 'n')) && ((chS == 'S') || (chS == 's'))) { //Use the previously stored value and move to the next state if (state == FIRST_VALUE) { pExpr->leftValue = g_dblLastResult; state = OP; } else { pExpr->rightValue = g_dblLastResult; state = DONE; }; nIdx += 3; //Skip characters } else return NULL; } else { //Convert the value to a double and enter op state if (state == FIRST_VALUE) { pExpr->leftValue = atof(szBuffer); state = OP; } else { pExpr->rightValue = atof(szBuffer); state = DONE; }; }; break; }; case OP : { pExpr->op = GetOperator(ch); if (pExpr->op < 0) return NULL; //Return to value state state = SECOND_VALUE; //Reset temporary buffer memset(szBuffer, 0, sizeof(szBuffer)); pBuffer = szBuffer; ++nIdx; //Next character break; }; }; }; //Whatever is left is the second value pExpr->rightValue = atof(szBuffer); return pExpr; }; int main ( void ) { char buffer[100]; while(true) { printf("Enter an expression (i.e. 5 + 4). Previous answer is available as ANS: "); std::cin >> buffer; if (strcmpi(buffer, "Q") == 0) break; Expression* pExpr = ParseExpression(buffer); if (pExpr != NULL) { printf("%g\n", EvaluateExpression(pExpr)); delete pExpr; } else printf("Error parsing expression.\n"); }; return 0; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with project |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|