帮我写一个复杂的祝朋友生日的c语言代码
时间: 2023-08-20 12:40:20 浏览: 90
以下是一个更为复杂一些的C语言代码,用于祝福朋友生日快乐,并且附带一些特殊的祝福语句:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char name[20];
int age, birthday, birthmonth, birthyear;
char *blessings[] = {"祝你生日快乐,心想事成!",
"愿你每天都能感受到幸福和快乐!",
"希望你在新的一岁里,能够实现自己的梦想!",
"祝你健康、快乐、平安!",
"生日快乐!让我们一起喝彩庆祝吧!",
"今天是你的生日,你是最棒的!",
"愿你的生日充满温馨、快乐和爱!",
"祝你拥有一个美好的生日,充满温馨和惊喜!",
"愿你的生日充满阳光和希望!",
"今天是你的生日,愿你的未来更加美好!"};
// 获取当前时间
time_t now = time(NULL);
struct tm *t = localtime(&now);
int current_day = t->tm_mday;
int current_month = t->tm_mon + 1;
int current_year = t->tm_year + 1900;
printf("请输入朋友的姓名:");
scanf("%s", name);
printf("请输入朋友的生日(格式为:日 月 年):");
scanf("%d %d %d", &birthday, &birthmonth, &birthyear);
// 计算年龄
age = current_year - birthyear;
if (birthmonth > current_month || (birthmonth == current_month && birthday > current_day)) {
age--;
}
// 打印祝福语
printf("\n亲爱的%s,", name);
if (current_month == birthmonth && current_day == birthday) {
printf("今天是你的生日,祝你生日快乐!\n");
} else {
printf("虽然今天不是你的生日,但我还是想祝你生日快乐!\n");
}
printf("你已经%d岁了,", age);
if (age < 18) {
printf("你还是个年轻人,但是你的未来一定会充满可能性!\n");
} else if (age < 30) {
printf("你已经成为了一个独立自主的成年人,希望你能够实现自己的梦想!\n");
} else if (age < 50) {
printf("你已经有了一定的人生经验,希望你的未来更加美好!\n");
} else {
printf("你是一个非常有智慧和经验的人,希望你能够继续保持年轻的心态!\n");
}
// 随机选择一条祝福语句
srand(time(NULL));
int random_index = rand() % 10;
printf("%s\n", blessings[random_index]);
return 0;
}
```
这个代码加入了一些特殊的祝福语句,并且会根据朋友的生日计算年龄,并输出适合不同年龄段的祝福语句。另外,代码还会根据当前时间判断今天是否是朋友的生日,并输出相应的祝福语句。最后,代码还会随机选择一条祝福语句,以增加祝福的趣味性。