Thread: string to source(?)

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    Question string to source(?)

    I'm trying to make a simple graphing program.
    Code:
    for(x=-10;x<11;x++)
      {
       y=(-(x*x))+4;
       putpixel(buffer,x+320,y+240,(255,255,255));
       blit(buffer,screen,0,0,0,0,640,480);
       delay(100);
      }
    I want to take this piece of code and change it from graphing hard-coded functions into a function that will take an equation from the user and graph that. My only problem is that I can't think of how to use a string and then have the program read it like it was hard-coded. Am I using the wrong approach?

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I'm not entirely sure what you mean. Data can be assigned to variables, you then use these variables as part of your equations.

    The data in these variables can be read from users by a number of means, if this is what you're having trouble with take a look at the 'fgets' function. The function 'atoi' may also be helpful.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    What kind of euations?
    You can limit it to simple equations that contains no brackets and only +,-,*,/.
    That should not be very difficult.

    Try this code. But it can only process simple equations. numbers can only be one digit. e.g. 6*x+4 or x*x-5
    Code:
    #include <stdio.h>
    
    int main() {
        char input[80];
        char temp[10];
        int i,j;
        char op;
        double y,x;
        
        printf("Input formula: y(x)=");
        fgets(input, 80, stdin);
        do {
        printf("Input x(0-9): ");
        fgets(temp, 10, stdin);
        sscanf(temp,"%lf",&x);
        } while (x<0 || x>9);
        y=0;
        j=0;
        op='+';
        for(i=0; input[i]!='\0'; i++) {
            if(input[i]=='+' || input[i]=='-' || input[i]=='*' || input[i]=='/') {
                op=input[i];
                i++;
            }
            do {
                if(input[i]=='\0') break;
                if(input[i]=='x') j=x;
                else j=input[i]-'0';
                if(j<0 || j>9) i++;
            } while(j<0 || j>9);
            if(input[i]=='\0') break;
            switch(op) {
                case '+': y+=j; break;
                case '-': y-=j; break;
                case '*': y*=j; break;
                case '/': y/=j; break;
            }
        }
        printf("\ny = %lf", y);
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    You cannot turn string to source, because your program does not have a built in compiler .

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    You could always use another language that has this functionality built in:
    Code:
    #! /usr/bin/perl
    
    # Simple simple evaluate and filter
    chomp($expr = <STDIN>);
    print eval $expr;
    Then you can pipe the output of this script through your C program, use atoi on the string you get, and you have an evaluated expression with way less effort than it would take to write it in C.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    19
    I was trying to do the same thing sometime ago. If you want to graph arbitrary functions, you'll have to "parse" the user-given expression. If all you want are simple equations like just +,-,*,/ etc., then you can just hand-code something like in the posting above. Hand-coding gets harder as you try to parse more complex expressions. One way out is to use yacc (or GNU bison), which will handle the parsing for you. In fact, you'll be writing a compiler-like program for a tiny language.

    You should be able to find details about the different methods of parsing and about yacc in any compiler design book. If you want the definitive reference, get the "Dragon Book" -- "Principles of Compiler Design", Aho, et. al. You can also try the GNU bison info pages.

    HTH

    Update: Oh, and also the last chapter of "The Unix Programming Environment" by Rob Pike and Kernighan(?) is about the design of a compiler using yacc for a 4-function calculator that grows into a small programming language by the end of the chapter.
    Last edited by vikasgp; 10-20-2003 at 07:25 AM.

  7. #7
    root
    Join Date
    Sep 2003
    Posts
    232
    >Hand-coding gets harder as you try to parse more complex expressions.
    If you do it in C that is. Perl's eval can parse any valid Perl expression (and any mathematical expression that this program would require). So why bother reinventing things that have already been created for no reason? I don't know what OS Draco uses or I would have suggested bc/dc instead of a relatively portable Perl solution.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  8. #8
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I use windowsxp. Thanks for the code DrZoidBerg. I like twm's suggestion too, but could you please explain to me where I would use the code segment, and what it accomplishes in the piping?

  9. #9
    root
    Join Date
    Sep 2003
    Posts
    232
    >I like twm's suggestion too
    You like me, you really like me!

    >could you please explain to me where I would use the code segment
    It's not a segment my young Perl patawan, it's a complete program. What you need to do is call it from your C program like so (for me, for you it may be different because I use UNIX and everyone here seems to like Windows):
    Code:
    /* Includes */
    
    /* Some shtuff */
            int  result;
            char sresult[80];
            FILE *pipey = popen("eva", "r"); /* I called the script eva, deal */
    
            fgets(sresult, 80, pipey);
            result = atoi(sresult);
            /* Tada! You have the result of an expression. I hope...I haven't tested it,
               but if I got lucky then everything I've said is true. :) */
    >and what it accomplishes in the piping?
    Well, piping in this case is the easiest thing to do. Since writing an expression parser in C is a mite complex, the coolness of my two line Perl script is worth a little PVC in your program. If you're using Windows, I think you still may have popen in some shape or form.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks, that clears things up. And so you know, it's padawan

  11. #11
    root
    Join Date
    Sep 2003
    Posts
    232
    >And so you know, it's padawan
    I uh...knew that...yea.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM