Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. I want to make a code that user could make a input about coefficients directly, not the program maker just decides the coefficients. For example, if user puts 5 0 1 5 2, it means 5x^4+x^2+5x+2. And I guess I can make this code using fgets and strtok, but I'm not sure how to do this specifically.

2. And after I do the calculation(add) on polynomial,there would be 3 polynomials. First and second polynomial are the ones that user made an input. And the third one is the result of adding those polynomials. Then, I want to put the specific value on x and get the final result. Let's suppose there's a second polynomial 5x^4+x^2+5x+2. Then if user make a input like '2 3', this means the program should make a evaluation on the 2nd polynomial, and we have to put 3 on x. How can I make this?

Both questions are related to the users' input. I want to know it very exactly.

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>  
#include <math.h>
#include <string.h>
#define MAX(a,b) ((a)>(b))?(a):(b) 
#define MAX_DEGREE 500
#define _CRT_SECURE_NO_WARNINGS
#pragma warning (disable:4996)

typedef struct {           
	int degree;             
	int* coef; 
}polynomial;   

polynomial addPoly(polynomial* A, polynomial* B, polynomial* C) { 
	int Apos = 0, Bpos = 0, Cpos = 0;   
	int degree_a = A->degree;
	int degree_b = B->degree;
	C->degree = MAX(A->degree, B->degree); 

	while (Apos <= (A->degree) && Bpos <= (B->degree)) {  
		if (degree_a > degree_b) {         
			C->coef[Cpos++] = A->coef[Apos++];
			degree_a--;
		}
		else if (degree_a == degree_b) {    
			C->coef[Cpos++] = A->coef[Apos++] + B->coef[Bpos++];
			degree_a--; degree_b--;
		}
		else {    
			C->coef[Cpos++] = B->coef[Bpos++];
			degree_b--;
		}
	}
	return *C;
}

void printPoly(polynomial* p) {
	for (int i = p->degree; i > 0; i--)
		printf("%dx^%d + ", p->coef[p->degree - i], i);
	printf("%d\n", p->coef[p->degree]);
}

int main(void) {

}
Posted
Updated 24-Mar-21 5:47am
v4

You can get the input values by:
C++
char text[132];
int values[5];
fgets(text, 132, stdin);
char* pNext = strtok(text, " \t\n");
for (int i = 0; i < 5; i++)
{
    values[i] = atoi(pNext);
    pNext = strtok(NULL, " \t\n");
    if (pNext == NULL)
        break; // not enough numbers
}
for (int i = 0; i < 5; i++)
{
    printf("number %d: %d\n", i, values[i]);
}

This is over simplified code and has no error checks so you will need to add that yourself.
 
Share this answer
 
Comments
anonymous thisis 24-Mar-21 11:08am    
Thank you for your solution. But, I'd really wanted to figure it out related to the polynomial program. I understand that I have to make a code like you've uploaded, but I still don't know how to apply it to this coefficients and degree of the polynomial. And also to the input value on x.
Richard MacCutchan 24-Mar-21 11:43am    
Sorry, that is a mathematics question, not a programming one.
anonymous thisis 24-Mar-21 13:06pm    
Okay, anyway thank you !
Richard MacCutchan 24-Mar-21 13:21pm    
Take a look at Polynomials[^], as it shows that just entering a set of numbers will not help. You need to enter it as an actual formula followed by the values of the variables.
anonymous thisis 25-Mar-21 9:20am    
Okay, thank you very much
It is best to separate program functionality. The code Richard gave you will accept input and process it into an array of five integers. These are the coefficients in your program. The value five could be anything else you want - it corresponds to the degree in your program. You could accept that value as the initial input. Even better would be to make a function to accept all inputs needed for each polynomial.

One step you seem to be missing is the polynomial evaluation. Like the input function I suggested, you could make a function that accepts a polynomial structure and a value of X and generates a result.

If I were you, I would tweak the input function to accept a file pointer so you can read input from a text file. That lets you work much faster than if you have to input numbers every time. It can read the file and process the same set of inputs every time or it can process any file of inputs you want it to, including from stdin which is really just another file pointer.

Here's how that might look :
C++
#define INPUT_SIZE = 127

int GetCoefficients( polynomial * ppoly, int degree, FILE * fin )
{
    const char * delimiters = " \t\r\n";
    char buffer[ INPUT_SIZE+1 ] = { 0 };
    char * intext = buffer;
    char * pnext = NULL;
    int i = 0;

    // allocate memory for the coefficients

    ppoly->coef = (int *) calloc( degree + 1, sizeof( int ) );

    // read text from the input file

    fgets( text, INPUT_SIZE, fin );

    // process the input

    for( i = 0; i <= degree; i++ )
    {
        pnext = strtok( intext, delims );
        if( pnext == NULL )
            return 0;       // not enough numbers

        // save the input value

        ppoly->coef[ i ] = atoi( pnext );
        intext = NULL;   // set input to null to continue reading from buffer
    }

    for( i = 0; i <= degree; i++ )
    {
        printf("coefficient %d is %d\n", i, ppoly->coef[ i ] );
    }

    return degree + 1;
}
Note the <= on the loops. That's because I think you need one more coefficient than the degree of the polynomial to account for the constant factor which is actually the coefficient for x raised to the zero power.

This function takes degree as an input parameter. It can be acquired exactly like how the input is read here.
 
Share this answer
 
v2
Comments
Rick York 24-Mar-21 12:37pm    
Oops - I noticed an error. The call to strtok should use delimiters.
anonymous thisis 24-Mar-21 13:14pm    
Hmm I have one more question. You wrote 'degree' as a Hmm I have one more question. You wrote 'degree' when defining a function. Then, I think it means user should put both coefficients and degree. But, if I just want user to put only coefficients not the degree, how can I do?
I tried like this. I just defined int degree= 0; at the next line not at the first line when making a function. But it seems it doesn't work. Can you tell me the solutions?
Rick York 24-Mar-21 13:25pm    
The degree parameter has to be accepted from the user or you define it to something like 3 or 4. Either way, it is passed to that function. Calling fgets and atoi can get the value from the user or a file. The thing is, if you are going to accept coefficients for three different polynomials, I would assume degree needs to be the same for all of them so it needs to be acquired first. Again, all of this data can be read from a file. Call fopen to open a text file and pass it to the function or pass stdin to the function and get data from the user.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900