嘿嘿,有沒有大佬幫忙看下這段代碼的錯誤,為什么輸入資料后運行不出來呀,感謝!
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct polynode
{
int exp;
float coef;
struct polynode *next;
};
typedef struct polynode* polypointer;
polypointer CreateList(int n){
polypointer head;
polypointer p,pre;
int i;
head=(polypointer)malloc(n*sizeof(struct polynode));
head->next=NULL;
pre=head;
for(i=1;i<=n;i++)
{
p=(polypointer)malloc(n*sizeof(struct polynode));
printf("請輸入指數\n");
scanf("%d",&p->exp);
printf("請輸入系數\n");
scanf("%d",&p->coef);
pre->next=p;
pre=p;
}
p->next=NULL;
return head;
}
void PrintList(polypointer h){
polypointer p;
p=h->next;
if(p!=NULL){
printf("%f*X^%d",p->coef,p->exp);
p=p->next;
printf("\n");
}
}
int bubble_sort(polypointer q,int o){
polypointer pre,p,temp;
int i,j;
pre=q->next;
p=pre->next;
for(i=1;i<=o-1;i++)
for(j=2;j<=o;j++){
if(pre>p){
pre->next=p->next;
p->next=pre;
temp=p;
p=pre;
p=temp;
}
p=p->next;
pre=p;
}
return 0;
}
polypointer AddList(polypointer head1,polypointer head2){
polypointer p,q,c,head3;
int x;
p=head1->next;
q=head2->next;
c=(polypointer)malloc(sizeof(struct polynode));
head3=c;
c=head3->next;
while(p!=NULL&&q!=NULL){
if(p->exp==q->exp) {
x=p->coef+q->coef;
c->coef=x;
c->exp=p->exp;
c=c->next;
p=p->next;
q=q->next;
}
else if((p->exp)>(q->exp)){
c->coef=q->coef;
c->exp=q->exp;
q=q->next;
c=c->next;
}
else if((p->exp)<(q->exp)){
c->coef=p->coef;
c->exp=p->exp;
p=p->next;
c=c->next;
}
}
c=head3;
return c;
}
int main(){
int m,n;
polypointer a,b,c;
printf("請輸入多項式 A 的項數 : \n ");
scanf("%d",&m);
a=CreateList(m) ;
printf("請輸入多項式 B 的項數 : \n ") ;
scanf("%d",&n);
b=CreateList(n) ;
bubble_sort(a,m);
bubble_sort(b,n);
c=AddList(a,b);
PrintList(c);
return 0;
}
uj5u.com熱心網友回復:
/*AddList 函式未做修改 樓主自行解決*/1.創建節點時 記憶體分配有問題
2.輸入時對弈系數的接收格式有問題
3.輸出函式判斷條件有問題
4.排序函式 結點比較條件有問題,結點之間交換有問題
/*以下程式已對上述問題作出更正 剩余 ADDList 請參照程式自行解決*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
typedef struct polynode
{
int exp;
float coef;
struct polynode *next;
}polynode,*polypointer;
polypointer CreateList(polypointer Head,int n){
polypointer NewNode;
polypointer pre;
pre = Head;
for(int i=1;i<=n;i++)
{
NewNode=(polypointer)malloc(sizeof(polynode));
NewNode->next=NULL;//創建一個結點 輸入一個資料結點
printf("請輸入<第 %d 位>指數\n",i);
scanf("%d",&NewNode->exp);
printf("請輸入系數:例如 2.5\n");
scanf("%f",&NewNode->coef);
pre->next=NewNode;
pre=NewNode;
}
return Head;
}
void PrintList(polypointer h){
polypointer p;
p=h->next;
while(p!=NULL)//if陳述句只會執行一次
{
printf("%.2f*X^%d ",p->coef,p->exp);
p=p->next;
printf("\n");
}
}
void bubble_sort(polypointer head,int o){
polypointer prepre,pre,p,temp;
//開始位置 prepre,pre,p 表示 Head,1,2
/*冒泡排序*/
for(int i=1;i<=o-1;i++)
{
prepre = head;
pre=head->next;//每比較一次,指標指向最鏈表第一個,第二個結點位置
p=pre->next;
for(int j=2;j<=o;j++)
{ //利用冪函式計算結點值
if(pow(pre->coef,pre->exp)>pow(p->coef,p->exp))//比較的應該是結點內部存盤的結點值 而不是結點本身
{
/*如果前驅結點 大于 后繼結點 交換位置*/
prepre->next = p;
temp = p->next;
p->next = pre;
pre->next = temp;
}
prepre = pre;//指標后移
pre=p;
p=p->next;
}
}
}
polypointer AddList(polypointer head1,polypointer head2){
polypointer p,q,c,head3;
int x;
p=head1->next;
q=head2->next;
c=(polypointer)malloc(sizeof(struct polynode));
head3=c;
c=head3->next;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
x=p->coef+q->coef;
c->coef=x;
c->exp=p->exp;
c=c->next;
p=p->next;
q=q->next;
}
else if((p->exp)>(q->exp))
{
c->coef=q->coef;
c->exp=q->exp;
q=q->next;
c=c->next;
}
else if((p->exp)<(q->exp))
{
c->coef=p->coef;
c->exp=p->exp;
p=p->next;
c=c->next;
}
}
c=head3;
return c;
}
int main(){
int m,n;
polypointer a=(polypointer)malloc(sizeof(polynode));
a->next = NULL;
polypointer b=(polypointer)malloc(sizeof(polynode));
b->next = NULL;
polypointer c=(polypointer)malloc(sizeof(polynode));
c->next = NULL;
printf("請輸入多項式 A 的項數 : \n");
scanf("%d",&m);
a=CreateList(a,m);
printf("請輸入多項式 B 的項數 : \n") ;
scanf("%d",&n);
b=CreateList(b,n);
/*功能函式測驗輸出*/
printf("輸出A-------\n");
PrintList(a);
printf("輸出B*******\n") ;
PrintList(b);
/*功能函式測驗輸出*/
bubble_sort(a,m);
printf("輸出A排序后**\n") ;
PrintList(a);
bubble_sort(b,n);
printf("輸出B排序后**\n") ;
PrintList(b);
/*AddList 函式未做修改 樓主自行解決*/
c=AddList(a,b);
printf("輸出B******* \n") ;
PrintList(c);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/75973.html
標籤:C語言
上一篇:關于字串的問題
