Thread: string to int

  1. #1
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    801

    string to int

    is there a way to scan a whole string that has a mathematical meaning, For example : ((4+5)*2) and transform it into a int or a float value, so if the user enters ((4+5)*2) it'll print 18.

    It might sound stupid, but i tried to scan it with %d, but as u al know it didn't work...so I tried to scan it with %s and then convert it to int, but did it work? of course not, why should it? it's me trying to get something done so, can anybody please help me!!!!

    thankx.
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: [email protected] []*[]

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    here's the zip, i may have put in some code
    it should compile fine (although it requires all arguements to be in the command line, in the first arguement, so put everything in quotes). not putting any arguements in causes a seg. fault
    Last edited by ygfperson; 05-03-2002 at 08:38 PM.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What you want to do is called parsing. You will either have to find some code online or make your own function. Here is a post where you can find some more information.

    http://www.cprogramming.com/cboard/s...hlight=parsing

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    801
    ygfperson, thankx for the code, It ran fine, But how do I use it, what kind of argument do I pass in it exactly when I run it from promt?

    thnkx
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: [email protected] []*[]

  5. #5
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    801
    thankx sean, I'll read the post and see if I can get any help from it!
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: [email protected] []*[]

  6. #6
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Ok, I found some code that will do it. This code will only evaluate integers though.
    Code:
    /* eval_expr.c */
    
    int	op_plus(int a,int b)
    {
       return (a+b);
    }
    
    int	op_moins(int a,int b)
    {
       return (a-b);
    }
    
    int	op_div(int a,int b)
    {
       return (a/b);
    }
    
    int	op_mul(int a,int b)
    {
       return (a*b);
    }
    
    int	op_mod(int a,int b)
    {
       return (a%b);
    }
    
    struct s_op 
    {
       char c;
       int  p;
       int  (*f)();
    } ;
    
    typedef	struct s_op	t_op;
    
    t_op	op [] =
    {
    {'+',1,op_plus},
    {'-',1,op_moins},
    {'/',2,op_div},
    {'*',2,op_mul},
    {'%',2,op_mod},
    {0,0,0}
    };
    
    int	find_last_op(char *str,int p)
    {
       int	pos;
       int	i;
       int	ret;
       int	par;
       
       ret = -2;
       par = 0;
       pos = strlen(str)-1;
       while (pos>=0)
       {
          if (str[pos] == ')')
    	 par ++;
          if (str[pos] == '(')
    	 par --;
          i = 0;
          while (!par && op[i].c)
          {
    	 if (op[i].c==str[pos])
    	 {
    	    ret = -1;
    	    if (op[i].p==p)
    	       return (pos);
    	 }
    	 i ++;
          }
          pos --;
       }
       return (ret);
    }
    
    int	is_nb(char *str)
    {
      if (*str=='(')
        {
           str[strlen(str)-1] = 0;
           return (eval_expr_sub(str+1,0));
        }
      return (atoi(str));
    }
    
    int	eval_expr_sub(char *str,int p)
    {
       int	pos;
       char	save;
       int	nb2;
       
       if ((pos = find_last_op(str,p))==-1)
          return (eval_expr_sub(str,p+1));
       if (pos==-2)
          return (is_nb(str));
       nb2 = eval_expr_sub(str+pos+1,p+1);
       save = str[pos];
       str[pos] = 0;
       pos = 0;
       while (op[pos].c)
       {
          if (save==op[pos].c)
    	 return (op[pos].f(eval_expr_sub(str,p),nb2));
          pos ++;
       }   
       printf("PB !!!\n");
       return (0);
    }
    
    
    int Evaluate(char *str)
    {
       return (eval_expr_sub(str, 0));
    }
    All you need to do is call the Evaluate function by passing it the string with what you want it to parse. It will return the value as an integer.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  7. #7
    Unregistered
    Guest
    Thankx sean, I really appreciate ur help, I'll try it when I get home. in these stupid librery computers, I can't do anything

  8. #8
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    801
    I posted the last massage, I guess I forgot to put the un and the pw
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: [email protected] []*[]

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Commander
    ygfperson, thankx for the code, It ran fine, But how do I use it, what kind of argument do I pass in it exactly when I run it from promt?

    thnkx
    in dos (and probably in linux too):
    Code:
    C:\...>eval "3* 460+(4^5*3)"
    evaluate(3* 460+(4^5*3)) returned 0
    eval = 4452.000000
    use the evaluate function, the main function is just to show what it can do.

  10. #10
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    801
    hey ygfperson,
    I tried to use the function as u said
    C:\...>eval "3* 460+(4^5*3)"
    evaluate(3* 460+(4^5*3)) returned 0
    eval = 4452.000000
    It gave me a error massage saying that the program has done some illigal thing.

    and sean, I tried to compile your code, in bc 5.02, it gave me some strange error massesges. I'll tell u what the error masses are exactly when I get home because i'm not allowed to do it in school.
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: [email protected] []*[]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM