Go Back   EQEmulator Home > EQEmulator Forums > Archives > Archive::Development > Archive::Development

Archive::Development Archive area for Development's posts that were moved here after an inactivity period of 90 days.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-17-2003, 12:58 AM
alkrun
Sarnak
 
Join Date: Jan 2002
Posts: 66
Default 5.0 fixes / changes

I figured I'd make one thread for things I fix / change, this was just the first significant change. If I understood what calc() was supposed to do in parser.cpp (that is, calculate a simple math expression in a string) then it had a few problems. I rewrote it, see below:

Code:
int calc( string calc )
{
	string::iterator iterator = calc.begin();
	string buffer;
	string integer1;
	char op = 0;
	int returnvalue = 0;
	
	while (*iterator)
	{
		char ch = *iterator;
		
		if(ch >= '0' && ch <= '9')//If this character is numeric add it to the buffer
		{
			buffer += ch;
		}
		else if(ch == '+' || ch == '-' || ch == '/' || ch == '*')//otherwise, are we an operator?
		{
			int val = atoi(buffer.c_str());
			if(!op)//if this is the first time through, set returnvalue to what we have so far
			{
				returnvalue = val;
			}
			else//otherwise we've got returnvalue initialized, perform operation on previous numbers
			{
				if(buffer.length() == 0)//Do we have a value?
				{
					printf("Parser::calc() Error in syntax: '%s'.\n", calc);
					return 0;
				}
				//what was the previous op
				switch(op)
				{
				case '+':
					{
						returnvalue += val;
						break;
					}
				case '-':
					{
						returnvalue -= val;
						break;
					}
				case '/':
					{
						if(val == 0)//can't divide by zero
						{
							printf("Parser::calc() Error, Divide by zero '%s'.\n", calc);
							return 0;
						}
						returnvalue /= val;
						break;
					}
				case '*':
					{
						returnvalue *= val;
						break;
					}
				};
				op = ch;//save current operator and continue parsing
				buffer.clear();//clear buffer now that we're starting on a new number
			}
			op = ch;
		}
		else
		{
			printf("Parser::calc() Error processing '%c'.\n", ch);
			return 0;
		}
	}
	return returnvalue;
}
Reply With Quote
 

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 12:25 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3