Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Random Data in String

string

  • Please log in to reply
3 replies to this topic

#1 Shokora

Shokora

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 September 2007 - 11:16 AM

I'm building a parser for mathematical functions, i'm still at the very basic but something has gone wrong and i can't find the solution. If i compile the code and i use the function 100+100 as input, it gives me this output:

$ ./test.exe
Enter your function please: 100+100
function[2] is numeric: 0
function[1] is numeric: 0
function[0] is numeric: 1
function[0] to function[2] is the first argument
function[4] is numeric: 1
function[5] is numeric: 0
function[6] is numeric: 0
function[4] to function[6] is the second argument
arg1 == 10↕aDË" && arg2 == 100

Please help me, because i'm really growing desperate.

Btw, i know the things like the function array aren't ideal, but i needed to test the principle before i was really going to make it.

//second mathematical function parser test
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PLUS 43

int main()
{
    char function[200];
    int i, scan1, scan2;
    char arg1[200], arg2[200];
    
    printf("Enter your function please: ");
    fgets(function,sizeof(function)-1,stdin);
    
    for(i=0;i<strlen(function);i++)
    {
        //If function[i] is a + operator
        if(function[i] == PLUS)
        {
            //Scan the string for numbers (arguments)
            for(scan1=i-1;scan1>=0;scan1--)
            {
                /*If function[i] is not a numeric value, then break because
                you are at the end of the argument*/
                if(function[scan1] < 48  || function[scan1] > 58)
                {
                    break;
                }
                printf("function[%d] is numeric: %d \n",scan1,(function[scan1] - 48));
            }
            printf("function[%d] to function[%d] is the first argument\n",scan1,(i-1));
            //because the loop still does scan1-- one more time
            scan1++;
            
            //scan the string for the second argument
            for(scan2=i+1;scan2<strlen(function);scan2++)
            {
                if(function[scan2] < 48 || function[scan2] > 58)
                {
                    break;
                }
                printf("function[%d] is numeric: %d \n",scan2,(function[scan2] - 48));
            }
            //because the loop still does scan2++ one more time
            scan2--;
            printf("function[%d] to function[%d] is the second argument\n",(i+1),scan2);
            
            //copy them to a buffer of their own
            strncpy(arg1,function+scan1,(i-1));
            strncpy(arg2,function+(i+1),scan2);
            
            printf("arg1 == %s && arg2 == %s",arg1,arg2);
            
        }
        
    }
    
    return 0;
}

  • 0

#2 felixme86

felixme86

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 27 September 2007 - 11:48 AM

If this project is going to be reused you need to reorganize your code or you will become hopelessly lost. you need to tokenize your data before you can do anything else. you have the right idea but you need to make it more generic and organized.

//second mathematical function parser test
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define PLUS 43
#define FunctionSize 200

int parsenumber(char *function, int start)
{
	for(int i=start;i<FunctionSize;i++)
	{
		if(function[scan1] < 48  || function[scan1] > 58)
		{
			return i-1;
		}
	}
}

int main()
{
    char function[FunctionSize];
    int i, scan1, scan2;
    char arg1[FunctionSize], arg2[FunctionSize];
    
    printf("Enter your function please: ");
    fgets(function,sizeof(function)-1,stdin);

	// get the first digit
	int StartofNumberOne, EndofNumberOne,
		StartofNumberTwo, EndofNumberTwo;
	StartofNumberOne=0;
	EndofNumberOne=parsenumber(function,StartofNumberOne);
	StartofNumberTwo=EndofNumberOne+2;
	EndofNumberTwo=parsenumber(function,StartofNumberTwo);

  • 0

#3 Shokora

Shokora

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 September 2007 - 12:06 PM

thanks i'll try to be more consistent in the future.

I solved my problem, I accidently didn't include string.h and strncpy(); so happens to need it... T_T
  • 0

#4 kkelly

kkelly

    CC Regular

  • Just Joined
  • PipPipPip
  • 46 posts

Posted 27 September 2007 - 12:33 PM

I think in this case the devil is in the details. Take a look at it from a more abstract perspective. In particular, to build a parser, you'll want to learn about Finite State Machines. Then on to Parsers and Lexical Analyzers. For a beginning programmer, it may prove difficult to grasp, but I think these concepts are what you are struggling with.
Once you've looked that material over you should start with some pseudo-code.
Also, I suggest staying away from string functions and sizeof() at all costs. It's the easiest way to avoid buffer overflow issues.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download