多项式相除!!!要求用C语言完成!

fantasy5700 2010-03-31 09:14:10
结合数据结构链表知识!
...全文
579 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Player1005 2010-04-09
  • 打赏
  • 举报
回复
问一下:
多项式的除法的算法该是怎么样?
jianguzi 2010-04-09
  • 打赏
  • 举报
回复
gaoshou
fantasy5700 2010-04-09
  • 打赏
  • 举报
回复
各位大哥大姐!谢谢啦!
bobo364 2010-03-31
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct _POLYNODE{
int coef;//系数
int exp;//指数
struct _POLYNODE *next;
}polynode,*polyptr;

void createPoly(polynode **P, char ch[]);//建立多项式链表
void polyAdd(polynode *A,polynode *B);//多项式加
void polyMinus(polynode *A,polynode *B);//减
void polyMulti(polynode *A,polynode *B);//乘
void polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多项式
void destroy(polynode **P);//销毁多项式
void menu();//命令菜单
int isPut(char ch[]);
//菜单
void menu(){
printf("1.输入多项式.\n"
"2.多项式相加.\n"
"3.多项式相减.\n"
"4.多项式相乘.\n"
"5.多项式相除.\n"
"6.显示多项式.\n"
"7.销毁多项式.\n"
"8.退出.\n");
}
//判断菜单选择
int IsChoice(int choice){
if(0 < choice && 9 > choice)
return 1;
else
return 0;
}

int isPut(char ch[]){
int i,j = 1;
for(i = 0; ch[i] != '\0'; i++){
{if(0 == j && '^' == ch[i])
return 0;
if('^' == ch[i] && 1 == j)
j = 0;
if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && 0 == j)
j = 1;

}
if('.' != ch[i] && 'x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !isdigit(ch[i]))
return 0;
else{
if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])
return 0;
if('\0' == ch[i+1] && '+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])
return 0;
// 上面是判断字符串首尾是否合格 下面是中间部分
if(0 != i && ch[i+1] != '\0' ){
if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' != ch[i-1] && '.' != ch[i-1])
return 0;
if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != ch[i+1])
return 0;
if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch[i+1])
return 0;
if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
return 0;
if('^' == ch[i] && !isdigit(ch[i+1]))
return 0;
if('.' == ch[i] && !isdigit(ch[i+1]) && !isdigit(ch[i-1]))
return 0;
}
}
}
return 1;
}


void createPoly(polynode **P, char ch[]){
char *t = ch;
int i = 0, j = 1;
int iscoef = 1,isminus = 1;
polyptr Q,L;

if('-' == ch[0]){
isminus = -1;
*t++;
}
while('\0' != *t){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = 1;
Q->exp = 0;
Q->next = NULL;//申请节点,初始化参数为1.

if(-1 == isminus){
Q->coef *= isminus;
isminus = 1;
}

while('+' != *t && '-' != *t && '*' != *t && '/' != *t && '\0' != *t){
if('x' != *t && 'X' != *t){
while(isdigit(*t)){
i =((int)*t - 48) + i*10;
t++;
j *= i;
}//抽取数字
if(1 == iscoef && 0 != i){
Q->coef *= i;
}
if(0 == iscoef){
Q->exp += i;
iscoef = 1;
}
//如果i=0,则

}
else{
iscoef = 0;
t++;
if('^' == *t)
t++;
else
Q->exp = 1;
}
i = 0;
}//while 遍历到加减乘除,则退出循环,到下一新的节点
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q->coef = 0;
j = 1;
}
printf("系数:%d,指数:%d\n",Q->coef,Q->exp);

if(NULL == *P){
*P = Q;
}
else{
L->next = Q;
}
L = Q;

}//while遍历整个字符串

}
void polyAdd(polynode *A,polynode *B){
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}

while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}

L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(©B);
order(©B);
printf("相加结果为:");
display(COPYB);
destroy(©B);
}
bobo364 2010-03-31
  • 打赏
  • 举报
回复
#include   <stdio.h>   
#include <stdlib.h>
#include <ctype.h>
struct List
{int coefficient; // 系数
int exponential ;//指数
struct List *Next;
};
typedef struct List Node;
typedef Node *Link;
void main()
{
char *p,temp,item[32],num[10];
Link list,current;
int i=0,j=0;
list =(Link)malloc(sizeof(Node)); //头结点
list->coefficient=0;
list->exponential=0;
list->Next=NULL;
printf("input your expression:\n");
p=gets(p);
label: while('\0'!=*p)
{ temp=*p;
if(temp=='+'||temp=='-')
{
p++;
goto label;
}
while (isalnum(temp)&&(temp!='\0'))
{


item[i]=temp;
i++;
p++;
temp= *p;

}
item[i]='\0';
i=0;
j=0;
current=(Link)malloc(sizeof(Node));
while(item[i]!='X')
{
num[i]=item[i];
i++;
}
num[i]='\0';
current->coefficient=atoi(num);
i++;
while(item[i]!='\0')
{
num[j]=item[i];
i++;
j++;
}
num[j]='\0';
current->exponential=atoi(num);
current->Next=list->Next;
list->Next=current;
i=0;
j=0;
}
TEST: current=list->Next;                 //测试代码
while(current!=NULL)
{
printf("%d^%d ",current->coefficient ,current->exponential);
current=current->Next;
}
}


建立的链表没有排序,自己随便看一本数据结构的书,上面有链表排序的
xiuxianshen 2010-03-31
  • 打赏
  • 举报
回复
作业贴。。先自己写吧,写好了贴上来让人改改
cy330206 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kevinyujm 的回复:]
既然有人贴出代码了,
楼主,把别人的代码调试通过并且理解,也是个学习过程。
[/Quote]

说的也是
FoxSerratifolia 2010-03-31
  • 打赏
  • 举报
回复
二楼也忒快了
kevinyujm 2010-03-31
  • 打赏
  • 举报
回复
既然有人贴出代码了,
楼主,把别人的代码调试通过并且理解,也是个学习过程。

69,371

社区成员

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

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