使用者工具

網站工具


c:ch12hw4-full

國立屏東大學 資訊工程學系 程式設計

Contact.c原始程式


#include <stdio.h>
#include "Contact.h"
#include <string.h>

Contact getAContact()
{
    int i=0;
    Contact c;
    char tempName[31]; 
    char *p, tempc;
    
    printf("Name: ");
//    scanf(" %s %s", c.name.firstname, c.name.lastname);
    fgets(tempName,32,stdin);

    p=tempName;
    i=0;
    while(*p!=' ')
    {
        c.name.firstname[i]=*p;
        i++;
        p++;    
    }
    c.name.firstname[i]='\0';
    i=0;
    p++;
    while(*p!='\n')
    {
        c.name.lastname[i]=*p;
        i++;
        p++;
    }
    c.name.lastname[i]='\0';
  
    printf("Gender (M/F): ");

    scanf(" %c",&tempc);
    
    if(tempc=='F') 
        c.gender=Female;
    else if(tempc=='M') 
        c.gender=Male;
    
    short month, day, year; 
    printf("Birthday (YYYY/MM/DD): ");

    scanf(" %hd/%hd/%hd", &year, &month, &day);

    c.birthday.month = (month-1);    
    c.birthday.day=day;
    c.birthday.year=year;

    printf("Phone Type (L/M): ");
    scanf(" %c",&tempc);
     
    if(tempc=='L')
    {
        c.phonetype=LandLine;
        char area[4];
        char number[8];
        printf("Number: ");
        scanf(" (%[^)])%s",area, number);
        strcpy(c.phone.landline.areacode, area);    
        strcpy(c.phone.landline.number, number);
    }
    else if(tempc=='M')
    {
        c.phonetype=MobilePhone;
        char number[11];
        printf("Number: ");
        scanf(" %s", number);
        strcpy(c.phone.mobile.number, number);
        printf("Carrier (C/T/F): ");
        scanf(" %c", &tempc);
        
        if(tempc=='C')
            c.phone.mobile.carrier=CHT;
        else if(tempc=='T')
            c.phone.mobile.carrier=TWN;
        else if(tempc=='F')
            c.phone.mobile.carrier=FET;
    }
    
    printf("Address: ");
    scanf(" %[^\n]",c.address);
    getchar();
    
    return c;
}

void showDate(Date d)
{
    char months[][10]={"January", "February", "March", "April", "May", "June",
             "July","August", "September", "October", "November", "December"};

    printf("%s %d", months[d.month], d.day);
    switch(d.day)
    {
        case 1:
        case 21:
        case 31:
            printf("st");
            break;
        case 2:
        case 22:
            printf("nd");
            break;
        case 3:
        case 23:
            printf("rd");
            break;
        default:
            printf("th");
            break;
    }
    printf(", %d", d.year);         
}

void showAContact(Contact c)
{
    printf("%s %s (", c.name.firstname, c.name.lastname);
    if(c.gender==Male)
        printf("Male");
    else
        printf("Female");
    printf(") ");
    showDate(c.birthday);
    if(c.phonetype==LandLine)
        printf(", (%s)%s,", c.phone.landline.areacode, c.phone.landline.number);
    else
    {
        printf(", %s(", c.phone.mobile.number);
        if(c.phone.mobile.carrier==CHT)
            printf("ChungHwa Telecom");
        else if(c.phone.mobile.carrier==TWN)
            printf("Taiwan Mobile");
        else
            printf("FarEasTone");
        printf("),");
    }
    printf(" %s\n", c.address);
}

int compareAge(Contact a, Contact b)
{
    // return 1 if a is older
    // return 0 if a and b have the same birthday
    // return -1 if b is older
    int adays, bdays;
    adays=a.birthday.year*365+a.birthday.month*30+a.birthday.day;
    bdays=b.birthday.year*365+b.birthday.month*30+b.birthday.day;
    
    if(adays>bdays)
        return -1;
    else if(adays<bdays)
        return 1;
    return 0;
}

void sortContacts(Contact contacts[])
{
    int i,j;
    
    for(i=0;i<numContact-1;i++)
    {
        for(j=0;j<numContact-i-1;j++)
        {
            if( compareAge(contacts[j],contacts[j+1])==1)
            {
                Contact temp;
                temp=contacts[j];
                contacts[j]=contacts[j+1];
                contacts[j+1]=temp;
            }
        }
    }
}

c/ch12hw4-full.txt · 上一次變更: 2022/03/30 14:44 由 junwu

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki