You are not allowed to perform this action
c:homework:hw14
國立屏東大學 資訊工程系 程式設計(二)
作業13
使用者自定資料型態
turnin code c.hw13eb c.hw13
due date: April 1, 13:29
目的
練習使用自定資料型態
第1題
設計一個程式用以管理10個聯絡人資訊。每個聯絡人包含以下資訊:
- name
- gender
- birthday
- phonetype
- phone
- address
下列標頭檔已定義了相關的結構體、列舉資料型別與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]
注意:
- 在電信業者方面,中華電信以Chunghwa Telecom表示,遠傳以FarEasTone表示,台灣大哥大以Taiwan Mobile表示。
- 程式執行的輸出格式必須與上述一致。
- 題目中的「contact.h」及「main.c」可在「/home/stu/public/c/2016SCHW13」下載。
- 此題僅需turnin一個檔案「contact.c」
提示:
- 在撰寫此程式時,你可以暫時將numContact設定為2~3,以便進行測試。
- 你也可以將10筆資料先存成文字檔,以導入(「< data.txt」)的方式進行測試。
c/homework/hw14.txt · 上一次變更: 2019/07/02 15:01 由 127.0.0.1
