#include <stdio.h>
#include <string.h>
struct date
{
int year;
int month;
int day;
};
struct contact
{
char name[0x20];
char phone[0x20];
char mail[0x20];
struct date birthday;
};
#ifdef FAKE
void fake_name(char * name, int n)
{
int i, r;
r = rand() % 5 + 8;
for(i = 0; i < r; i++)
{
name[i] = 'a' + rand() % 26;
}
name[r] = '\0';
}
void fake_phone(char * phone, int n)
{
int s[30] = {130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189};
snprintf(phone, 0x20, "%3d%08d", s[rand() % 30], rand() % 100000000);
}
void fake_mail(char * mail, int n)
{
int r;
char * s[10] = {"@live.com", "@hotmail.com", "@msn.com", "@163.com", "@126.com", "@sina.com", "@qq.com", "@263.com", "@gmail.com", "@outlook.com"};
r = rand() % 10;
fake_name(mail, n);
strncat(mail, s[r], 0x20 - strlen(s[r]) - 1);
}
void fake_date(struct date * birthday)
{
int d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
birthday->year = rand() % 111 + 1900;
birthday->month = rand() % 12 + 1;
d[1] += ((birthday->year % 100 != 0) && (birthday->year % 4 == 0) || (birthday->year % 400 == 0));
birthday->day = rand() % d[birthday->month - 1] + 1;
}
void fake_contact(struct contact * data, int * length, int n)
{
int i;
for(i = *length; i < *length + n; i++)
{
fake_name(data[i].name, 0x20);
fake_phone(data[i].phone, 0x20);
fake_mail(data[i].mail, 0x20);
fake_date(&data[i].birthday);
}
(*length) += n;
}
#endif
void input_date(struct date * d)
{
printf("DATE [YYYY-MM-DD] : ");
fflush(stdout);
scanf("%d-%d-%d", &d->year, &d->month, &d->day);
getchar();
}
void output_date(const struct date * d)
{
printf("%04d-%02d-%02d", d->year, d->month, d->day);
}
void input_contact(struct contact * c)
{
printf("NAME : ");
fflush(stdout);
fgets(c->name, 0x20, stdin);
c->name[strlen(c->name) - 1] = 0;
printf("PHONE : ");
fflush(stdout);
fgets(c->phone, 0x20, stdin);
c->phone[strlen(c->phone) - 1] = 0;
printf("MAIL : ");
fflush(stdout);
fgets(c->mail, 0x20, stdin);
c->mail[strlen(c->mail) - 1] = 0;
input_date(&c->birthday);
}
void output_contact(const struct contact * c)
{
printf("%-16s %-16s %-16s ", c->name, c->phone, c->mail);
output_date(&c->birthday);
printf("\n");
}
void add_contact(struct contact * data, int * length)
{
input_contact(data + *length);
(*length)++;
}
void list_contact(const struct contact * data, int length, int selected)
{
int i;
for(i = 0; i < length; i++)
{
printf("[%c%d] ", i == selected ? '*' : ' ', i);
output_contact(data + i);
}
}
void delete_contact(struct contact * data, int * length, int index)
{
int i;
for(i = index; i < *length - 1; i++)
{
data[i] = data[i + 1];
}
bzero(data + *length - 1, sizeof(struct contact));
(*length)--;
}
int compare_by_name(const struct contact * c1, const struct contact * c2)
{
return strcmp(c1->name, c2->name);
}
int compare_by_phone(const struct contact * c1, const struct contact * c2)
{
return strcmp(c1->phone, c2->phone);
}
int compare_by_date(const struct contact * c1, const struct contact * c2)
{
if(c1->birthday.year != c2->birthday.year)
{
return c1->birthday.year - c2->birthday.year > 0 ? 1 : -1;
}
if(c1->birthday.month != c2->birthday.month)
{
return c1->birthday.month - c2->birthday.month > 0 ? 1 : -1;
}
if(c1->birthday.day != c2->birthday.day)
{
return c1->birthday.day - c2->birthday.day > 0 ? 1 : -1;
}
return 0;
}
void swap(struct contact * c1, struct contact * c2)
{
struct contact t;
t = *c1;
*c1 = *c2;
*c2 = t;
}
struct contact * max(struct contact * data, int n, int (*compare)(const struct contact *, const struct contact *))
{
int i;
struct contact * m = data;
for(i = 1; i < n; i++)
{
m = compare(data + i, m) > 0 ? data + i : m;
}
return m;
}
void sort(struct contact * data, int n)
{
int i;
for(i = n; i > 1; i--)
{
swap(max(data, i, compare_by_date), data + i - 1);
}
}
int main()
{
int length = 0;
int selected = -1;
struct contact data[0x100];
char cmd[0x100];
#ifdef FAKE
srand(time(NULL));
#endif
while(1)
{
printf("> ");
scanf("%s", cmd);
fflush(stdout);
if(strcmp(cmd, "exit") == 0)
{
break;
}
else if(strcmp(cmd, "add") == 0)
{
getchar();
add_contact(data, &length);
}
else if(strcmp(cmd, "list") == 0)
{
getchar();
list_contact(data, length, selected);
}
else if(strcmp(cmd, "select") == 0)
{
scanf("%d", &selected);
getchar();
list_contact(data, length, selected);
}
else if(strcmp(cmd, "delete") == 0)
{
getchar();
delete_contact(data, &length, selected);
}
#ifdef FAKE
else if(strcmp(cmd, "fake") == 0)
{
int n;
scanf("%d", &n);
getchar();
fake_contact(data, &length, n);
}
#endif
else if(strcmp(cmd, "sort") == 0)
{
getchar();
sort(data, length);
}
}
return 0;
}
#include <string.h>
struct date
{
int year;
int month;
int day;
};
struct contact
{
char name[0x20];
char phone[0x20];
char mail[0x20];
struct date birthday;
};
#ifdef FAKE
void fake_name(char * name, int n)
{
int i, r;
r = rand() % 5 + 8;
for(i = 0; i < r; i++)
{
name[i] = 'a' + rand() % 26;
}
name[r] = '\0';
}
void fake_phone(char * phone, int n)
{
int s[30] = {130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189};
snprintf(phone, 0x20, "%3d%08d", s[rand() % 30], rand() % 100000000);
}
void fake_mail(char * mail, int n)
{
int r;
char * s[10] = {"@live.com", "@hotmail.com", "@msn.com", "@163.com", "@126.com", "@sina.com", "@qq.com", "@263.com", "@gmail.com", "@outlook.com"};
r = rand() % 10;
fake_name(mail, n);
strncat(mail, s[r], 0x20 - strlen(s[r]) - 1);
}
void fake_date(struct date * birthday)
{
int d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
birthday->year = rand() % 111 + 1900;
birthday->month = rand() % 12 + 1;
d[1] += ((birthday->year % 100 != 0) && (birthday->year % 4 == 0) || (birthday->year % 400 == 0));
birthday->day = rand() % d[birthday->month - 1] + 1;
}
void fake_contact(struct contact * data, int * length, int n)
{
int i;
for(i = *length; i < *length + n; i++)
{
fake_name(data[i].name, 0x20);
fake_phone(data[i].phone, 0x20);
fake_mail(data[i].mail, 0x20);
fake_date(&data[i].birthday);
}
(*length) += n;
}
#endif
void input_date(struct date * d)
{
printf("DATE [YYYY-MM-DD] : ");
fflush(stdout);
scanf("%d-%d-%d", &d->year, &d->month, &d->day);
getchar();
}
void output_date(const struct date * d)
{
printf("%04d-%02d-%02d", d->year, d->month, d->day);
}
void input_contact(struct contact * c)
{
printf("NAME : ");
fflush(stdout);
fgets(c->name, 0x20, stdin);
c->name[strlen(c->name) - 1] = 0;
printf("PHONE : ");
fflush(stdout);
fgets(c->phone, 0x20, stdin);
c->phone[strlen(c->phone) - 1] = 0;
printf("MAIL : ");
fflush(stdout);
fgets(c->mail, 0x20, stdin);
c->mail[strlen(c->mail) - 1] = 0;
input_date(&c->birthday);
}
void output_contact(const struct contact * c)
{
printf("%-16s %-16s %-16s ", c->name, c->phone, c->mail);
output_date(&c->birthday);
printf("\n");
}
void add_contact(struct contact * data, int * length)
{
input_contact(data + *length);
(*length)++;
}
void list_contact(const struct contact * data, int length, int selected)
{
int i;
for(i = 0; i < length; i++)
{
printf("[%c%d] ", i == selected ? '*' : ' ', i);
output_contact(data + i);
}
}
void delete_contact(struct contact * data, int * length, int index)
{
int i;
for(i = index; i < *length - 1; i++)
{
data[i] = data[i + 1];
}
bzero(data + *length - 1, sizeof(struct contact));
(*length)--;
}
int compare_by_name(const struct contact * c1, const struct contact * c2)
{
return strcmp(c1->name, c2->name);
}
int compare_by_phone(const struct contact * c1, const struct contact * c2)
{
return strcmp(c1->phone, c2->phone);
}
int compare_by_date(const struct contact * c1, const struct contact * c2)
{
if(c1->birthday.year != c2->birthday.year)
{
return c1->birthday.year - c2->birthday.year > 0 ? 1 : -1;
}
if(c1->birthday.month != c2->birthday.month)
{
return c1->birthday.month - c2->birthday.month > 0 ? 1 : -1;
}
if(c1->birthday.day != c2->birthday.day)
{
return c1->birthday.day - c2->birthday.day > 0 ? 1 : -1;
}
return 0;
}
void swap(struct contact * c1, struct contact * c2)
{
struct contact t;
t = *c1;
*c1 = *c2;
*c2 = t;
}
struct contact * max(struct contact * data, int n, int (*compare)(const struct contact *, const struct contact *))
{
int i;
struct contact * m = data;
for(i = 1; i < n; i++)
{
m = compare(data + i, m) > 0 ? data + i : m;
}
return m;
}
void sort(struct contact * data, int n)
{
int i;
for(i = n; i > 1; i--)
{
swap(max(data, i, compare_by_date), data + i - 1);
}
}
int main()
{
int length = 0;
int selected = -1;
struct contact data[0x100];
char cmd[0x100];
#ifdef FAKE
srand(time(NULL));
#endif
while(1)
{
printf("> ");
scanf("%s", cmd);
fflush(stdout);
if(strcmp(cmd, "exit") == 0)
{
break;
}
else if(strcmp(cmd, "add") == 0)
{
getchar();
add_contact(data, &length);
}
else if(strcmp(cmd, "list") == 0)
{
getchar();
list_contact(data, length, selected);
}
else if(strcmp(cmd, "select") == 0)
{
scanf("%d", &selected);
getchar();
list_contact(data, length, selected);
}
else if(strcmp(cmd, "delete") == 0)
{
getchar();
delete_contact(data, &length, selected);
}
#ifdef FAKE
else if(strcmp(cmd, "fake") == 0)
{
int n;
scanf("%d", &n);
getchar();
fake_contact(data, &length, n);
}
#endif
else if(strcmp(cmd, "sort") == 0)
{
getchar();
sort(data, length);
}
}
return 0;
}