鱼C论坛

 找回密码
 立即注册
查看: 2695|回复: 19

[已解决]C语言数组03(生命计算器1)

[复制链接]
发表于 2024-1-6 16:14:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 liyifeng295641 于 2024-1-8 10:56 编辑

1. 写一个生命计算器,要求用户输入生日,显示他在这个世界上活了多少天?

程序实现如下图:


                               
登录/注册后可看大图


@FishC这道题不会写,可以给点提示吗?
最佳答案
2024-1-6 16:36:04
  1. #include <iostream>
  2. #include <chrono>
  3. #include <sstream>
  4. #include <iomanip>

  5. using namespace std;

  6. chrono::system_clock::time_point parse_date(const string &date_str) {
  7.     tm tm = {};
  8.     istringstream ss(date_str);
  9.     ss >> get_time(&tm, "%Y-%m-%d");
  10.     return chrono::system_clock::from_time_t(mktime(&tm));
  11. }

  12. int main() {
  13.     string birth_date_str, current_date_str;
  14.     cout << "请输入您的生日(格式:YYYY-MM-DD): ";
  15.     cin >> birth_date_str;

  16.     cout << "请输入当前日期(格式:YYYY-MM-DD): ";
  17.     cin >> current_date_str;

  18.     auto birth_date = parse_date(birth_date_str);
  19.     auto current_date = parse_date(current_date_str);

  20.     auto duration = chrono::duration_cast<chrono::hours>(
  21.                         current_date - birth_date).count() / 24;

  22.     cout << "您在这个世界上活了 " << duration << " 天。" << endl;

  23.     return 0;
  24. }
复制代码


好了,求最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-1-6 16:17:33 | 显示全部楼层

鸡哥,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-6 21:03:36 | 显示全部楼层
FishC 发表于 2024-1-6 16:14
当计算一个人活了多少天时,我们需要知道他的出生日期和当前日期。然后,我们可以使用日期函数来计算天数差 ...
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         long count = 0; // count用于存放一共活了多少天
  5.         int year1, year2; // year1是你的生日年份,year2是今天的年份
  6.         int month1, month2;
  7.         int day1, day2;
  8.         int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  9.         printf("请输入你的生日(如1988-05-20):");
  10.         scanf("%d-%d-%d", &year1, &month1, &day1);

  11.         printf("请输入今年日期(如2016-03-28):");
  12.         scanf("%d-%d-%d", &year2, &month2, &day2);

  13.         while (year1 <= year2)
  14.         {
  15.                 days[1] = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
  16.                 while (month1 <= 12)
  17.                 {
  18.                         while (day1 <= days[month1 - 1])
  19.                         {
  20.                                 if (year1 == year2 && month1 == month2 && day1 == day2)
  21.                                 {
  22.                                         goto FINISH; // 跳出多层循环才被迫用goto语句
  23.                                 }
  24.                                 day1++;
  25.                                 count++;
  26.                         }
  27.                         day1 = 1;
  28.                         month1++;
  29.                 }
  30.                 month1 = 1;
  31.                 year1++;
  32.         }

  33. FINISH: printf("你在这个世界上总共生存了%d天\n", count);

  34.         return 0;
  35. }
复制代码

这是小甲鱼给的代码,你能给点解析吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-6 21:05:43 | 显示全部楼层
FishC 发表于 2024-1-6 21:04
这段代码是一个计算一个人活了多少天的生命计算器。它首先要求用户输入生日和当前日期,然后通过循环来 ...

不要改代码,解析代码!
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         long count = 0; // count用于存放一共活了多少天
  5.         int year1, year2; // year1是你的生日年份,year2是今天的年份
  6.         int month1, month2;
  7.         int day1, day2;
  8.         int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  9.         printf("请输入你的生日(如1988-05-20):");
  10.         scanf("%d-%d-%d", &year1, &month1, &day1);

  11.         printf("请输入今年日期(如2016-03-28):");
  12.         scanf("%d-%d-%d", &year2, &month2, &day2);

  13.         while (year1 <= year2)
  14.         {
  15.                 days[1] = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
  16.                 while (month1 <= 12)
  17.                 {
  18.                         while (day1 <= days[month1 - 1])
  19.                         {
  20.                                 if (year1 == year2 && month1 == month2 && day1 == day2)
  21.                                 {
  22.                                         goto FINISH; // 跳出多层循环才被迫用goto语句
  23.                                 }
  24.                                 day1++;
  25.                                 count++;
  26.                         }
  27.                         day1 = 1;
  28.                         month1++;
  29.                 }
  30.                 month1 = 1;
  31.                 year1++;
  32.         }

  33. FINISH: printf("你在这个世界上总共生存了%d天\n", count);

  34.         return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-5 06:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表