基于visual Studio2013解决C语言竞赛题之1091多项式
生活随笔
收集整理的這篇文章主要介紹了
基于visual Studio2013解决C语言竞赛题之1091多项式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目
解決代碼及點(diǎn)評(píng)
/************************************************************************/ /* 91. 建立兩個(gè)鏈表,來(lái)表示x冪的兩個(gè)多項(xiàng)式,鏈表中的結(jié)點(diǎn)有三個(gè)字段coef、exp和next,分別表示多項(xiàng)式每項(xiàng)的系數(shù)、x的指數(shù)及指向下一項(xiàng)的指針。編一程序,按x的降冪輸入多項(xiàng)式的系數(shù)和指數(shù),建立兩個(gè)鏈表,然后編一函數(shù)來(lái)完成把兩個(gè)多項(xiàng)式的鏈表疊加到第三個(gè)鏈表中。例如: 第一個(gè)多項(xiàng)式為: -4x8 +5x6 +3x4 -4x的鏈表為:-4 8 5 6 3 4 -4 1 第二個(gè)多項(xiàng)式為: 5x9 -5x8 -3x4 +7x的鏈表為:5 9 -9 8 5 6 3 1 結(jié)果的多項(xiàng)式為: 5x9 -9x8 +5x6 +3x5 9 -9 8 5 6 3 1 */ /************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h>typedef struct student STU; struct student {int coef;int exp;struct student * next; }; STU * Init91() {STU * p=(STU *)malloc(sizeof(STU));if (p==NULL){return NULL;}elsep->next=NULL;return p; } STU * Insert91(STU * head,int coef,int exp) { STU * last=head;if (last==NULL){return NULL;}while(last->next!=NULL)last=last->next;STU *p=(STU *)malloc(sizeof(STU));if (p==NULL){return NULL;}else{p->coef=coef;p->exp=exp;last->next=p;p->next=NULL;return p;} } void DeleteNode91(STU* pre,STU *cur) {pre->next=cur->next;free(cur); } void printfNodes91(STU *head) {STU *p=head->next;while(p!=NULL){printf("%2d%2d",p->coef,p->exp);printf("->");p=p->next;}printf("\n");} STU * GB91(STU * A,STU * B) {STU *C=Init91();STU *p1=A->next;STU * tempA=A;STU *tempB=B;STU*p2=B->next;STU *temp=NULL;while(p2!=NULL&&p1!=NULL){if (p1->exp>p2->exp){temp=Insert91(C,p1->coef,p1->exp);DeleteNode91(tempA,p1);p1=tempA->next;}else if (p1->exp<p2->exp){temp=Insert91(C,p2->coef,p2->exp);DeleteNode91(tempB,p2);p2=tempB->next;}else{if (p1->coef+p2->coef!=0){temp=Insert91(C,p2->coef+p1->coef,p2->exp);DeleteNode91(tempB,p2);p2=tempB->next;DeleteNode91(tempA,p1);p1=tempA->next;}else{p1=p1->next;tempA=tempA->next;p2=p2->next;tempB=tempB->next;}}}if (p2==NULL){temp->next=p1;}else{temp->next=p2;}return C;} void main() {STU * A=Init91();Insert91(A,-4,8);Insert91(A,5,6);Insert91(A,3,4);Insert91(A,-4,1);printfNodes91(A);STU * B=Init91();Insert91(B,5,9);Insert91(B,-5,8);Insert91(B,-3,4);Insert91(B,7,1);printfNodes91(B);printfNodes91(GB91(A,B));system("pause"); }代碼編譯以及運(yùn)行
由于資源上傳太多,資源頻道經(jīng)常被鎖定無(wú)法上傳資源,同學(xué)們可以打開(kāi)VS2013自己創(chuàng)建工程,步驟如下:
1)新建工程
2)選擇工程
3)創(chuàng)建完工程如下圖:
4)增加文件,右鍵點(diǎn)擊項(xiàng)目
5)在彈出菜單里做以下選擇
6)添加文件
7)拷貝代碼與運(yùn)行
程序運(yùn)行結(jié)果
代碼下載
http://download.csdn.net/detail/yincheng01/6681845
解壓密碼:c.itcast.cn
轉(zhuǎn)載于:https://www.cnblogs.com/new0801/p/6177395.html
總結(jié)
以上是生活随笔為你收集整理的基于visual Studio2013解决C语言竞赛题之1091多项式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python——因子分析(KMO检验和B
- 下一篇: 是时候静下心来学点东西了