# 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. ## 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. 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

3. It might be easier to do what some hand held scientific calculators do: just read in the coefficients and the exponents.

4. Originally Posted by laserlight
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

5. Originally Posted by laserlight
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. 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

7. Tahnks, I will do some reading

8. 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. 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.

10. Originally Posted by laserlight
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. in C, a string is just an array of characters, so char polynomial[1000] would be the right way to do that.

--
Mats

12. Ok, thanks I've found a pretty good page about character arrays/strings

13. 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. = is assignment. == is comparison. Characters live in single quotes.

15. 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 Last