Thread: C Programming Question

  1. #1
    Unregistered
    Guest

    C Programming Question

    How do I both allow a user to input some formula such as X + 1 then allow myself to use that formula to produce some output such as x = 3; 3+1 =4; output: 4?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    Uh, that's nat that simple!

    You have to write a parser on your own. In the book "C++ Complete Reference", Herbert Schildt discribes how to write such a parser...
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Are you the same one who's posted this question 3 times already? Do something like the following to parse the string entered by the user then use your favorite switch statement to do the calculation.
    Code:
    int main(){
    	char array[] = "x+x*3", *p = &array[0];
    	char y = '5';
    
    	while(*p != '\0'){
    		if(*p == 'x')
    			*p = y;
    		p++;
    	}
    
    	puts(array);
    
    	return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM