Input polynomial, convert to function.

This is a discussion on Input polynomial, convert to function. within the C Programming forums, part of the General Programming Boards category; I have yet another question. I want to input a polynomial of the form: a_1x^n + a_2x^(n-1) +...+ a_(n-1)x^2 + ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    Input polynomial, convert to function.

    I have yet another question.

    I want to input a polynomial of the form:

    a_1x^n + a_2x^(n-1) +...+ a_(n-1)x^2 + a_nx + d

    as a string of characters and convert this to a function. (n are integers, a,d are floats.)

    For individual terms, I thought I could do something like:

    1.) Check for the character 'x' in the term.

    2.) IF 'x' is present in term
    THEN do 3.)
    ELSE return constant (d)

    3.) IF 'x' is preceeded by '^'
    THEN declare a new integer and set it equal to the number succeeding '^', declare a float and set it equal to the number precceding x, and calculate a*x^n for this term.
    ELSE declare a float and set it equal to the number precceding x, and calculate a*x for this term.

    I would also first have to do something to check for +/- characters, and then identify each of the terms before applying 1.)-3.) on them.

    The main problem is how to do the checking of sucedding/preceeding characters/strings in the main inputted char variable. Is this even possible in c, or does it go back to my previous thread where we have to use windows/linux specific functions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Read the entire line in into a buffer. That way, you can look at the "before" and "after" of what you are at right now. No need for system specific functions here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It might be easier to do what some hand held scientific calculators do: just read in the coefficients and the exponents.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    It might be easier to do what some hand held scientific calculators do: just read in the coefficients and the exponents.
    But not half the challenge!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by laserlight View Post
    It might be easier to do what some hand held scientific calculators do: just read in the coefficients and the exponents.

    Yeh I thought about that, but as matsp said it's too easy like that. I was aiming at entering the whole string just for completion/user friendliness, and of course a challenge.

    matsp: How can I read the string into a buffer? Do I use some alternative to scanf() to read in the char? If you have some information to point me towards that would be great (baring in mind I'm not that experienced). Cheers

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd suggest using fgets(), as that is a function specifically written to read a line of text. You also get no problems with for example spaces or very long inputs using fgets() - it will not accept more data than you specify.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Tahnks, I will do some reading

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    For the code:

    Code:
    char polynomial;
    char fgets(char polynomial, 1000, stdin);
    printf("%c", polynomial);
    I tried to compile it and had a load of errors that I didn't understand.

    I thought this code should read in a char from the input stream with no more than 1000 characters, store it in a buffer in the char 'polynomial', and then print it back out again.

    Is my syntax wrong or the use of the function?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Is my syntax wrong or the use of the function?
    Both, I think. You are declaring a function named fgets on the second line when you actually want to call it. But if polynomial is a char instead of a string, then perhaps you would like to use fgetc instead of fgets. However, by saying that you want to read up to 1000 characters, then perhaps you should use a string, not a char.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by laserlight View Post
    Both, I think. You are declaring a function named fgets on the second line when you actually want to call it. But if polynomial is a char instead of a string, then perhaps you would like to use fgetc instead of fgets. However, by saying that you want to read up to 1000 characters, then perhaps you should use a string, not a char.
    Ah ok, I actually want to declare it as a string...so I can input a polynomial of arbitrary length like above. our lecturer hasn't taught us about strings, so I don't know how to declare them or reference to them. Would it be something like strng polynomial and %s?

    yeh I want to read up to 1000 characters, but the polynomial could be of arbitrary length within this limit. (I though 1000 was a pretty safe value)

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    in C, a string is just an array of characters, so char polynomial[1000] would be the right way to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Ok, thanks I've found a pretty good page about character arrays/strings

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    How can you compare the stored chars from the string with other characters and numbers?
    For characters can you just use "character" in quotes like a constant string? I have this code so far to compare the first character of the entered string againts the character "-", to see whether the first co-efficient is negative or mnot, and declare it:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    char polynomial[1000];
    fgets(polynomial,1000,stdin);
    
    if (polynomial[0]="-")
    	{
    	int a_1=-1*polynomial[1];
    	}
    	else
    	{
    	int a_1=polynomial[0];
    	}
    
    
    return (0);
    }
    so I'm assuming the first co-efficient is a single digit integer, but this is to check whether this integer is negative or not and then declare it.

    I'm also unsure whether I can return for example "5" as a character from the string, and perform arithmetic on it, or whether I need to use te ASCII value for the character....i.e. whether if polynomial[0]="5", that polynomial[0]=5....if you see what I mean.

    Also could, you check my use of fgets again? I think I have it right this time.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    = is assignment. == is comparison. Characters live in single quotes.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    I changed the code to:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    printf("input:")
    
    char polynomial[1000];
    fgets(polynomial,1000,stdin);
    
    if (polynomial[0]=='-')
    	{
    	int a_1=-1*polynomial[1];
    	}
    	else
    	{
    	int a_1=polynomial[0];
    	}
    
    printf("%dx^2",a_1)
    
    return (0);
    }
    and it's reading "2" as the number 50, for e.g. I got:

    in:2X^2
    out:50x^2

    in:-2x^2
    out:-50x^2

    in:4x^2
    out:52x^2

    in:-4x^2
    out:-52x^2

    Is this because the ASCII number for "2" is 50? I thought the ASCII values for the characters 0-9, were simply 0-9 respectively, and what I've read confirms this, so why does it return 50 for 2 and 52 for 4?

    and it actually compiled this time, but when I input "2x^2" or "-2x^2" to test it, there was no output,

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21