#include <stdlib.h>
void main( )
{
double coe[20], x, sum = 0
int i, n
printf("请输入总项数: ")
scanf("%d", &n)
printf("请按指数从高到低的顺序输入各项系数: ")
for(i = ni >= 0i--)
scanf("%lf", &coe[i])
printf("请输入消培备变量x的值: ")
scanf("%lf", &x)
for(i = ni >= 0i--)
sum = sum * x + coe[i]
printf("中源\n结果为拿毁: %lf\n", sum)
}
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define EPS 1E-6
typedef struct item {
double coefficient
int power
struct item *next
} *POLYNOMIAL,*pItem
POLYNOMIAL Create() { // 创建多项式
pItem head,p
double coe
int pwr
head = p = (pItem)malloc(sizeof(item))
while(1) {
printf("系数 幂次(0 0结束) : ")
scanf("%lf%d",&coe,&pwr)
if(fabs(coe) <= EPS && !pwr) break
p->next = (pItem)malloc(sizeof(item))
p->next->coefficient = coe
p->next->power = pwr
p = p->next
}
p->next = NULL
return head
}
void Sort(POLYNOMIAL head) { // 按幂次降排序
pItem pt,q,p = head
while(p->next) {
q = p->next
while(q->next) {
if(p->next->power 枣郑< q->next->power) {
pt = p->next
p->next = q->next
q->next = p->next->next
p->next->next = pt
}
else q = q->next
}
p = p->next
}
}
void Show(POLYNOMIAL head) { // 显示多项式
POLYNOMIAL p = head->next
int flag = 1
if(p == NULL) return
while(p) {
if(flag) {
if(fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("%.2lf ",p->coefficient)
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("x ")
else if(p->coefficient == -1.0) printf("-x ")
else printf("%.2lfx ",p->coefficient)
}
else if(p->coefficient == 1.0) printf("x^%d ",p->power)
else if(p->coefficient == -1.0) printf("-x^%d ",p->power)
else printf("%.2lfx^%d ",p->coefficient,p->power)
flag = 0
}
}
else if(p->coefficient > 0.0 && fabs(p->coefficient) >档岩漏= EPS) {
if(p->power == 0) printf("+ %.2lf ",p->coefficient)
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("+ x ")
else printf("+ %.2lfx ",p->coefficient)
}
else if(p->coefficient == 1.0) printf("+ x^%d ",p->power)
else printf("+ %.2lfx^%d ",p->coefficient,p->power)
}
else if(p->coefficient < 0.0 && fabs(p->coefficient) >= EPS) 行烂{
if(p->power == 0) printf("- %.2lf ",-p->coefficient)
else if(p->power == 1) {
if(p->coefficient == -1.0) printf("- x ")
else printf("- %.2lfx ",-p->coefficient)
}
else if(p->coefficient == -1.0) printf("- x^%d ",p->power)
else printf("- %.2lfx^%d ",-p->coefficient,p->power)
}
p = p->next
}
printf("\n")
}
double Power(double x,int n) {
double value = 1.0
int i
for(i = 0 i < n ++i) value *= x
return value
}
double Value(POLYNOMIAL head,double x) { // 多项式求值
POLYNOMIAL p
double value = 0.0
for(p = head->next p p = p->next)
value += p->coefficient * Power(x,p->power)
return value
}
POLYNOMIAL Copy(POLYNOMIAL A) {
POLYNOMIAL head,t,p
head = t = (pItem)malloc(sizeof(item))
for(p = A->next p p = p->next) {
t->next = (pItem)malloc(sizeof(item))
t->next->coefficient = p->coefficient
t->next->power = p->power
t = t->next
}
t->next = NULL
return head
}
POLYNOMIAL Additive(POLYNOMIAL A, POLYNOMIAL B) { // 多项式相加
POLYNOMIAL head,p,q,t
head = Copy(A)
for(p = B p->next p = p->next) {
q = head
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient += p->next->coefficient
if(fabs(q->next->coefficient) <= EPS) {
t = q->next
q->next = t->next
free(t)
}
break
}
q = q->next
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(item))
q->next->coefficient = p->next->coefficient
q->next->power = p->next->power
q->next->next = NULL
}
}
Sort(head)
return head
}
POLYNOMIAL Subtract(POLYNOMIAL A, POLYNOMIAL B) { // 多项式相减
POLYNOMIAL head,p,q,t
head = Copy(A)
for(p = B p->next p = p->next) {
q = head
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient -= p->next->coefficient
if(fabs(q->next->coefficient) <= EPS) {
t = q->next
q->next = t->next
free(t)
}
break
}
q = q->next
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(item))
q->next->coefficient = -p->next->coefficient
q->next->power = p->next->power
q->next->next = NULL
}
}
Sort(head)
return head
}
POLYNOMIAL Multiplication(POLYNOMIAL A, POLYNOMIAL B) { // 多项式相乘
POLYNOMIAL head,t,p,q
head = t = (pItem)malloc(sizeof(item))
for(p = A->next p p = p->next) { // 完成相乘过程
for(q = B->next q q = q->next) {
t->next = (pItem)malloc(sizeof(item))
t->next->coefficient = p->coefficient * q->coefficient
t->next->power = p->power + q->power
t = t->next
}
}
t->next = NULL
Sort(head) // 排序
p = head
while(p->next) { // 合并同类项
q = p->next
while(q->next) {
if(p->next->power == q->next->power) {
p->next->coefficient += q->next->coefficient
t = q->next
q->next = t->next
free(t)
}
else q = q->next
}
p = p->next
}
return head
}
void FreeMemory(POLYNOMIAL head) {
POLYNOMIAL q,p = head
while(p) {
q = p
p = q->next
free(q)
}
}
int main() {
printf("创建多项式A:\n")
POLYNOMIAL A = Create()
Sort(A)
printf("A(x) = ")Show(A)
printf("创建多项式B:\n")
POLYNOMIAL B = Create()
Sort(B)
printf("B(x) = ")Show(B)
POLYNOMIAL C = Additive(A,B)
printf("C(x) = ")Show(C)
POLYNOMIAL D = Subtract(A,B)
printf("D(x) = ")Show(D)
POLYNOMIAL E = Multiplication(A,B)
printf("E(x) = ")Show(E)
printf("A(%.2lf) = %.4lf\n",2.0,Value(A,2.0))
printf("B(%.2lf) = %.4lf\n",2.0,Value(B,2.0))
printf("C(%.2lf) = %.4lf\n",2.0,Value(C,2.0))
printf("D(%.2lf) = %.4lf\n",2.0,Value(D,2.0))
printf("E(%.2lf) = %.4lf\n",2.0,Value(E,2.0))
FreeMemory(A)
FreeMemory(B)
FreeMemory(C)
FreeMemory(D)
FreeMemory(E)
return 0
}
#include <stdio.h>double poly(double x, double a, double b, double c) { // 计算多项式
return a * x * x * x + b * x * x + c
}
int main() {
double x, a, b, c// 定义变量x、a、b、c
printf("请余扰洞输入x的值:")
scanf("%lf", &x)// 从键盘读入竖枯x的值
printf("请输入多项式系数a、b、c的值:")
scanf("%lf %lf %lf", &a, &b, &c)// 从键盘读入多项式系数的值
double result = poly(x, a, b, c)// 调李芦用函数计算多项式的值
printf("当x=%.2f时,%.2fx^3+%.2fx^2+%.2f的值为:%.2f\n", x, a, b, c, result)
return 0
}
望采纳,谢谢。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)