C 语言生日提醒代码149


在本文中,我们将探讨如何使用 C 语言编写一个生日提醒程序。这个程序将提醒您朋友和家人的生日,并允许您轻松管理生日列表。

数据结构

首先,我们需要建立一个数据结构来存储生日信息。我们将使用一个结构体来表示每个生日:```c
struct birthday {
char name[50]; // 姓名
int day; // 日期
int month; // 月份
};
```

初始化生日列表

接下来,我们将初始化生日列表。我们可以使用一个常量数组来存储默认的生日信息,例如:```c
const struct birthday birthdays[] = {
{"John Doe", 15, 11},
{"Jane Doe", 12, 3},
{"Alice Smith", 24, 6},
};
```

这个数组包含三个生日条目。您可以根据需要添加或删除条目。

主函数

在主函数中,我们将执行以下步骤:1. ```c
printf("欢迎使用生日提醒程序!");
```
2. ```c
int choice;
do {
// 显示菜单
printf("选择一个选项:");
printf("1. 添加生日");
printf("2. 删除生日");
printf("3. 显示生日列表");
printf("4. 退出");
scanf("%d", &choice);

// 根据选择执行相应的操作
switch (choice) {
case 1:
addBirthday();
break;
case 2:
deleteBirthday();
break;
case 3:
showBirthdays();
break;
case 4:
printf("谢谢使用!再见。");
exit(0);
default:
printf("无效的选择。请再试一次。");
}
} while (choice != 4); // 4 为退出选项
```

添加生日

在 `addBirthday()` 函数中,我们将提示用户输入生日信息,然后将其添加到生日列表中:```c
void addBirthday() {
struct birthday newBirthday;

printf("请输入姓名:");
scanf(" %s", ); // 注意前面的空格,避免输入空白字符

printf("请输入日期(1-31):");
scanf("%d", &);
printf("请输入月份(1-12):");
scanf("%d", &);

// 将新生日添加到列表中
birthdays[numBirthdays++] = newBirthday;
printf("生日已添加成功!");
}
```

删除生日

在 `deleteBirthday()` 函数中,我们将提示用户输入要删除的姓名,然后将其从生日列表中删除:```c
void deleteBirthday() {
char name[50];
int found = 0;

printf("请输入要删除的姓名:");
scanf(" %s", name); // 注意前面的空格,避免输入空白字符

for (int i = 0; i < numBirthdays; i++) {
if (strcmp(birthdays[i].name, name) == 0) {
// 找到匹配的生日
for (int j = i; j < numBirthdays - 1; j++) {
// 将后面的生日向前移动一位
birthdays[j] = birthdays[j + 1];
}
numBirthdays--;
found = 1;
break;
}
}

if (found) {
printf("生日已删除成功!");
} else {
printf("找不到该生日。");
}
}
```

显示生日列表

在 `showBirthdays()` 函数中,我们将循环遍历生日列表并打印出每个生日信息:```c
void showBirthdays() {
if (numBirthdays == 0) {
printf("生日列表为空。");
} else {
printf("生日列表:");
for (int i = 0; i < numBirthdays; i++) {
printf("%s:%d-%d", birthdays[i].name, birthdays[i].day, birthdays[i].month);
}
}
}
```

完整代码

以下是完整的 C 语言生日提醒代码:```c
#include
#include
struct birthday {
char name[50];
int day;
int month;
};
const struct birthday birthdays[] = {
{"John Doe", 15, 11},
{"Jane Doe", 12, 3},
{"Alice Smith", 24, 6},
};
int numBirthdays = sizeof(birthdays) / sizeof(birthdays[0]);
int main() {
printf("欢迎使用生日提醒程序!");

int choice;
do {
printf("选择一个选项:");
printf("1. 添加生日");
printf("2. 删除生日");
printf("3. 显示生日列表");
printf("4. 退出");
scanf("%d", &choice);

switch (choice) {
case 1:
addBirthday();
break;
case 2:
deleteBirthday();
break;
case 3:
showBirthdays();
break;
case 4:
printf("谢谢使用!再见。");
exit(0);
default:
printf("无效的选择。请再试一次。");
}
} while (choice != 4);

return 0;
}
void addBirthday() {
struct birthday newBirthday;

printf("请输入姓名:");
scanf(" %s", );

printf("请输入日期(1-31):");
scanf("%d", &);
printf("请输入月份(1-12):");
scanf("%d", &);

birthdays[numBirthdays++] = newBirthday;
printf("生日已添加成功!");
}
void deleteBirthday() {
char name[50];
int found = 0;

printf("请输入要删除的姓名:");
scanf(" %s", name);

for (int i = 0; i < numBirthdays; i++) {
if (strcmp(birthdays[i].name, name) == 0) {
for (int j = i; j < numBirthdays - 1; j++) {
birthdays[j] = birthdays[j + 1];
}
numBirthdays--;
found = 1;
break;
}
}

if (found) {
printf("生日已删除成功!");
} else {
printf("找不到该生日。");
}
}
void showBirthdays() {
if (numBirthdays == 0) {
printf("生日列表为空。");
} else {
printf("生日列表:");
for (int i = 0; i < numBirthdays; i++) {
printf("%s:%d-%d", birthdays[i].name, birthdays[i].day, birthdays[i].month);
}
}
}
```

2025-02-06


上一篇:如何使用学习提醒器软件提升学习效率

下一篇:苹果提醒事项赌博:风险与后果