|
まあこんな感じになりますかね。最大登録件数はDATASIZEです。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 128
#define DATASIZE 1000
typedef struct address {
char szAdd[51]; /* 住所 */
char szName[21]; /* 氏名 */
char szTel[11]; /* 電話番号*/
} Address;
int search(Address *ap, int cnt);
int add(Address *ap, int cnt);
int amend(Address *ap, int cnt);
int del(Address *ap, int cnt);
int printall(Address *ap, int cnt);
int (*func[])(Address *, int) = {search, add, amend, del, printall};
Address ad[DATASIZE];
int main(void)
{
char dummy[BUFSIZE];
int i, cnt = 0;
while (1) {
puts("●メニューを入力して下さい");
puts("1.検索\n2.追加\n3.修正\n4.削除\n5.全体表示\n6.終了");
scanf("%d", &i); gets(dummy);
if (1 <= i && i <= 5) cnt = func[i - 1](ad, cnt);
else if (i == 6) break;
}
return 0;
}
int search(Address *ap, int cnt)
{
char buf[BUFSIZE], dummy[BUFSIZE];
char *target[] = {"住所", "氏名", "電話番号"};
int i, j = 0, count = 0;
Address *t = ap;
while (1) {
puts("何で探しますか?\n1.住所 2.氏名 3.電話番号");
scanf("%d", &i); gets(dummy);
if (1 <= i && i <= 3) break;
}
printf("%sを入力して下さい:", target[--i]);
gets(buf);
while (j++ < cnt) {
switch (i) {
case 0: /* 住所 */
if (!strcmp(t->szAdd, buf)) goto print;
break;
case 1: /* 氏名 */
if (!strcmp(t->szName, buf)) goto print;
break;
case 2: /* 電話番号 */
if (!strcmp(t->szTel, buf)) goto print;
break;
default: /* no condition */
break;
}
t++;
continue;
print:;
printf("%d: 住所:%s 氏名:%s 電話番号:%s\n", j, t->szAdd, t->szName, t->szTel);
t++;
count++;
}
printf("%d件見つかりました\n", count);
return cnt;
}
int add(Address *ap, int cnt)
{
Address *t = ap + cnt;
printf("住所:"); /* 住所 */
gets(t->szAdd);
printf("氏名:"); /* 氏名 */
gets(t->szName);
printf("電話番号:"); /* 電話番号 */
gets(t->szTel);
/*表示*/
/* printf("%s %s %s\n", t->szAdd, t->szName, t->szTel); */
return cnt + 1;
}
int amend(Address *ap, int cnt)
{
Address *t = ap;
char buf[BUFSIZE];
int i, j = 0;
puts("何番を修正しますか?");
scanf("%d", &i); gets(buf);
while (j++ < cnt) {
if (j == i) {
puts("変更しない場合は単に[return]を押して下さい");
printf("住所: %s -> ", t->szAdd);
gets(buf);
if (strlen(buf)) strcpy(t->szAdd, buf);
printf("氏名: %s -> ", t->szName);
gets(buf);
if (strlen(buf)) strcpy(t->szName, buf);
printf("電話番号: %s -> ", t->szTel);
gets(buf);
if (strlen(buf)) strcpy(t->szTel, buf);
return cnt;
} else t++;
}
puts("番号が大きすぎます");
return cnt;
}
int del(Address *ap, int cnt)
{
char buf[BUFSIZE];
int i, j;
puts("何番を削除しますか?");
scanf("%d", &i); gets(buf);
if (i > 0 && i < cnt) {
for (j = i - 1; j < cnt - 1; j++)
ap[j]= ap[j + 1];
return cnt - 1;
} else if (i == cnt) return cnt - 1;
else {
puts("番号が大きすぎます");
return cnt;
}
}
int printall(Address *ap, int cnt)
{
int i = 0;
while (i < cnt) {
printf("%d: 住所:%s 氏名:%s 電話番号:%s\n", ++i, ap->szAdd, ap->szName, ap->szTel);
ap++;
}
return cnt;
}
|