1 / 22

Structure and User-Define Type

Structure and User-Define Type. struct tag { members;…;} variable; typedef old_type new_type ;. Structures 結構變數. Syntax: struct type_id { type1 variable1; type2 variable2; …} varible1, ..; struct type_id variable1, variable2;. 4 bytes. 8 bytes. &a. &b. &ss.

ophira
Download Presentation

Structure and User-Define Type

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. Structure and User-Define Type structtag {members;…;} variable; typedef old_type new_type;

  2. Structures 結構變數 • Syntax: structtype_id { type1 variable1; type2 variable2; …} varible1, ..; structtype_id variable1, variable2;

  3. 4 bytes 8 bytes &a &b &ss 4 bytes 8 bytes &ss.n &ss.x Memory layout 計憶體的配置 • int a; double b; • struct {int n; double x;} ss;

  4. Structure members (example)結構變數之成員 struct item_record { unsigned int number; float price; } pen, book; pen.number = 10; pen.price = 13.5; book.number = 5; book.price = 590.5; total = pen.price * pen.number + book.price * book.number; struct item_record notebook; notebook.price = 60,4; notebook.number = 20; total = total + notebook.price * notebook.number;

  5. User-Type Define (example) 自定新的變數型態 Syntax: (語法) typedef old_type_name new_type_name; typedef unsigned int Uint; typedef struct struct_type new_type_name;

  6. Example 2. 將相關變數集合為一變數結構 typedef struct { char name[80]; unsigned long int id; float t1, t2, t3, avg;} Student; Student sss; // Student 為一新的變數型態 sss.name // name of student. 姓名 sss.id // id number. 學號 sss.t1 // score of first test. 成績 …. sss.avg // average over t1, t2, and t3 平均

  7. While Loop 用 while 控制迴圈 (1) while (condition) { …; …..; …..; } (2) do { …; …; …; …; } while (condition); sum = 0; n = 0; while (n <= 10) { sum = sum + n; n++; } char a; printf(“Press q to quit”;) fflush(stdin); do { a = getc(stdin); } while (a != ‘q’);

  8. File input: getc(file_pointer)從檔案中讀取一個字元 Symbol represents the file pointer of Keyboard: stdin : standard input device, defined within stdio.h getc(stdin) : read a single character from keyboard.

  9. 直角座標 與 球座標 轉換 ex4 typedef struct { double x, y, z;} Cartesian; typedef struct { double r, t, p;} Spherical; Cartesian v1; Spherical v2; v2.r = sqrt(v1.x*v1.x + v1.y*v1.y+v1.z*v1.z); v2.t = acos(v1.z / v2.r); v2.p = atan(v1.y / v1.x);

  10. Practice 2. 增加 expval(x) 的可用範圍 A double variable can represent a value < ~ exp(709) exp(800) = #INF 超出 double 的範圍 Problem : How to increase the applicable range of our previous program?

  11. Numerical Scheme 選擇 a = ln(10) = 2.302585092994045684 選擇 n 使 y=(x – na) , 0 <= y < ln(10)

  12. Flow Chart of the Scheme mytype myexp() main double expval() Input x myexp(x) expval(y) nq = (int)floor(x / LN10) Set n = 30 Call myexp(x) Do Taylor series In reverse order y = x – nq *LN10 Output result return sum return expval(y), nq

  13. Using Netwon’s method find the root of Pratice 5. Another root searching Write the function and its derivative within a same subroutine, And return these values in a two-double structure.

  14. details struct two_double { double x; double y;}; typedef struct two_double twodim; twodim funct(double x) { twodim fdf; ……. fdf.x = ??; fdf.y = ??; return fdf; } int main() { twodim ff; …. do { … ff = funct(x); delta = ff.x / ff.y; } …… }

  15. Related usages or functions • #include <math.h> double floor(double a) ; // return the largest integer that smaller than a. • typedef struct {double x; int n}MyType; For example : floor(2.34) = 2.00, floor(-3.48) = -4.00 MyType myexp(x) { MyType ttt; ……; …..; ttt.n = nq; ttt.x = expval(y); return ttt; }

  16. Project 3. 計算二次方程的根 設計一程式 • 寫一函數讀取系數 a, b, c. 回傳主程式. • 將系數傳到令一函數計算兩複數根. • 將兩複數根傳回主程式 • 再傳到另一副程式列印結果.

  17. 自定新變數格式 • Quardratic typedef struct {double a, b, c;} Quardratic; • Complex typedef struct {double x; double y;} Complex; • TwoComplex typedef struct { Complex r1; Complex r2; } TwoComplex;

  18. functions • 讀取系數, 並回傳到主程式 Quadratic inputcoef(void); • 給與系數計算兩根 TwoComplex findroots(Quadratic coef); • 列印 void printroots(TwoComplex root);

  19. Techniques -- input • Quadratic inputcoef(void) { double a, b, c; Quadratic coef; …. 讀入 a, b, c… coef.a = a; coef.b = b; coef.c = c; return coef; }

  20. Output viod printroots (TwoComplex twor) { printf(“root1 = (%.10lf, %.10lf)\n”, twor.r1.x, twor.r2.y); ……… return; }

  21. Main and prepocessors #include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct {double a, b, c;} Quadratic; typedef struct {double x, y;} Complex; typedef struct {Complex r1, r2;} TwoComplex; Quadratic inputcoef(void); TwoComplex findroots(Quadratic); void printroots(TwoComplex); int main() { Quadratic coefs; TwoComplex roots; coefs = inputcoef(); roots = findroots(coefs); printroots(roots); system("pause"); return 0; } 輸入系數 解根 列印

  22. Complex root1, root2; TwoComplex root; a  cf.a b  cf.b c  cf.c findroot(Quadratic cf) 判別式 crit = b*b – 4*a*c crit >= 0.0 ? no yes crit = sqrt(-crit) crit = sqrt(crit) root1.x = (-b+crit) / (2a) root2.x = (-b-crit) / (2a) root1.y = root2.y = 0.0; root1.x = root2.x = (-b) / (2a) root1.y = crit / (2a) root2.y = (-crit) / (2a); root.r1 = root1; root.r2 = root2; return root;

More Related