被一道题难住了。。。

cookieshi 2007-06-29 09:50:34
很久没碰C了,今天一个朋友发了一道题给我。发现自己真是把C忘干净了。

特来求助。不甚感激。

==============================
多项式计算
1.设计要求:
具备一元多项式加减乘除的功能.(多项式系数的输入和输出均按高到低排列形式安排系数.最右面的为零次项系数,从又向左依次增加.)
列如:多项式加法
输入:2 -5 0 7(表示多项式2x^3-5x^2+7)
3 1 -8 (表示多项式3x^2+x-8)
输出2 -2 1 -1
(2x^3-5x^2+7)+(3x^2+x-8)=2x^3-2x^2+x-1
输入的数据项数是不定长的,最多不超过10项,用菜单让用户选择运算类别
菜单格式如下:
1.多项式的加法
2.多项式的减法
3.多项式的乘法
4.多项式的除法
5.退出
=============================
...全文
308 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlbdan 2007-06-30
  • 打赏
  • 举报
回复
百度上搜吧,很多的
jixingzhong 2007-06-30
  • 打赏
  • 举报
回复
void MultiplyProcess(PolyPointer polyBuff, const int iPolyNum,
PolyPointer pResult)
{
int iCounter = 1, jCounter = 0, iLength; /* 缓冲区的长度 */
PolyPointer pTempBuff = NULL; /* 存放中间结果的缓冲区 */
polynomial pCurrNode_1, pCurrNode_2, pNewNode = NULL;

/* 初始化 */
pCurrNode_1 = polyBuff[0];
iLength = PolynLength(polyBuff[0]);
MakePolyBuff(&pTempBuff, iLength);
while (TRUE)
{
while (pCurrNode_1 != NULL)
{
pCurrNode_2 = polyBuff[iCounter];
while (pCurrNode_2 != NULL)
{
MakeNode(&pNewNode,
pCurrNode_1->fCoeff * pCurrNode_2->fCoeff,
pCurrNode_1->iExpon + pCurrNode_2->iExpon);
AppNodeToList(&pTempBuff[jCounter], pNewNode);
pCurrNode_2 = pCurrNode_2->next;
}
jCounter++;
pCurrNode_1 = pCurrNode_1->next;
}

/* 回收旧的中间结果 */
if (pResult != NULL) DestroyPolyn(*pResult);
/* 获得新的中间结果 */
AddProcess(pTempBuff, iLength, pResult , POSITIVE);
DestroyBuff(pTempBuff, iLength); /* 回收存中间结果的缓冲区 */
jCounter = 0;
if (++iCounter >= iPolyNum)
break;
else
{
iLength = PolynLength(*pResult);
MakePolyBuff(&pTempBuff, iLength);
pCurrNode_1 = *pResult;
}
}
}

void PrintPolyn(const polynomial polyList)
{
polynomial pCurrNode = polyList;

printf("多项式的长度为: %d\n", PolynLength(polyList));
while (pCurrNode != NULL)
{
printf("%.2fX^%d", pCurrNode->fCoeff, pCurrNode->iExpon);
if (pCurrNode->next != NULL)
if (pCurrNode->next->fCoeff > 0 )
printf("+");
pCurrNode = pCurrNode->next;
}
printf("\n");
}

int PolynLength(const polynomial polyList)
{
int iLength = 0;
polynomial pCurrNode = polyList;

while (pCurrNode != NULL)
{
pCurrNode = pCurrNode->next;
iLength++;
}
return iLength;
}

void DestroyBuff(PolyPointer polyBuff, const int iPolyNum)
{
int iCounter;

for (iCounter = 0; iCounter < iPolyNum; iCounter++)
DestroyPolyn(polyBuff[iCounter]);
free(polyBuff);
}

void DestroyPolyn(polynomial polyList)
{
polynomial pCurrNode;

while (polyList != NULL)
{
pCurrNode = polyList;
polyList = polyList->next;
free(pCurrNode);
}
}

status ProcStrError(const char str[])
{
const char *cpCurr = str;

if (!strlen(str))
{
printf("你没有输入数据!\n");
return FALSE;
}
while (*cpCurr != '\0')
{
if (!(*cpCurr == ' ' || *cpCurr == ',' || *cpCurr == ';'
|| *cpCurr == '-')
&& ('0' > *cpCurr || *cpCurr > '9')
|| (*(cpCurr + 1) == '\0' && *cpCurr != ';'))
{
printf("输入数据出错,请注意正确的输入方式!\n");
return FALSE;
}
cpCurr++;
}

return TRUE;
}
jixingzhong 2007-06-30
  • 打赏
  • 举报
回复
void SortPolyn(PolyPointer PolyBuff, int iPolyNum)
{
int iCounter;
polynomial pTemp, pTempCurrNode, /* 临时链表 */
pPrevMinExp, pCurrMinExp,/* 指向最小iExpon结点的指针 */
pCurrNode, pPrevNode;

for (iCounter = 0; iCounter < iPolyNum; iCounter++)
{
pTemp = NULL;
while (*(PolyBuff + iCounter) != NULL)
{
pPrevNode = pPrevMinExp = pCurrMinExp =
*(PolyBuff + iCounter);
pCurrNode = (*(PolyBuff + iCounter))->next;
while (pCurrNode != NULL)
{
if (pCurrMinExp->iExpon > pCurrNode->iExpon)
{
pPrevMinExp = pPrevNode;
pCurrMinExp = pCurrNode;
}
pPrevNode = pCurrNode;
pCurrNode = pCurrNode->next;
}
/* 将系数最小的结点从原链表中取出 */
if (pCurrMinExp == *(PolyBuff + iCounter))
*(PolyBuff + iCounter) = pPrevMinExp->next;
else
pPrevMinExp->next = pCurrMinExp->next;
/* 将系数最小的结点插入升序链表 */
pCurrMinExp->next = NULL;
if (!pTemp)
pTemp = pTempCurrNode = pCurrMinExp;
else
{
pTempCurrNode->next = pCurrMinExp;
pTempCurrNode = pTempCurrNode->next;
}
}

*(PolyBuff + iCounter) = pTemp;
}
}

void MergePolynCoeff(PolyPointer PolyBuff, int iPolyNum)
{
int iCounter;
float MergeCoeffRes = 0;
polynomial TempList, ResList = NULL, pCurrNode, pPreNode,
pNewNode = NULL;

for (iCounter = 0; iCounter < iPolyNum; iCounter++)
{
pPreNode = TempList= *(PolyBuff + iCounter);
MergeCoeffRes = pPreNode->fCoeff;
pCurrNode = (*(PolyBuff + iCounter))->next;
while (pCurrNode != NULL)
{
while ((pCurrNode != NULL) &&
(pCurrNode->iExpon == pPreNode->iExpon))
{
MergeCoeffRes += pCurrNode->fCoeff;
pPreNode = pCurrNode;
pCurrNode = pCurrNode->next;
}

/* 在ResList中加入新结点 */
if (MergeCoeffRes != 0)
{
MakeNode(&pNewNode, MergeCoeffRes, pPreNode->iExpon);
AppNodeToList(&ResList, pNewNode);
MergeCoeffRes = 0;
}

pPreNode = pCurrNode;
}

DestroyPolyn(TempList);
*(PolyBuff + iCounter) = ResList;
ResList = NULL;
}

}

void AddProcess(PolyPointer polyBuff, const int iPolyNum,
PolyPointer pResult, const int iSign)
{
int iCounter;
float fCoeffRes;
polynomial pNewNode, pCurrNode_1, pCurrNode_2,
pDelList = NULL, /* 下次要删除的中间结果链表 */
pResList = NULL; /* 中间结果链表 */

pCurrNode_1 = *(polyBuff);
for (iCounter = 1; iCounter < iPolyNum; iCounter++)
{
pCurrNode_2 = *(polyBuff + iCounter);
while (pCurrNode_1 != NULL && pCurrNode_2 != NULL)
{
if (pCurrNode_1->iExpon == pCurrNode_2->iExpon)
{
fCoeffRes = 0;
fCoeffRes = pCurrNode_1->fCoeff +
iSign * pCurrNode_2->fCoeff;
if (fCoeffRes != 0)
{
MakeNode(&pNewNode, fCoeffRes,
pCurrNode_1->iExpon);
AppNodeToList(&pResList, pNewNode);
}
pCurrNode_1 = pCurrNode_1->next;
pCurrNode_2 = pCurrNode_2->next;
}
else if (pCurrNode_1->iExpon < pCurrNode_2->iExpon)
{
MakeNode(&pNewNode, pCurrNode_1->fCoeff,
pCurrNode_1->iExpon);
AppNodeToList(&pResList, pNewNode);
pCurrNode_1 = pCurrNode_1->next;
}
else /* 当pCurrNode_1->iExpon > pCurrNode_2->iExpon时候 */
{
MakeNode(&pNewNode, iSign * pCurrNode_2->fCoeff,
pCurrNode_2->iExpon);
AppNodeToList(&pResList, pNewNode);
pCurrNode_2 = pCurrNode_2->next;
}
}
/* 加入余下的多项式 */
while (pCurrNode_1 != NULL)
{
MakeNode(&pNewNode, pCurrNode_1->fCoeff,
pCurrNode_1->iExpon);
AppNodeToList(&pResList, pNewNode);
pCurrNode_1 = pCurrNode_1->next;
}
while (pCurrNode_2 != NULL)
{
MakeNode(&pNewNode, iSign * pCurrNode_2->fCoeff,
pCurrNode_2->iExpon);
AppNodeToList(&pResList, pNewNode);
pCurrNode_2 = pCurrNode_2->next;
}

if (pDelList != NULL) DestroyPolyn(pDelList);
pCurrNode_1 = pResList;
pDelList = pResList;
pResList = NULL;
}

*pResult = pCurrNode_1;
}

void SubstractProcess(PolyPointer polyBuff, const int iPolyNum,
PolyPointer pResult)
{
AddProcess(polyBuff, iPolyNum, pResult , NEGATIVE);
}
jixingzhong 2007-06-30
  • 打赏
  • 举报
回复
/*
* 实验环境: Turbo C 2.0
* 完成时间: 2003年2月22日
*--------------------------------------------------------------------
* 改进说明: 可以实现多个多项式的加法、减法、乘法,并且比书中算法更加
* 合理. 例如: 连加a+b+c+d,连减a-b-c-d,连乘a*b*c*d.
*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0
#define POSITIVE 1
#define NEGATIVE -1

typedef int status;
typedef struct NodeType
{
float fCoeff;
int iExpon;
struct NodeType *next;
} NodeType, *LinkType;
typedef LinkType polynomial;
typedef polynomial *PolyPointer;

status MakePolyBuff(PolyPointer *, const int);
status MakeNode(polynomial *, const float, const int);
void AppNodeToList(polynomial *, polynomial); /* 在链表尾追加结点 */
status CreatePolyn(PolyPointer, int);
status ProcStrError(const char[]); /* 检查输入的数据 */
void SortPolyn(PolyPointer, int); /* 根据iExpon域对链表进行升序排序 */
void DestroyBuff(PolyPointer, const int);
void DestroyPolyn(polynomial);
int PolynLength(const polynomial); /* 求链表的长度 */
void AddProcess(PolyPointer, const int, PolyPointer, const int);
void SubstractProcess(PolyPointer, const int, PolyPointer);
void MultiplyProcess(PolyPointer, const int, PolyPointer);
void PrintPolyn(const polynomial);
void MergePolynCoeff(PolyPointer, int); /* 在有序链表中,合并同类项 */

int main(void)
{
int iCounter,
iPolyNum; /* 多项式链表缓冲区中链表的个数 */
PolyPointer PolyBuff = NULL; /* 用户输入的多项式链表缓冲区 */
polynomial PolyAddRes = NULL, /* 存放连加结果链表 */
PolySubRes = NULL, /* 存放连减结果链表 */
PolyMulRes = NULL; /* 存放连乘结果链表 */
char strNum[10];

do
{
printf("请输入需要构造多项式的个数,至少2个: ");
gets(strNum);
iPolyNum = atoi(strNum);
} while (iPolyNum < 2);

MakePolyBuff(&PolyBuff, iPolyNum);
CreatePolyn(PolyBuff, iPolyNum);
SortPolyn(PolyBuff, iPolyNum);
MergePolynCoeff(PolyBuff, iPolyNum);
printf("\n打印用户输入并整合后的多项式:\n");
for (iCounter = 0; iCounter < iPolyNum; iCounter++)
{
printf("第%d个项式:\n", iCounter + 1);
PrintPolyn(*(PolyBuff + iCounter));
}

AddProcess(PolyBuff, iPolyNum, &PolyAddRes, POSITIVE);
printf("\n----------------连加结果-----------------\n");
PrintPolyn(PolyAddRes);

SubstractProcess(PolyBuff, iPolyNum, &PolySubRes);
printf("\n----------------连减结果-----------------\n");
PrintPolyn(PolySubRes);

MultiplyProcess(PolyBuff, iPolyNum, &PolyMulRes);
printf("\n----------------连乘结果-----------------\n");
PrintPolyn(PolyMulRes);

printf("\n运行完毕!\n");
/* 回收资源 */
DestroyBuff(PolyBuff, iPolyNum);
DestroyPolyn(PolyAddRes);
DestroyPolyn(PolySubRes);
DestroyPolyn(PolyMulRes);

getch();
return 0;
}

status MakePolyBuff(PolyPointer *polyBuffHead, const int iPolyNum)
{
int iCounter;

*polyBuffHead = (PolyPointer)
malloc(sizeof(polynomial) * iPolyNum);
if (!(*polyBuffHead))
{
printf("错误,内存溢出!\n");
return FALSE;
}
for (iCounter = 0; iCounter < iPolyNum; iCounter++)
*(*polyBuffHead + iCounter) = NULL;

return TRUE;
}

status CreatePolyn(PolyPointer PolyBuff, int iPolyNum)
{
int iCounter, iExpon;
float fCoeff;
char strNum[100], strTemp[64], *cpCurr, *cpCurrNum;
polynomial pNewNode = NULL, pInsPos = NULL;

printf("\n请输入构造多项式的系数和指数...\n");
printf("输入一个多项式的方式为: 系数, 指数; ... ; 系数, 指数;\n例如: 3, 4; 5, 6; 7, 8;\n");
for (iCounter = 0; iCounter < iPolyNum; iCounter++)
{
printf("\n请输入第%d个多项式:\n", iCounter + 1);
gets(strNum);
if(!ProcStrError(strNum)) return FALSE;
cpCurr = cpCurrNum = strNum;
while (*cpCurr != '\0')
{
if (*cpCurr == ',')
{
strncpy(strTemp, cpCurrNum, cpCurr - cpCurrNum);
strTemp[cpCurr - cpCurrNum] = '\0';
fCoeff = (float)atof(strTemp);
cpCurrNum = cpCurr + 1;
}
else if (*cpCurr == ';')
{
strncpy(strTemp, cpCurrNum, cpCurr - cpCurrNum);
strTemp[cpCurr - cpCurrNum] = '\0';
iExpon = atoi(strTemp);
MakeNode(&pNewNode, fCoeff, iExpon);
AppNodeToList(PolyBuff + iCounter, pNewNode);
cpCurrNum = cpCurr + 1;
}
cpCurr++;
}
}

return TRUE;
}

status MakeNode(LinkType *pp, const float coeff, const int expon)
{
if (!(*pp = (LinkType)malloc(sizeof(NodeType) * 1)))
{
printf("Error, the memory is overflow!\n");
return FALSE;
}
(*pp)->fCoeff = coeff;
(*pp)->iExpon = expon;
(*pp)->next = NULL;

return TRUE;
}

void AppNodeToList(polynomial *pHead, polynomial pNewNode)
{
static polynomial pCurrNode;

if (!(*pHead))
(*pHead) = pCurrNode = pNewNode;
else
{
pCurrNode->next = pNewNode;
pCurrNode = pCurrNode->next;
}
}
iu_81 2007-06-30
  • 打赏
  • 举报
回复
#include <stdio.h>
#include "conio.h"
#include <stdlib.h>
#include "malloc.h"
#define M 3//可以改变输入的多项式的项数
#define N 2

typedef struct poly *polytype;
struct poly{
float coef;
int exp;
polytype next;
};
polytype Create_Empty_Node(int );
void Input_Values(polytype ,int );
void Insert_Node(polytype ,float ,int );
void Add_Poly(polytype ,polytype );
void Sub_Poly(polytype ,polytype );
void OutPut_Results(polytype);
void OutPut_Values(polytype);
int Infact(int ,int);/*计算阶乘*/

int main()
{
polytype pa,pb;
pa = Create_Empty_Node(M);//对加法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
Add_Poly(pa,pb);
printf("Add Results Are:\n");
printf("C(x) = A(x) + B(x) =\t");
OutPut_Results(pa->next);/*从第一个结点输出*/
OutPut_Values(pa->next);


pa = Create_Empty_Node(M);//对减法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
Sub_Poly(pa,pb);
printf("Sub Results Are:\n");
printf("D(x) = A(x)-B(x) =\t");
OutPut_Results(pa->next);
OutPut_Values(pa->next);


/*pa = Create_Empty_Node(M);//对乘法进行操作
printf("Please Input ListPoly pa:%d\n",M);
Input_Values(pa,M);
printf("A(x) =\t");
OutPut_Results(pa->next);
pb = Create_Empty_Node(N);
printf("Please Input ListPoly pb:%d\n",N);
Input_Values(pb,N);
printf("B(x) =\t");
OutPut_Results(pb->next);
By_Poly(pa,pb);
printf("E(x) =\t");
OutPut_Results(pa->next);/*从第一个结点输出*/
/*OutPut_Values(pa->next);*/

getchar();
return 0;
}
polytype Create_Empty_Node(int n)
{
polytype p;
p = (polytype)malloc(sizeof(struct poly));
if(p != NULL){
p->coef = (float)n;
p->exp = -1;
p->next = NULL;
}
else
printf("Failed To malloc!");
return
p;
}
void Input_Values(polytype p,int t)
{
float m;
int n;
for(int i = 0 ;i < t;i++)
{
scanf("%f",&m);
scanf("%d",&n);
Insert_Node(p,m,n);
}
}

void Insert_Node(polytype p,float coef,int exp)
{
polytype q,r;
q = p;
while(q->next != NULL )/*找到链表的结尾*/
q = q->next;
r = (polytype)malloc(sizeof(struct poly));
if(r == NULL)
{
printf("The Memory Is Full!");
exit(1);
}
else{
r->coef = coef;
r->exp = exp;
r->next = NULL;
q->next = r;
}
}
void Add_Poly(polytype pa,polytype pb)
{
float x = 0;
polytype p,q,pre,u;
p = pa->next;
q = pb->next;
pre = pa;
while(p && q)
{
if(p->exp < q->exp){
pre = p;
p = p->next;
}
else if(p->exp == q->exp){
x = p->coef+q->coef;
if(x){
p->coef = x;
pre = p;
}
else{
pre->next = p->next;
free(p);
}
p = pre->next;
u = q;
q = q->next;
free(u);
}
else{
u = q->next;
q->next = p;
pre ->next = q;
pre = q;
q = u;
}
}
if(q)
pre->next = q;
free(pb);
}
void Sub_Poly(polytype pa,polytype pb)
{
float x = 0;
polytype p,q,pre,u;
p = pa->next;
q = pb->next;
pre = pa;
while(p && q)
{
if(p->exp < q->exp){
pre = p;
p = p->next;
}
else if(p->exp == q->exp){
x = p->coef-q->coef;
if(x){
p->coef = x;
pre = p;
}
else{
pre->next = p->next;
free(p);
}
p = pre->next;
u = q;
q = q->next;
free(u);
}
else{//p->coef > q->coef
u = q->next;
q->next = p;
q->coef = -q->coef;
pre ->next = q;
pre = q;
q = u;
}
}
if(q){
pre->next = q;
q->coef = -q->coef;
}
free(pb);
}
/*void By_Poly(polytype pa,polytype pb)
{*/


void OutPut_Results(polytype pa)
{
polytype p;
for(p = pa;p->next != NULL;p = p->next)
{
printf("%.1fX^%d\t+",p->coef,p->exp);
}
printf("%.1fX^%d\n",p->coef,p->exp);
}
void OutPut_Values(polytype pa)
{
polytype p;
int x;
float Sum;
Sum = 0;
printf("Please Input X:\n");
scanf("%d",&x);
for(p = pa;p != NULL;p = p->next)
{
Sum += p->coef*Infact(x,p->exp);
}
printf("The Results Is :\n");
printf("%.1f\n",Sum);
}
int Infact(int m,int n)
{
int Fact;
Fact = 1;
for(int t = 0;t < n;t++)
Fact = Fact * m;
return Fact;
}
cookieshi 2007-06-30
  • 打赏
  • 举报
回复
TO lightnut() :

那个链接里面的东西,看的不是很懂。能稍微说明一下么?谢谢。
Inhibitory 2007-06-30
  • 打赏
  • 举报
回复
可以用链表来做,结点存储多项式的系数与次数,按多项式的次数从低到高排序.最后直接相加减就行了.每个多项式用一个链表来存储.
freshui 2007-06-29
  • 打赏
  • 举报
回复
zyt
lightnut 2007-06-29
  • 打赏
  • 举报
回复
看这个
http://community.csdn.net/Expert/topic/5616/5616704.xml?temp=.6259272

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧