1 / 8

資料結構實習 - 四

資料結構實習 - 四. 多項式的結構. 使用 typedef 建立 ploynomial 的結構 #define MAX_TERMS 100 typedef struct { float coef; int exp; }polynomial;. 從檔案輸入資料. 用 FILE 型態宣告一個 file pointer 開檔 FILE *fopen ( “ 檔案名稱” , “ 開檔型式” ) 抓資料 int fgetc ( FILE * ) fgets ( char * , int , FILE * ) End-of-file

ringo
Download Presentation

資料結構實習 - 四

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 資料結構實習-四

  2. 多項式的結構 • 使用typedef建立ploynomial的結構 #define MAX_TERMS 100 typedef struct { float coef; int exp; }polynomial;

  3. 從檔案輸入資料 • 用FILE型態宣告一個 file pointer • 開檔 • FILE *fopen ( “檔案名稱” , “開檔型式” ) • 抓資料 • int fgetc ( FILE * ) • fgets ( char * , int , FILE * ) • End-of-file • int feof ( FILE * ) • 關檔 • fclose ( FILE * )

  4. 多項式的存法 • 把兩個多項式A(x)= 和B(x)= 儲存在陣列中。 Polynomial term[MAX]; start A start B finish B finish A avail 0 1 2 3 4 5 6 start A,start B :表示A,B的第一項的index。 finish A ,finish B :表示A,B的最後一項index。 avail :表示陣列中下一個可用的空閒位置之索引。

  5. 多項式的乘法 –演算法 • A(X) = X2 + 2X + 1 • B(X) = X3 + 2X + 3 X2 + 2X + 1 X3 + 2X + 3 * X5 + 2X4 + X3 + 2X3 + 4X2 + 2X 3X2 + 6X + 3 + A(X) B(X) A(X) *X3 [1] A(X) *2X [2] [1]+[2] [3] A(X) *3 [4] [3]+[4]

  6. 練習1-減法 • 以型態ploynomial為基礎完成多項式減法。 D(x)=A(x)-B(x) void psub(int startA ,int finishA ,int startB, int finishB ,int *startD ,int *finishD)

  7. 輸出至檔案 • 把傳入的多項式輸出至檔案 void print(int startD ,int finishD ) • fprintf( FILE * , printf的格式 ) • fprintf( fptr ,“%d%d” , x , y );

  8. 練習2-乘法 • 以型態ploynomial為基礎完成多項式乘法 D(x)=A(x)*B(x) void pmul(int startA ,int finishA ,int startB, int finishB ,int *startD ,int *finishD)

More Related