#include <stdio.h>
#include <string.h>
struct akb_db
{
char name[30];
char home[10];
int yar;
int mon;
int dy;
};
void sort_al(struct akb_db *, int);
void sort_age(struct akb_db *, int);
void pp(struct akb_db *, int, FILE *);
int main()
{
struct akb_db dat[100];
char buf[100], fn[30], name1[15], name2[15];
int n, m;
FILE *fp;
printf("indata file name = "); for (n
= 0; fgets(buf
, 100, fp
) != NULL
; ++n
) {
sscanf(buf
, "%s %s %s %d %d %d", name1, name2, dat[n].home, &dat[n].yar, &dat[n].mon, &dat[n].dy);
sprintf(dat
[n
].
name, "%s %s", name1
, name2
); }
do
{
printf("1.Sorting in alphabetical order/2.Sorting in order of age.\n"); printf("Please input number:"); }
while (m < 1 || m > 2);
if (m == 1)
sort_al(dat, n);
else
sort_age(dat, n);
return 0;
}
void sort_al(struct akb_db *dat, int n)
{
FILE
*fp
= fopen("member_data_al.dat", "w"); char name_work1[30], name_work2[30], *p;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
strncat(name_work1
, dat
[i
].
name, p
- dat
[i
].
name); strncat(name_work2
, dat
[j
].
name, p
- dat
[j
].
name); if (strcmp(name_work1
, name_work2
) > 0) {
struct akb_db t = dat[i];
dat[i] = dat[j];
dat[j] = t;
}
}
}
pp(dat, n, fp);
}
void sort_age(struct akb_db *dat, int n)
{
FILE
*fp
= fopen("member_data_age.dat", "w"); char ymd1[20], ymd2[20];
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
sprintf(ymd1
, "%d%02d%02d", dat
[i
].
yar, dat
[i
].
mon, dat
[i
].
dy); sprintf(ymd2
, "%d%02d%02d", dat
[j
].
yar, dat
[j
].
mon, dat
[j
].
dy); {
struct akb_db t = dat[i];
dat[i] = dat[j];
dat[j] = t;
}
}
}
pp(dat, n, fp);
}
void pp(struct akb_db *dat, int n, FILE *fp)
{
char buf[100];
for (int i = 0; i < n; ++i)
{
dat[i].name, dat[i].home, dat[i].yar, dat[i].mon, dat[i].dy);
}
}
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gbnVtYmVyOiIpOwogICAgICAgIHNjYW5mKCIlZCIsICZtKTsKICAgIH0KICAgIHdoaWxlIChtIDwgMSB8fCBtID4gMik7CiAgICBpZiAobSA9PSAxKQogICAgICAgIHNvcnRfYWwoZGF0LCBuKTsKICAgIGVsc2UKICAgICAgICBzb3J0X2FnZShkYXQsIG4pOwogICAgcmV0dXJuIDA7Cn0Kdm9pZCBzb3J0X2FsKHN0cnVjdCBha2JfZGIgKmRhdCwgaW50IG4pCnsKICAgIEZJTEUgKmZwID0gZm9wZW4oIm1lbWJlcl9kYXRhX2FsLmRhdCIsICJ3Iik7CiAgICBjaGFyIG5hbWVfd29yazFbMzBdLCBuYW1lX3dvcmsyWzMwXSwgKnA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkKICAgIHsKICAgICAgICBmb3IgKGludCBqID0gaSArIDE7IGogPCBuOyArK2opCiAgICAgICAgewogICAgICAgICAgICBwID0gc3RyY2hyKGRhdFtpXS5uYW1lLCAnICcpOwogICAgICAgICAgICBzcHJpbnRmKG5hbWVfd29yazEsICIlcyIsIHAgKyAxKTsKICAgICAgICAgICAgc3RybmNhdChuYW1lX3dvcmsxLCBkYXRbaV0ubmFtZSwgcCAtIGRhdFtpXS5uYW1lKTsKICAgICAgICAgICAgcCA9IHN0cmNocihkYXRbal0ubmFtZSwgJyAnKTsKICAgICAgICAgICAgc3ByaW50ZihuYW1lX3dvcmsyLCAiJXMiLCBwICsgMSk7CiAgICAgICAgICAgIHN0cm5jYXQobmFtZV93b3JrMiwgZGF0W2pdLm5hbWUsIHAgLSBkYXRbal0ubmFtZSk7CiAgICAgICAgICAgIGlmIChzdHJjbXAobmFtZV93b3JrMSwgbmFtZV93b3JrMikgPiAwKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzdHJ1Y3QgYWtiX2RiIHQgPSBkYXRbaV07CiAgICAgICAgICAgICAgICBkYXRbaV0gPSBkYXRbal07CiAgICAgICAgICAgICAgICBkYXRbal0gPSB0OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcHAoZGF0LCBuLCBmcCk7Cn0Kdm9pZCBzb3J0X2FnZShzdHJ1Y3QgYWtiX2RiICpkYXQsIGludCBuKQp7CiAgICBGSUxFICpmcCA9IGZvcGVuKCJtZW1iZXJfZGF0YV9hZ2UuZGF0IiwgInciKTsKICAgIGNoYXIgeW1kMVsyMF0sIHltZDJbMjBdOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgICB7CiAgICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgbjsgKytqKQogICAgICAgIHsKICAgICAgICAgICAgc3ByaW50Zih5bWQxLCAiJWQlMDJkJTAyZCIsIGRhdFtpXS55YXIsIGRhdFtpXS5tb24sIGRhdFtpXS5keSk7CiAgICAgICAgICAgIHNwcmludGYoeW1kMiwgIiVkJTAyZCUwMmQiLCBkYXRbal0ueWFyLCBkYXRbal0ubW9uLCBkYXRbal0uZHkpOwogICAgICAgICAgICBpZiAoc3RyY21wKHltZDEsIHltZDIpID4gMCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3RydWN0IGFrYl9kYiB0ID0gZGF0W2ldOwogICAgICAgICAgICAgICAgZGF0W2ldID0gZGF0W2pdOwogICAgICAgICAgICAgICAgZGF0W2pdID0gdDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHBwKGRhdCwgbiwgZnApOwp9CnZvaWQgcHAoc3RydWN0IGFrYl9kYiAqZGF0LCBpbnQgbiwgRklMRSAqZnApCnsKICAgIGNoYXIgYnVmWzEwMF07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkKICAgIHsKICAgICAgICBzcHJpbnRmKGJ1ZiwgIiVzICVzICVkICVkICVkIiwKICAgICAgICAgICAgICAgIGRhdFtpXS5uYW1lLCBkYXRbaV0uaG9tZSwgZGF0W2ldLnlhciwgZGF0W2ldLm1vbiwgZGF0W2ldLmR5KTsKICAgICAgICBwcmludGYoIiVzXG4iLCBidWYpOwogICAgICAgIGZwcmludGYoZnAsICIlc1xuIiwgYnVmKTsKICAgIH0KICAgIGZjbG9zZShmcCk7Cn0K