國立屏東大學 資訊工程系 程式設計(二)
使用者自定資料型態
turnin code c.hw13eb c.hw13
due date: April 1, 13:29
練習使用自定資料型態
設計一個程式用以管理10個聯絡人資訊。每個聯絡人包含以下資訊:
下列標頭檔已定義了相關的結構體、列舉資料型別與union:
#include <stdio.h>
#include <string.h>
#define numContact 10
typedef enum {Male, Female} Gender;
typedef enum {January, February, March, April, May, June, July,
August, September, October, November, December} Month;
typedef struct
{
Month month;
short day;
short year;
} Date;
typedef struct
{
char firstname[20];
char lastname[10];
} Name;
typedef enum {CHT, TWN, FET} Carrier;
typedef struct
{
char number[10];
Carrier carrier;
} Mobile;
typedef struct
{
char areacode[4];
char number[8];
} Landline;
typedef enum {LandLine, MobilePhone} PhoneType;
typedef struct
{
Name name;
Gender gender;
Date birthday;
PhoneType phonetype;
union
{
Landline landline;
Mobile mobile;
} phone;
char address[50];
} Contact;
Contact getAContact();
void showAContact(Contact c);
void sortContacts(Contact cs[]);
請設計一個新的程式,其名稱為contact.c。完成「getAContact()」、「showAContact()」與「sortContacts()」函式之實作,並且使用下列在main.c中的「main()」函式內容來執行聯絡人資料的讀取、排序以及輸出:
#include "contact.h"
int main()
{
int i;
Contact mycontacts[numContact];
for(i=0;i<numContact;i++)
mycontacts[i]=getAContact();
sortContacts(mycontacts);
for(i=0;i<numContact;i++)
showAContact(mycontacts[i]);
return 0;
}
其中「numContact」定義了聯絡人的個數(#define numContact 10)。你可以視需要增加新的函式及程式碼,但不可以變動contact.h檔案內容以及main.c中的內容。
[9:19 user@ws hw13] ./a.out
Name: Amy Wang <---- 一律為firstname lastname
Gender (M/F): F
Birthday (YYYY/MM/DD): 1977/3/5 <---- 假設使用者不會輸入錯誤的日期
Phone Type (L/M): L
Number: (08)7238700 <---- 區碼用括號標示
Address: No.51, Mingsheng E. Rd., Pingtung City
Name: Cheng-Kung Liu
Gender (M/F): M
Birthday (YYYY/MM/DD): 1995/12/3
Phone Type (L/M): M
Number: 0918123456
Carrier (C/T/F): T
Address: No.99, ChongHui St., Taipei City
.... 略, 排序後(依birthday排序,年紀由小到大輸出),輸出如下:
Cheng-Kung Liu (Male) December 3rd, 1995, 0918123456(Taiwan Mobile), No. 99, ChongHui St., Taipei City.
Amy Wang (Female) March 5th, 1977, (08)7238700, No.51, Mingsheng E. Rd., Pingtung City.
... 略
[9:19 user@ws hw13]
注意:
提示: