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
下一篇:苹果提醒事项赌博:风险与后果
告别遗忘:电脑定时提醒全攻略,从系统内置到专业工具,助你效率倍增!
https://www.weitishi.com/remind/129796.html
高安解封短信:一条通知背后的城市智慧、信息力量与社会信任
https://www.weitishi.com/remind/129795.html
智能版本更新提醒器:告别手动繁琐,一键下载畅享安全高效软件体验
https://www.weitishi.com/remind/129794.html
告别遗忘症与拖延症:短信、任务、提醒,你的高效生产力秘密武器
https://www.weitishi.com/remind/129793.html
苹果日历深度指南:告别遗忘,轻松掌控你的日程与提醒
https://www.weitishi.com/remind/129792.html
热门文章
微信双开通知无声音提醒?手把手教你开启,不错过重要消息!
https://www.weitishi.com/remind/23592.html
快递总是没有短信提醒?教你4招,从此告别错过包裹
https://www.weitishi.com/remind/26507.html
高德导航设置提醒功能,轻松无忧出行
https://www.weitishi.com/remind/16680.html
联通卡总收到短信提醒?教你一步步解决
https://www.weitishi.com/remind/51189.html
农信短信提醒扣费吗?揭秘背后的真相
https://www.weitishi.com/remind/14719.html