1. 數據結構(c語言)
呵呵,碰巧到你網路空間看了下,看到了你想知道的這個問題,蒽蒽,下午就給你答案哈~~~
恩,可以了:
#include <stdio.h>
#include <stdlib.h>
typedef struct term
{
double coef;//系數
int expn; //指數
struct term *next;
}term,*polynomial;
//注:下面的head_p是頭指針,而非頭結點。其存放的是多項式的第一項
int depart_polyn(polynomial &head_p,polynomial &Odd_items,polynomial &Even_items)//奇數項,偶數項
{
int i=1,j=1;
polynomial odd,even,p;
Odd_items=Even_items=NULL;
p=head_p;
while(1)
{
if(p->expn%2)
{
if(i)
{
i=0;//使得奇數項頭指針只賦值一次
Odd_items=p;
odd=Odd_items;
}
else
{
odd->next=p;
odd=odd->next;
}
p=p->next;
odd->next=Odd_items;
}
else
{
if(j)
{
j=0;//作用同上
Even_items=p;
even=Even_items;
}
else
{
even->next=p;
even=even->next;
}
p=p->next;
even->next=Even_items;
}
if(p==head_p)
break;
}
return 1;
}
void print_polyn(polynomial head)//這函數只是針對main函數內給出的多項式例子編的,處理的系數均為正數。
{
polynomial p=head;
if( !head)
{
puts("該表為空:");
return;
}
while(1)
{
if(p->next!=head)
printf("%gm^%d + ",p->coef,p->expn);
else
printf("%gm^%d\n",p->coef,p->expn);
p=p->next;
if(p==head)
break;
}
putchar('\n');
}
int main()
{
int i;
polynomial head_p,head,Odd_items,Even_items,newbase;
head_p=(polynomial)malloc(sizeof(term));
head_p->coef=2.71;
head_p->expn=0;
head=head_p;
//隨意創建了一個稀疏多項式,以驗證
for(i=1;i<11;i++)
{
newbase=(polynomial)malloc(sizeof(term));
newbase->coef=2.71*i;
newbase->expn=i*13;
head->next=newbase;
head=head->next;
}
head->next=head_p;//構成循環鏈表
puts("原稀疏多項式:");
print_polyn(head_p);
if(depart_polyn(head_p,Odd_items,Even_items) )
{
puts("分解後的奇數項稀疏多項式:");
print_polyn(Odd_items);
puts("分解後的偶數項稀疏多項式:");
print_polyn(Even_items);
}
return 0;
}
哪裡不懂可追問:
2. C語言實現的一元多項式的表示及相減
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;//鏈表結點
typedef struct LinkList//定義鏈表數據類型
{
Node *head;//表頭結點
void PrintMform()//輸出多項式
{
Node *p=head->next;
int exp = head->data-1;
while(p!=NULL)
{
if(exp==head->data-1)
printf("%dX^%d",p->data,exp);
else
{
if(p->data>0)
printf("+%dX^%d",p->data,exp);
if(p->data==0)
printf("+0");
if(p->data<
0)
printf("%dX^%d",p->data,exp);
}
exp--;
p=p->next;
}
printf("\n");
}
void CreateByInput(int length)//通過輸入元素建立鏈表
{
head=(Node *)malloc(sizeof(Node));
head->data=0;//表頭節點存儲鏈表真實長度
head->next=NULL;
int i,temp;
Node *p,*cur = head;
for(i=1;i<=length;i++)
{
printf("structing LinkList,Please input the value:\n");
scanf("%d",&temp);
p = (Node *)malloc(sizeof(Node));
p->data=temp;
p->next=cur->next;
cur->next=p;
cur = cur->next;
head->data++;
}
}
}LinkList;
void main()
{
LinkList L1,L2;
int length;//就是多項式的項數
printf("Please input the first LinkList's length:\n");
scanf("%d",&length);
printf("begin to struct the first LinkList\n");//開始構造第一個多項式
L1.CreateByInput(length);
printf("begin to struct the second LinkList\n");//開始構造第二個多項式
L2.CreateByInput(length);
printf("the first oxiangshi is:\n");
L1.PrintMform();//輸出第一個多項式
printf("the second oxiangshi is:\n");
L2.PrintMform();//輸出第二個多項式
Node *p = L1.head->next;/////////////////從這里開始
Node *q = L2.head->next;//是計算多項式L1-L2,結果存入L1
while(p!=NULL)
{
p->data-=q->data;
p=p->next;
q=q->next;
}/////////////////////////////////////到這里結束
printf("the substract is:\n");
L1.PrintMform();
system("pause");
}
3. C語言數據結構問題
#include<stdio.h>
typedef struct polynode
{
int coef;
int exp;
struct polynode *next;
}polynode,*polylist;
polylist polycreate()
{
polynode *head,*rear,*s;
int c,e;
head=(polynode *)malloc(sizeof(polynode));
rear=head;
scanf("%d,%d",&c,&e);
while(c!=0)
{
s=(polynode *)malloc(sizeof(polynode));
s->coef=c;
s->exp=e;
rear->next=s;
rear=s;
scanf("%d,%d",&c,&e);
}
rear->next=NULL;
return(head);
}
void polyadd(polylist polya,polylist polyb)
{
polynode *p,*q,*tail,*temp;
int sum;
p=polya->next;
q=polyb->next;
tail=polya;
while(p!=NULL&&q!=NULL)
{
if(p->exp<q->exp)
{tail->next=p;tail=p;p=p->next;}
else if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
{
p->coef=sum;
tail->next=p;tail=p;
p=p->next;
temp=q;q=q->next;
free(temp);
}
else{
temp=p;p=p->next;free(temp);
temp=q;q=q->next;free(temp);
}
}
else{
tail->next=q;tail=q;
q=q->next;
}
}
if(p!=NULL) tail->next=p;
else if(q!=NULL) tail->next=q;
}
main()
{
int m;
polynode *p;
polylist la,lb;
printf("when input the data,please by count.\n");
printf("choose an operation(1.add or 2.minus)");
scanf("%d",&m);
if(m==1)
{
printf("Input first list(want to over,input 0)(coef,exp):\n");
la=polycreate();
printf("Input second list(wanto to over,input 0)(coef,exp):\n");
lb=polycreate();
polyadd(la,lb);
p=la->next;
printf("the result is:\n");
printf("%dX(%d)",p->coef,p->exp);
p=p->next;
while(p!=NULL)
{
printf("%+dX(%d)",p->coef,p->exp);
p=p->next;
}
}
if(m==2)
{
printf("input first list(want to over,input 0)(coef,exp):\n");
la=polycreate();
printf("input second list(want to over,input 0)(coef,exp):\n");
lb=polycreate();
p=lb;
while(p!=NULL)
{
p->coef=(-p->coef);
p=p->next;
}
polyadd(la,lb);
p=la->next;
printf("the result is:\n");
printf("%dX(%d)",p->coef,p->exp);
p=p->next;
while(p!=NULL)
{
printf("%+dX(%d)",p->coef,p->exp);
p=p->next;
}
}
}
4. c語言問題
大概看了一下,樓主的意思是
通過CreatPolyn創建一個鏈表,而且貌似元素是根據expn的值有序排列的
創建時首先搜索待插入元素的關鍵字是否存在,不存在才插入
插入的時候,樓主不想再做一次比較了,因此搞了個q想來保存插入的位置
拋開邏輯不談,如果這三個函數的實現與q的定義在一個文件中,那麼q就是全局變數,可以被本文件的代碼所訪問,這一點建議樓主學習一下全局變數的相關知識。
關於效率,其實再次搜索一次也無妨,因為while(p && e.expn<p->data.expn)
與while (p->next &&i<q)的執行效率沒有很大的差別。而且不在這里使用全局變數,也可以增加各個功能函數間的獨立性,易於閱讀代碼,減少bug的出現。
5. C語言高手請進!!
#ifndef
Polynomial_H
#define
Polynomial_H
#include
"List.h"
class
Term
{
public:
int
coef;
int
exp;
Term()
:
coef(0),
exp(0)
{}
Term(int
c,
int
e)
:
coef(c),
exp(e)
{}
Term(int
c)
:
coef(c),
exp(0)
{}
};
class
Polynomial
:
List<Term>
{
public:
void
Input()
{
cout
<<
endl
<<
"輸入多項式的各項系數和指數";
cout
<<
endl
<<
"注意:請按降序輸入各項,輸入系數0表示結束"
<<
endl;
int
coef,
exp;
for(int
i
=
1;
;
i
)
{
cout
<<
"第"
<<
i
<<
"項的系數:";
cin
>>
coef;
if
(coef)
{
cout
<<
"指數:";
cin
>>
exp;
Term
term(coef,
exp);
Insert(term);
}
else
break;
}
}
void
Print()
{
cout
<<
endl;
First();
if
(!IsEmpty())
{
Term
*p
=
Next();
cout
<<
p->coef;
if
(p->exp)
{
cout
<<
"x";
if
(p->exp
!=
1)
cout
<<
"^"
<<
p->exp;
}
while
(Next()
!=
NULL)
{
p
=
Get();
if
(p->coef
>
0)
cout
<<
"
";
cout
<<
p->coef;
if
(p->exp)
{
cout
<<
"x";
if
(p->exp
!=
1)
cout
<<
"^"
<<
p->exp;
}
}
}
cout
<<
endl;
}
friend
void
PolyAdd
(Polynomial
6. coef是什麼意思
coef 是coefficient的縮寫,是「系數」。
系數(coefficient),是指代數式的單項式中的數字因數。單項式中所有字母的指數的和叫做它的次數。通常系數不為0,應為有理數。
簡介
這里「系數」這個詞的用法與它的原本用法不太相同,但仍可以借用。假設所要反映的社會關系為3x=y,x代表基本情況(人口、資源等事實),不同的國家有不同的情況,3則代表那個數系——表示關系的數字。
這么一乘我們就可以得出,它所要勾畫的相應國家的實際情況了,即得數y。當然,這樣做是否能真實地反映實際社會關系倒不一定。數學總結。
7. 這段程序什麼意思啊!C語言求大神指導!!!
while(pa&&pb) //pa,pb指針都不為空,則進入循環
{
CreateItem(s); //創建一個新的項目s(不知道函數實現調用的是不是malloc)
i=ItemComp(*pa,*pb); //兩個項目之間的比較,返回值的意思需要根據函數代碼來看
if(i==0){
s->coef=pa->coef+pb->coef; //將兩個項目中的coef元素之後給新的項目s的coef
s->expn=pa->expn;//給結構體的元素賦值
if(s->coef){last->next=s;s->next=NULL;last=s;}//如果項目s的coef不為零,那就把s插入到鏈表的尾部,同時把last指向新的尾節點
pa=pa->next;//指著向後移
pb=pb->next;//指著向後移
}
else if(i<0){s->coef=pa->coef;s->expn=pa->expn;//如果i<0,就值大的pa指針的兩個元素賦給s
last->next=s;//下面三句是把s插入到鏈表的尾部,同時把last指向新的尾節點
s->next=NULL;
last=s;
pa=pa->next;//值大的pa指針向後一位
}
else{ s->coef=pb->coef;s->expn=pb->expn;//下面的這個分支的功能同上,把值大的pb指針賦給s
last->next=s; s->next=NULL;
last=s;
pb=pb->next;
}
}
s0=pa?pa:pb;//如果pa是空指針就把pb指針賦給s0,反之,則把pa賦給s0
while(s0){
CreateItem(s);//創建新的項目節點s
*s=*s0;
last->next=s;//下面三句是把s插入到鏈表的尾部,同時把last指向新的尾節點
s->next=NULL;
last=s;
s0=s0->next;//s0指針向後移一位
}
認真解答的,及時採納。
8. c 語言中 score是什麼意思
在C語言中沒有score這個保留字,換句話它在C語言中只能算是一個標識符,沒有特殊的語法功能。
一般來說C語言的標識符,有兩個基本的使用原則。
1、要符合語法要求,C語言中規定,標識符有數字、字母、下劃線(_)組成,而且第1符號只能為字母或者下劃線。
2、標識符的命名,盡量便於閱讀。如問題中的score用於表示分數,就容易理解。