Presentation is loading. Please wait.

Presentation is loading. Please wait.

複習 char name1[20]; //長度為20的字串 char name2[20]; //讀取字串 gets(name1);

Similar presentations


Presentation on theme: "複習 char name1[20]; //長度為20的字串 char name2[20]; //讀取字串 gets(name1);"— Presentation transcript:

1 複習 char name1[20]; //長度為20的字串 char name2[20]; //讀取字串 gets(name1);
fflush(stdin); scanf(“%d”,name2); //列印 puts(name1); //會自動加上換行 printf(“%s\n”,name2);

2 複習 //處理字串的函數 If(!strcmp(name1,name2)){ //name1和name2一樣時會執行 }
strcat(name1,name2);//將name2接在name1 //的後面 strcpy(name1,name2); //將name2的內容 //copy到name1

3 複習 char ch; //讀取字元 ch=getchar(); ch=getch(); scanf(“%c”,&ch); //印出字元
putchar(ch); putch(ch); printf(“%c”,ch);

4 複習 ch=toupper(ch);//轉大寫 If(isalpha(ch)){ ch=tolower(ch);//轉大寫
} If(isdigit(ch)){ //ch是0~9會執行 If(isupper(ch)){ //ch是英文大寫字母會執行 If(islower(ch)){ //ch是英文小寫字母會執行

5 Class 7 結構

6 想一想 如果今天我們是公司負責人力資源的部門,老闆可能要求要處理20個員工的資料,資料的內容包括員工姓名、性別與薪資,在程式語言中應該要如何定義呢? char employee[20][10], employee_gender[20]; int employee_salary[20]; 如果每一個員工有更多的資料項目,怎麼辦?

7 結構 像這樣彼此之間有較密切相關性的資料,C語言提供了struct這種衍生的資料型態 和陣列不同處:可以用來結合不同資料型態的元素

8 範例 struct employee_data_type //定義結構部份 { char name[10], gender;
int salary; }; //注意這裡要加分號 struct employee_data_type employee; //employee才是個變數

9 結構定義 struct structure_tag // structure_tag 是個結構的名字 {
data members declarations; }; //註: structure_tag, member_name, variable_name 可以是相同的 struct myType{ int myType; }myType;

10 變數宣告 直接寫在定義結構的時候 像平常宣告變數的情況 可以是省略結構定義的structure_tag
struct student_type student[10]; struct student_type { int id, class; }student1[10]; struct }student2[10]; 像平常宣告變數的情況 struct employee_data_type employee;

11 初始化 struct employee_data_type employee={"Yang",'M',20000}; //初始化
注意:要按照結構定義中成員的宣告順序來給值(由上到下,由左至右)

12 結構的運算 關於結構合法的運算有下列四種: 指定結構變數給另一個相同型態的結構變數 (=) 取得結構變數的位址 (&)
存取結構的成員 (.) 與 (->) 使用sizeof()來得到結構變數的大小

13 結構的取用方式 結構成員運算子(.) //variable.member_name; 結構指標成員運算子(->)
printf("The employee's name is %s.\n",employee.name); printf("The employee's salary is %d.\n",employee.salary); 結構指標成員運算子(->) 如果取用的是一個結構指標變數,可以使用(->)運算子。 struct employee_data_type *ptr_employee=&employee; printf("The employee's name is %s.\n", (*ptr_employee).name); printf("The employee's name is %s.\n", ptr_employee->name); printf("The employee's name is %s.\n", (&employee)->name); //兩種取用的方式是一樣的,由於成員運算子 (.)/(->) 的結合優先權比提領/取址運算子 (*)/(&) 還高,所以要記得用 () 括起來。

14 範例練習 利用結構,定義一個學生個人資料的資料型態Student_Person_Data
成員有字元陣列的姓名、整數型態的年齡、字元陣列的地址以及興趣 試著在程式中指定初始值給這個資料型態的stu變數,並將其印出

15 結構陣列 宣告 字串輸入 整數輸入 字串輸出 整數輸出 struct employee_data_type employee2[5];
strcpy(employee2[0].name,"Mary"); 整數輸入 employee2[0].salary=1000; 字串輸出 printf("The employee's name is %s.\n",employee2[0].name); 整數輸出 printf("The employee's salary is %d.\n",employee2[0].salary);

16 範例練習 請設計一個學生資料表,需求欄位為: Name:字串長度10 Gender:字元 Age:整數 Favorite:字串長度20
Grade:浮點數 請定義一個student_type的結構,宣告變數,指定值,並輸出 範例程式見下頁

17

18 巢狀結構 結構可以不只一層,想要描述複雜的事物,我們可以用雙層以上的結構來描述。 取用方式相似: 巢狀部件1: 巢狀部件2: 巢狀主體:
struct engine_type { int capacity; char num_cylinders; }; 巢狀部件2: struct body_type { char color; int num_doors; 巢狀主體: struct car_type { struct engine_type engine; struct body_type body; } car; 取用方式相似: car.engine.capacity=1500; car.body.num_doors = 4;

19 範例練習 三角形的三個頂點之座標 一個三角形的自訂資料型態由三個頂點成員所構成,而座標本身也是一個結構,有三個成員:x,y,z

20 範例練習 請修改此程式,使之可以讀入一個三角形的三個點座標,並用座標格式輸出

21 結構的記憶體大小 結構資料型態的變數所占用的記憶體大小,取決於其結構定義中每個成員的資料型態和成員的個數
C語言有一個函式叫做sizeof(),可以用來看每個型態會佔用多少位元組 利用這個函式來驗證結構資料型態變數的記憶體大小

22 範例練習 利用sizeof()看看各種資料型態的占用位元組數,並看看結構資料型態的大小

23 簡化struct的定義語法-typedef
Case 1 如何把原來的宣告方式加上一個新的"暱稱"? struct employee_data_type //已經先宣告好了 { char name[10], gender; int salary; }; typedef struct employee_data_type EMPLOYEE; //加這個typedef EMPLOYEE employee={"Yang",'M',20000}; EMPLOYEE employee2; //以後拿EMPLOYEE來定義就可以, //當然還是可以繼續使用struct employee_data_type來定義。

24 typedef Case2 可以像以下這種寫法: typedef struct {
char name[10], gender; int salary; }EMPLOYEE; //這樣跟Case1定義出EMPLOYEE的方式一樣, //不過系統就不知道struct employee_data_type。

25 enum 定義一些有彼此有相關性的常數 這樣其實四個常數會對應到0、1、2、3,但是寫程式時不必特別記得 例如在程式中可以這樣寫:
enum COLOR { BLUE, YELLOW, RED, PINK }; 這樣其實四個常數會對應到0、1、2、3,但是寫程式時不必特別記得 例如在程式中可以這樣寫: COLOR color = YELLOW; switch(color) case BLUE: printf("藍色\n"); break; case YELLOW: printf("黃色\n"); break; case RED: printf("紅色\n"); break; case PINK: printf("粉紅色\n"); break; default: printf("不知道\n"); break; }

26 範例練習 定義enum列舉資料型態,名稱為card 定義card列舉成員有:JCB常數值為35、VISA常數值45、MASTER常數值為54

27 malloc+struct struct myType{ int a; int b; }; int main(){ myType *s;
int size=5; s=(myType*)malloc(sizeof(myType)*size); s->a=99; s->b=80; printf("%d %d",s->a,s->b); system("pause"); }

28 作業八 定義名稱為stu_score的struct結構資料型態,該結構含有name(姓名),chi(國文),eng(英文),math(數學),avg(平均)五個欄位 宣告屬於stu_score結構變數 由鍵盤輸入資料並指定給該結構的name、chi、eng、math的欄位 求出三科目的平均指定給avg欄位 顯示此結構變數中name及avg欄位的內容


Download ppt "複習 char name1[20]; //長度為20的字串 char name2[20]; //讀取字串 gets(name1);"

Similar presentations


Ads by Google