多项式计算【C】
数据结构课的作业,昨晚写完的,说是小组作业实则基本上就是我自己一个人写……其他人根本就不会啥#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct Node{
float coef; // 系数
int exp; // 指数
struct Node *next;
}Node, *Polynomial;
// 创建新节点
Node* createNode(float coef, int exp){
Node *node = (Node*)malloc(sizeof(Node));
node->coef = coef;
node->exp = exp;
node->next = NULL;
return node;
}
// 创建多项式
void createPolynomial(Polynomial *p){
int n;
float coef;
int exp;
printf("请输入待插入多项式的项数:");
scanf("%d", &n);
for(int i=0; i<n; i++){
printf("请插入新第%d项的系数和指数:(按降幂输入)", i+1);
scanf("%f %d", &coef, &exp);
if(coef != 0){
Node *node = createNode(coef, exp);
if(*p == NULL){
*p = node;
}else{
Node *last = *p;
while(last->next != NULL){
last = last->next;
}
last->next = node;
}
}
else
{
printf("系数不应为0!\n");
}
}
}
// 输出多项式
void printPolynomial(Polynomial p){
if(p == NULL){
printf("0\n");
}else{
while(p != NULL){
printf("%.2f", p->coef);
if(p->exp != 0){
printf("x^%d", p->exp);
}
if(p->next != NULL && p->next->coef > 0){
printf("+");
}
p = p->next;
}
printf("\n");
}
}
//多项式赋值
void assignPolynomial(Polynomial *a, Polynomial b){
Node *p = NULL;
Node *q = b;
//int k;
// 如果b为空链表,直接返回
if (q == NULL) {
printf("赋值多项式为空!\n");
return;
}
// 如果a不为空链表,先删除a的所有节点
while(*a != NULL){
Node *temp = *a;
*a = (*a)->next;
free(temp);
}
/*k = (p == NULL);
printf("%d\n",k);*/
// 复制b的所有节点到a
while(q != NULL){
Node *node = createNode(q->coef, q->exp);
if(p == NULL){
//printf("成功");
*a = node;
p = *a;
}else{
p->next = node;
p = p->next;
}
q = q->next;
}
}
// 多项式相加
Polynomial addPolynomial(Polynomial a, Polynomial b){
Node *pa = a;
Node *pb = b;
Polynomial c = NULL, pc = NULL;
while(pa != NULL && pb != NULL){
if(pa->exp > pb->exp){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
}else if(pa->exp < pb->exp){
if(pb->coef != 0){
Node *node = createNode(pb->coef, pb->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pb = pb->next;
}else{
float coef_sum = pa->coef + pb->coef;
if(coef_sum != 0){
Node *node = createNode(coef_sum, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
pb = pb->next;
}
}
while(pa != NULL){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
pc->next = node;
pc = pc->next;
}
pa = pa->next;
}
while(pb != NULL){
if(pb->coef != 0){
Node *node = createNode(pb->coef, pb->exp);
pc->next = node;
pc = pc->next;
}
pb = pb->next;
}
return c;
}
// 多项式相减
Polynomial subPolynomial(Polynomial a, Polynomial b){
Node *pa = a;
Node *pb = b;
Polynomial c = NULL, pc = NULL;
while(pa != NULL && pb != NULL){
if(pa->exp > pb->exp){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
}else if(pa->exp < pb->exp){
if(-pb->coef != 0){
Node *node = createNode(-pb->coef, pb->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pb = pb->next;
}else{
float coef_diff = pa->coef - pb->coef;
if(coef_diff != 0){
Node *node = createNode(coef_diff, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
pb = pb->next;
}
}
while(pa != NULL){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
pc->next = node;
pc = pc->next;
}
pa = pa->next;
}
while(pb != NULL){
if(-pb->coef != 0){
Node *node = createNode(-pb->coef, pb->exp);
pc->next = node;
pc = pc->next;
}
pb = pb->next;
}
return c;
}
// 多项式求值
float evalPolynomial(Polynomial p, float x){
float sum = 0;
while(p != NULL){
float term = p->coef * pow(x, p->exp);
sum += term;
p = p->next;
}
return sum;
}
// 多项式求导
Polynomial derPolynomial(Polynomial p){
Node *q = NULL, *last = NULL;
while(p != NULL){
if(p->exp > 0){
float coef_der = p->coef * p->exp;
Node *node = createNode(coef_der, p->exp-1);
if(q == NULL){
q = node;
last = q;
}else{
last->next = node;
last = last->next;
}
}
p = p->next;
}
return q;
}
//更新多项式
void updatePolynomial(Polynomial *p)
{
while(*p != NULL){
Node *temp = *p;
*p = (*p)->next;
free(temp);
}
int n;
float coef;
int exp;
printf("请输入待插入多项式的项数:");
scanf("%d", &n);
for(int i=0; i<n; i++){
printf("请插入新第%d项的系数和指数:(按降幂输入)", i+1);
scanf("%f %d", &coef, &exp);
if(coef != 0){
Node *node = createNode(coef, exp);
if(*p == NULL){
*p = node;
}else{
Node *last = *p;
while(last->next != NULL){
last = last->next;
}
last->next = node;
}
}
else
{
printf("系数不应为0!\n");
}
}
}
int main(){
Polynomial A = NULL, B = NULL;
int choice;
float x;
while(1){
printf("请选择操作:\n");
printf("1.向多项式A插入\t\t");
printf("2.向多项式B插入\n");
printf("3.输出多项式A\t\t");
printf("4.输出多项式B\n");
printf("5.将A赋值给B\t\t");
printf("6.将B赋值给A\n");
printf("7.计算A+B\t\t");
printf("8.计算A-B\n");
printf("9.计算多项式的值\n");
printf("10.求多项式的导数\n");
printf("11.更新多项式A\t\t");
printf("12.更新多项式B\n");
printf("13.退出\n");
scanf("%d", &choice);
switch(choice){
case 1:
createPolynomial(&A);
break;
case 2:
createPolynomial(&B);
break;
case 3:
printf("A = ");
printPolynomial(A);
break;
case 4:
printf("B = ");
printPolynomial(B);
break;
case 5:
assignPolynomial(&B, A);
/*if (B==NULL){
assignPolynomial(&B,A);
}*/
break;
case 6:
assignPolynomial(&A, B);
/*if (A==NULL){
assignPolynomial(&A,B);
}*/
break;
case 7:
printf("A + B = ");
printPolynomial(addPolynomial(A, B));
break;
case 8:
printf("A - B = ");
printPolynomial(subPolynomial(A, B));
break;
case 9:
printf("请输入x的值:");
scanf("%f", &x);
printf("A(%g) = %g\n", x, evalPolynomial(A, x));
printf("B(%g) = %g\n", x, evalPolynomial(B, x));
break;
case 10:
printf("A' = ");
printPolynomial(derPolynomial(A));
printf("B' = ");
printPolynomial(derPolynomial(B));
break;
case 11:
updatePolynomial(&A);
break;
case 12:
updatePolynomial(&B);
break;
case 13:
return 0;
default:
printf("无效操作,请重新选择。\n");
}
}
return 0;
}
这段代码是一个多项式计算器,可以实现多项式的创建、输出、赋值、相加、相减、求值、求导等功能。主要使用了链表来存储多项式的每一项。
首先,定义了一个结构体Node表示多项式中的每一项,包含系数和指数两个成员变量,同时声明了typedef别名Polynomial表示指向Node结构体的指针。
然后,实现了一些基本的操作函数,如createNode用于创建一个新节点,createPolynomial用于创建多项式,printPolynomial用于输出多项式,assignPolynomial用于将一个多项式赋值给另一个多项式,addPolynomial和subPolynomial分别用于多项式的相加和相减,evalPolynomial用于求多项式在某个特定值下的值,derPolynomial用于求多项式的导数,updatePolynomial用于更新多项式。
最后,在main函数中实现了一个简单的交互界面,通过输入选择操作,调用相应的函数进行处理。
这段代码中的注释已经很详细了,如果你有具体的问题,可以提出来让我帮你解答。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 {:10_256:}{:10_256:}{:10_256:}我记得我之前用了 Python 写了一个类似的丐版 这个你写了多久?
熟练度多少?
自学还是有人教? 六翻了 发表于 2023-11-24 11:10
这个你写了多久?
熟练度多少?
自学还是有人教?
一个礼拜断断续续,熟练度,其实没有什么高级东西,就是简单的代码堆起来的,至于你最后那个问题,这是我们的作业……
页:
[1]