C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 14th, 2006, 08:21 PM
pr0jectx pr0jectx is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Posts: 1 pr0jectx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 20 sec
Reputation Power: 0
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;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with project


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT