【发布时间】:2022-01-19 15:04:17
【问题描述】:
我正在尝试编写一个代码,其中用户输入多项式的值,然后集成该多项式函数。我想知道如何保存用户输入的功能以使其正常工作。这是我目前用于输入多项式的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, power;
float x, polySum;
printf("Enter the order of the polynomial \n");
scanf("%d", &num);
printf("Enter the value of x \n");
scanf("%f", &x);
printf("Enter %d coefficients \n", num + 1);
for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
polySum = array[0];
for (i = 1; i <= num; i++)
{
polySum = polySum * x + array[i];
}
power = num;
printf("Given polynomial is: \n");
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
/* printing proper polynomial function */
if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
【问题讨论】:
标签: c integration polynomials numerical-integration