求测试求指错求讲解——关于多项式
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
typedef struct Polynomial
{
float coef;
int expn;
struct Polynomial *next;
}*Polyn;
void InitPolyn(Polyn *P);
void CreatPolyn(Polyn *P,int m);//创建一个多项式
void ShowPolyn(Polyn P);//打印出多项式
void SortPolyn(Polyn P);//按序排列
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa + Pb
void SubPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa - Pb
void valPolyn(Polyn *P,int x);//求多项式当X为多少的值
void InitPolyn(Polyn *P)
{
*P = (Polyn )malloc(sizeof(Polyn));
(*P)->coef = 0.0;
(*P)->expn = 0;
(*P)->next = NULL;//初始化
}
void CreatPolyn(Polyn *P,int m)
{
float c;
int e,i;
Polyn s,q;
q = *P;
printf("请输入项数:");
scanf("%d",&m);
for(i = 1;i<=m;i++)
{
printf("请输入第%d项的系数和指数:",i);
scanf("%f %d",&c,&e);
s = (Polyn )malloc(sizeof(Polyn));
s->coef = c;
s->expn = e;
s->next = NULL;
q->next = s;
q = s;
q->next = NULL;
}
}
void SortPolyn(Polyn P)
{
Polyn temp;
while(P!=NULL)
{
if(P->expn<P->next->expn)
{
temp = P->next;
P->next = P;
P = temp;
}
}
}
void ShowPolyn(Polyn P)
{
Polyn pchar;
pchar = P->next;
while(pchar)
{
printf("%f X^%d",pchar->coef,pchar->expn);
pchar = pchar->next;
}
printf("\n");
}
int main()
{
Polyn Pa;
InitPolyn(&Pa);
CreatPolyn(&Pa,2);
SortPolyn(Pa);
ShowPolyn(Pa);
}
还有很多编码没有写,只是想先写个main函数来测试测试,我知道中间有个问题,就是CreatPolyn函数的两个参数,第二个m参数改掉之后编译不过关,添上的话就能编译成功,但是添上的话CreatPolyn要用户输入的项数就形同鸡肋了。不知道怎么解决。 然后就是排序和打印的时候直接把黑框给关闭了,不解啊。求测试求指错求讲解。 还有就是要睡觉了,要回也得等明天了,先谢谢热心帮助的人。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Polynomial
{
float coef;
int expn;
struct Polynomial *next;
}*Polyn;
int m; // 项数
void InitPolyn(Polyn *P);
void CreatPolyn(Polyn *P,int m);//创建一个多项式
void ShowPolyn(Polyn P);//打印出多项式
void SortPolyn(Polyn P);//按序排列
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa + Pb
void SubPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa - Pb
void valPolyn(Polyn *P,int x);//求多项式当X为多少的值
void InitPolyn(Polyn *P)
{
*P = (Polyn )malloc(sizeof(Polyn));
(*P)->coef = 0.0;
(*P)->expn = 0;
(*P)->next = NULL;//初始化,加上头结点,便于操作
}
void CreatPolyn( Polyn *P ) // 去掉指针符号
{
float c;
int e,i;
Polyn s,q;
q = *P;// 去掉指针符号
printf("请输入项数:");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("请输入第%d项的系数和指数:",i);
scanf("%f%d",&c,&e);
s = (Polyn )malloc(sizeof(Polyn));
s->coef = c;
s->expn = e;
s->next = NULL;
q->next = s;
q = s;
q->next = NULL;
}
}
/*
void SortPolyn(Polyn P)// 什么排序法?
{
Polyn temp;
while(P!=NULL)
{
if( P->expn < P->next->expn )
{
temp = P->next;
P->next = P;
P = temp;
}
// 感觉少了这一句 P = P ->next;
}
}
*/
void SortPolyn( Polyn *P )
{
Polyn q, g;
int i, j, c, e;
// 冒泡排序法
g = (*P);
for( i = 1; i < m; i++ )
{
q = g ->next;
for( j = 0; j < m - i; j++ )
{
// 用 temp 来处理好像 行不通
if( ( q ->next ) && ( q->expn < q->next->expn ) )
{
c = q ->coef;
e = q ->expn;
q ->coef = q ->next ->coef;
q ->expn = q ->next ->expn;
q ->next ->coef = c;
q ->next ->expn = e;
}
q = q ->next;
}
g = g ->next;
}
}
void ShowPolyn(Polyn P)
{
Polyn pchar;
pchar = P->next;
while(pchar)
{
printf("%.3f X^%d\t",pchar->coef,pchar->expn);
pchar = pchar->next;
}
printf("\n");
}
int main( void )
{
Polyn Pa;
InitPolyn(&Pa);
CreatPolyn(&Pa);
ShowPolyn(Pa);
system("pause");
SortPolyn(&Pa);
ShowPolyn(Pa);
return 0;// 最好加上
}
f:\结果.jpg 没有人嘛???!! 求解释啊! 这个是程序的结果: HHR 发表于 2014-4-21 17:09 static/image/common/back.gif
这个是程序的结果:
膜拜。。。。。。。。。
页:
[1]