问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

数据结构作业 要用tc能编译出来的

发布网友 发布时间:2022-05-22 02:50

我来回答

2个回答

热心网友 时间:2024-03-28 00:34

多项式的有一个现成的,其中有个乱码是& C O P Y 百度的问题。
#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);
}
void polyMinus(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);

}
void polyMulti(polynode *A,polynode *B){
polyptr R = A, P = B, Q, L = NULL, T;

if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(0 == A->coef || 0 == B->coef){
printf("多项式乘积为:0\n");
return ;
}

while(NULL != R){
while(NULL != P){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == L)
L = Q;
else
T->next = Q;
T = Q;
P = P->next;
}
P = B;
R = R->next;
}
order(&L);
order(&L);
printf("多项式乘积为:\n");
display(L);
destroy(&L);
}
void polyDiv(polynode *A,polynode *B){//多项式除法
polyptr P = A, Q,L,R,T,C,D;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(A->coef == 0){
printf("0.\n");
return ;
}
if(B->coef == 0){
printf("除数为0,错误!\n");
return ;
}
if(A->coef < B->coef){
printf("商:0,余数为:");
display(A);
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;
}
C = P = Q = L = R = T = NULL;

//------------------开始计算
while(COPYA->exp >= COPYB->exp){
D = COPYA;
while(NULL != D->next)
D = D->next;
R = COPYB;
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = (-COPYA->coef) / R->coef;
Q->exp = COPYA->exp - R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
P->next = Q;
P = Q;

while(NULL != R){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == T)
T = Q;
else
C->next = Q;
C = Q;

R = R->next;
}
D->next = T;
order(©A);
order(©A);
T = NULL;
C = NULL;
}
order(&L);
order(©A);
printf("A除以B,商:");
display(L);
printf("余数:");
display(COPYA);

destroy(&L);
destroy(©A);
destroy(©B);
}

void display(polynode *P){
//考虑情况有系数为1,指数为1,0,一般数;系数为系数不为1,指数为1,0,一般数;
//系数为负数,指数为1,0,一般数,主要考虑中间符号问题.
if(NULL == P){
printf("多项式为空.\n");
return ;
}
if(1 == P->coef){
if(0 == P->exp)
printf("1");
else if(1 == P->exp) printf("x");
else printf("x^%d",P->exp);
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
P = P->next;
while(NULL != P){
if(0 < P->coef){
if(1 == P->coef){
if(0 == P->exp)
printf("+1");
else if(1 == P->exp) printf("+x");
else printf("+x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("+%d",P->coef);
else if(1 == P->exp) printf("+%dx",P->coef);
else
printf("+%dx^%d",P->coef,P->exp);
}
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
}
P = P->next;
}
printf("\n");
}

void destroy(polynode **P){
polyptr Q = *P;
if(NULL == *P)
return ;
while(*P != NULL){
Q = *P;
*P = (*P)->next;
delete Q;
}

}

void order(polynode **P){
//首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的要合并.
polyptr prev,curr,OUT,INcurr;//前一节点和当前节点
int temp;

//出去第一节点系数为0的项
while(NULL != *P){
if(0 != (*P)->coef)
break;
else{
if(NULL == (*P)->next)
return;
curr = *P;
(*P) = (*P)->next;
delete curr;
}
}
if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不需要整理,退出函数
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT->next){//外循环
while(NULL != INcurr->next){//内循环
prev = INcurr;
INcurr = INcurr->next;
if(prev->exp < INcurr->exp){
temp = prev->coef;
prev->coef = INcurr->coef;
INcurr->coef = temp;//交换系数

temp = prev->exp;
prev->exp = INcurr->exp;
INcurr->exp = temp;//交换指数
}
}
OUT = OUT->next;
INcurr = *P;
}

//去除0项
prev = curr = *P;
curr = curr->next;
while(NULL != curr){
if(0 == curr->coef){
prev->next = curr->next;
delete curr;
curr = prev->next;
}
else{
prev = curr;
curr = curr->next;
}
}
//合并同类项
OUT = INcurr = *P;
while(NULL != OUT->next){
while(NULL != INcurr->next){
prev = INcurr;
INcurr = INcurr->next;
if(INcurr->exp == OUT->exp){
OUT->coef += INcurr->coef;
prev->next = INcurr->next;
delete INcurr;
INcurr = prev;
}
}
INcurr = OUT = OUT->next;
if(NULL == OUT)
return;
}

}

void main(){
int choice;
// int i;
char ch[100];
polynode *polyA,*polyB;

polyA = polyB = NULL;

menu();
scanf("%d",&choice);
while(!IsChoice(choice)){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
while(8 != choice){
switch(choice){
case 1:
if(NULL != polyA || NULL != polyB){
destroy(&polyA);
destroy(&polyB);
printf("原多项式被销毁.\n");
}
printf("多项式输入格式:4x^3+7x^2+x+6--x不分大小写.\n输入多项式A:\n");
scanf("%s",&ch);

while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyA,ch);//建立多项式A链表

printf("输入多项式B:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyB,ch);//建立多项式B链表
order(&polyB);
order(&polyA);//整理排序多项式,默认降幂

printf("建立多项式成功!多项式:\nA为:");
display(polyA);
printf("B为:");
display(polyB);
break;
case 2:
polyAdd(polyA,polyB);
break;
case 3:
polyMinus(polyA,polyB);
break;
case 4:
polyMulti(polyA,polyB);
break;
case 5:
printf("PS:系数只支持整数,计算除法存在误差;\n如果除数所有项系数为1,能得到正确答案,或者某些情况系数刚好被整除.");
polyDiv(polyA,polyB);
break;
case 6:
printf("------显示多项式------\nA :");
display(polyA);
printf("B :");
display(polyB);
break;
case 7:
destroy(&polyA);
destroy(&polyB);
printf("此多项式已被清空.\n");
break;
default:
return ;
}
choice = 0;
menu();
scanf("%d",&choice);
while(!IsChoice(choice) || 0 == choice){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
}
}

热心网友 时间:2024-03-28 00:35

顺序表的
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node* next;
};
typedef struct node* qnode;

qnode mergesort(qnode a,qnode b);
qnode create(void);
qnode sort(qnode a);
void print(qnode a);
void main(void)
{
qnode a,b;
printf("Input list a:\n");
a=create();
printf("Input list b:\n");
b=create();
a=mergesort(a,b);
print(a);
system("pause");
}
void print(qnode a)
{
while(a)
{
printf("%d ",a->data);
a=a->next;
}
printf("\n");
}
qnode create(void)
{
qnode p=NULL,head=NULL,t;
int d;
int b=0;
while(1)
{
printf("input a new number(input -1 to end):");
scanf("%d",&d);
if(d==-1)
break;
t=(qnode)malloc(sizeof(struct node));
t->data=d;
t->next=NULL;
if(!b++)
head=t;
else
p->next=t;
p=t;
}
return head;
}
qnode sort(qnode a)
{
int n=1,i,j;
qnode p,heada=a,t,*list;
if(!a)
return NULL;
while(a->next)
{
n++;
a=a->next;
}
a=heada;
list=(qnode *)malloc(sizeof(qnode)*n);
for(i=0;i<n;i++)
{
list[i]=a;
a=a->next;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(list[i]->data>list[j]->data)
{
t=list[i];
list[i]=list[j];
list[j]=t;
}
a=list[0];
for(i=0;i<n;i=j)
{
list[i]->next=NULL;
j=i+1;
while(j<n&&list[i]->data==list[j]->data)
j++;
if(j<n)
{
list[i]->next=list[j];
for(i++;i<j;i++)
free(list[i]);
}
}
free(list);
return a;
}
qnode mergesort(qnode a,qnode b)
{
qnode heada=a;
if(!a)
{
if(!b)
{
printf("List a & b are empty!\n");
return NULL;
}
else
{
printf("List a is empty!\n");
return sort(b);
}
}
else if(!b)
{
printf("List b is empty!\n");
return sort(a);
}
else
{
while(a->next)
a=a->next;
a->next=b;
return sort(heada);
}
}

多项式的
#include<stdio.h>
#include<stdlib.h>

struct poly
{
int n;
double *data;
};
typedef struct poly* qpoly;

qpoly create(void);
void print(qpoly a);
qpoly multiply(qpoly a,qpoly b);
qpoly plus(qpoly a,qpoly b);
void main(void)
{
float arg,*point=&arg;
qpoly a,b,c,d;
printf("Input polynomial a:\n");
a=create();
printf("Input polynomial b:\n");
b=create();
system("cls");
printf("\n\npolynomial a:\n");
print(a);
printf("\n\npolynomial b:\n");
print(b);
c=multiply(a,b);
printf("\n\npolynomial a * b:\n");
print(c);
printf("\n\npolynomial a + b:\n");
d=plus(a,b);
print(d);
system("pause");
}
qpoly plus(qpoly a,qpoly b)
{
int i,j;
qpoly c=(qpoly)malloc(sizeof(struct poly));
c->n=a->n>b->n-1?a->n:b->n;
c->data=(double *)malloc(sizeof(double)*c->n);
for(i=0;i<c->n;i++)
{
c->data[i]=0;
if(i<a->n)
c->data[i]+=a->data[i];
if(i<b->n)
c->data[i]+=b->data[i];
}
return c;
}
qpoly multiply(qpoly a,qpoly b)
{
int i,j;
qpoly c=(qpoly)malloc(sizeof(struct poly));
c->n=a->n+b->n-1;
c->data=(double *)malloc(sizeof(double)*c->n);
for(i=0;i<c->n;i++)
c->data[i]=0;

for(i=0;i<a->n;i++)
for(j=0;j<b->n;j++)
c->data[i+j]+=a->data[i]*b->data[j];
return c;
}
void print(qpoly a)
{
int i=a->n-1;
printf("%lfx^%d",a->data[i],i);
i--;
while(i>0)
{
printf(" + %lfx^%d",a->data[i],i);
i--;
}
printf(" + %lf",a->data[i]);
printf("\n");
}
qpoly create(void)
{
qpoly p;
int d;
int b=0,i=0;

do
{
printf("Input number of terms of polynomial(at least 1):",&d);
scanf("%d",&d);
}
while(d<1);

p=(qpoly)malloc(sizeof(struct poly));
p->data=(double *)malloc(sizeof(double)*d);
p->n=d;

for(i=0;i<d;i++)
{
printf("Input terms x^%d:",i);
scanf("%lf",p->data+i);
}
return p;
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
推荐一款在卧室听的音响,最好带CD,带收音机,可插U盘,价格1000以内!尽量... 求问,近几年还会有类似于声之形、你的名字这样的动漫电影么w_百度... 苦学包括什么 北京世纪百奥科技有限公司怎么样? 居住证积分需要哪些材料 上海办居住证积分需要什么资料 居住证积分有哪些材料 如何建立良好的家庭情感氛围 如何让家庭氛围更快乐 如何营造一个欢乐轻松的家庭氛围呢? 有限责任公司被起诉中可以变更股权吗? 起诉董事可以转让股权吗 严重失信企业可以股权变更吗 1.编写一个add()函数实现多项式加法逻辑,将两个链表加法合并成一个. 2.将合并后的链表显示。 用C语言编写 公司董事长可否起诉转让股权 公司被起诉后股权可以转让吗 用来表示多项式的单链表,为什么头结点的值为 股权能不能在起诉前变更? PolyBrodge:建桥鬼才惊现1:100完美还原赵州桥这才是建桥游戏! 有官司在身,公司法人股权好变更吗 公司有债务纠纷可以转让股权吗? 上海宝政市政工程有限公司怎么样? 芷江中路610号属于哪个居委会? 上海名师学习软件研究所怎么样? 股份公司被起诉可以股权转让吗 上海市芷江中路青云路382弄归属哪个派出所? 闸北区芷江中路680号属于那个街道办事处 上海芷江中路227号属于哪个街道?属于哪个居委会? 上海市芷江中路258弄在什么区 静安区芷江中路363弄3号301是属于什么街道? 多粘菌素Polymyxin B是治疗什么的,哪里可以查到相关信息? 韩国的几个女团在三个音乐节目中得过一位吗?如果得过,是靠哪首歌? 我用matlab的poly命令好像不是求多项式系数啊 以下为输出结果,请高手解答一下 深圳哪里有拍微电影的?大概要多少钱? 深圳微电影拍摄多少钱 拍一部电影要多少钱 哪个深圳微电影公司的报价最便宜? 一种桶装纯净水,小明一家三口两天喝3升水,小明全家两个月大约喝5桶水,这种纯净水一桶有多少升 皖露纯净水一桶几升 润发一世一桶水多少升 第一次吃了清美酵素梅会拉肚子吗 我在淘宝上买孝素梅,吃了拉稀,为什么,以前便秘? 带有歌词 你到底在哪里 歌名? 歌词 现在的你到底在哪里 我知道你现在并不在意 什么歌 求歌名:我在草原等你……亲爱的你不要再生我的气,我们已走过多少风雨,你到底在哪里,我真的好想你…… 茫茫人海中你到底在哪里为何总是对我不断逃避你给我留下那枯萎的茉莉 想你的时候你到底在哪里 歌词 这首是什么歌 &quot;多年以后还是想着你,现在你究竟在哪里&quot;是哪首歌里的歌词啊 快播 下载好的电影怎么弄到手机里 快播里的电影传到手机上会有病毒吗