Structure and UserDefine Type struct tag members variable
Structure and User-Define Type struct tag {members; …; } variable; typedef old_type new_type;
Structures 結構變數 n Syntax: struct type_id { type 1 variable 1; type 2 variable 2; …} varible 1, . . ; struct type_id variable 1, variable 2;
Memory layout 計憶體的配置 n int a; double b; 4 bytes &a 8 bytes &b n struct {int n; double x; } ss; &ss 4 bytes &ss. n 8 bytes &ss. x
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;
User-Type Define (example) 自定新的變數型態 Syntax: (語法) typedef old_type_name new_type_name; typedef unsigned int Uint; typedef struct_type new_type_name;
Example 2. 將相關變數集合為一變數結構 typedef struct { char name[80]; unsigned long int id; float t 1, t 2, t 3, avg; } Student; Student sss; // Student 為一新的變數型態 sss. name sss. id sss. t 1 …. sss. avg // name of student. 姓名 // id number. 學號 // score of first test. 成績 // average over t 1, t 2, and t 3 平均
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’);
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.
直角座標 與 球座標 轉換 ex 4 typedef struct { double x, y, z; } Cartesian; typedef struct { double r, t, p; } Spherical; Cartesian v 1; Spherical v 2; v 2. r = sqrt(v 1. x*v 1. x + v 1. y*v 1. y+v 1. z*v 1. z); v 2. t = acos(v 1. z / v 2. r); v 2. p = atan(v 1. y / v 1. x);
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?
Numerical Scheme 選擇 a = ln(10) = 2. 302585092994045684 選擇 n 使 y=(x – na) , 0 <= y < ln(10)
Flow Chart of the Scheme main Input x mytype myexp() myexp(x) double expval() expval(y) nq = (int)floor(x / LN 10) Set n = 30 y= x – nq *LN 10 Do Taylor series In reverse order return expval(y), nq return sum Call myexp(x) Output result
Pratice 5. Another root searching Using Netwon’s method find the root of Write the function and its derivative within a same subroutine, And return these values in a two-double structure.
details struct two_double { double x; double y; }; typedef struct two_double twodim; twodim funct(double x) { twodim fdf; ……. fdf. x = ? ? ; int main() fdf. y = ? ? ; { twodim ff; return fdf; } …. do { … ff = funct(x); delta = ff. x / ff. y; } …… }
Related usages or functions n #include <math. h> double floor(double a) ; // return the largest integer that smaller than a. For example : floor(2. 34) = 2. 00, floor(-3. 48) = -4. 00 n typedef struct {double x; int n} My. Type; My. Type myexp(x) { My. Type ttt; ……; …. . ; ttt. n = nq; ttt. x = expval(y); return ttt; }
自定新變數格式 1. Quardratic typedef struct {double a, b, c; } Quardratic; 2. Complex typedef struct {double x; double y; } Complex; 3. Two. Complex typedef struct { Complex r 1; Complex r 2; } Two. Complex;
functions 1. 讀取系數, 並回傳到主程式 Quadratic inputcoef(void); 2. 給與系數計算兩根 Two. Complex findroots(Quadratic coef); 3. 列印 void printroots(Two. Complex root);
Techniques -- input n Quadratic inputcoef(void) { double a, b, c; Quadratic coef; …. 讀入 a, b, c… coef. a = a; coef. b = b; coef. c = c; return coef; }
Output viod printroots (Two. Complex twor) { printf(“root 1 = (%. 10 lf, %. 10 lf)n”, twor. r 1. x, twor. r 2. y); ……… return; }
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 r 1, r 2; } Two. Complex; Quadratic inputcoef(void); Two. Complex findroots(Quadratic); void printroots(Two. Complex); int main() { Quadratic coefs; Two. Complex roots; coefs = inputcoef(); roots = findroots(coefs); printroots(roots); 列印 system("pause"); return 0; }
Complex root 1, root 2; Two. Complex root; a cf. a b cf. b c cf. c findroot(Quadratic cf) 判別式 crit = b*b – 4*a*c no crit >= 0. 0 ? yes crit = sqrt(crit) crit = sqrt(-crit) root 1. x = (-b+crit) / (2 a) root 2. x = (-b-crit) / (2 a) root 1. y = root 2. y = 0. 0; root 1. x = root 2. x = (-b) / (2 a) root 1. y = crit / (2 a) root 2. y = (-crit) / (2 a); root. r 1 = root 1; root. r 2 = root 2; return root;
- Slides: 22