简体   繁体   English

日计数和星期几(C++)

[英]day counting and day of week(c++)

I'm working on a program in c++ that will get ask the user to enter a date such as (12 31) and the program will output the number of days and the day of the week so (12 31) will return (365 Tue).我正在用 C++ 编写一个程序,它会要求用户输入一个日期,例如 (12 31),该程序将输出天数和星期几,因此 (12 31) 将返回 (365 Tue )。 So far I have到目前为止我有

 #include <iostream>
 using namespace std;
 int main (){ 
 while (true)  
 cout << "Enter date: "; cin >> mon >>day;
 if (!mon && !day) break; //this is so that 
 when the user enters (0 0) the program ends
 }
 cout << "Bye" << endl;
 return 0;
 }

How should I get the program to match the date to a number and day of the week?我应该如何让程序将日期与星期几和数字相匹配? I'm just starting to learn c++ through online tutorials so I'm not that fluent but I do know some stuff.我刚开始通过在线教程学习 C++,所以我不是很流利,但我确实知道一些东西。 Do I need to create a new function?我需要创建一个新函数吗? My main issue is that I've hit a roadblock on how I should get the program to count the days from the given date, (I was thinking a range from 1-365).我的主要问题是我在如何让程序从给定日期开始计算天数方面遇到了障碍(我在考虑 1-365 的范围)。 Not looking for an answer but some help would be nice.不是在寻找答案,但有一些帮助会很好。

Not looking for an answer but some help would be nice.不是在寻找答案,但有一些帮助会很好。

when you do cin >> mon >>day first declare the int variables mon and day but also check the return, so if (!(cin >> mon day)) ...EOF occurred...当你做cin >> mon >>day首先声明int变量monday还要检查返回值,所以if (!(cin >> mon day)) ...EOF occurred...

If you look at the function managing time you have the ones declared through <time.h> including mktime and as you can see they work with struct tm containing exactly what you want :如果您查看管理时间的函数,您会发现通过<time.h>声明的函数,包括mktime,并且如您所见,它们与包含您想要的内容的struct tm一起使用:

 int tm_wday; /* Day of the week (0-6, Sunday = 0) */ int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */

mktime is also exactly what you need : mktime也正是您所需要的:

The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. mktime() 函数将分解的时间结构(表示为本地时间)转换为日历时间表示。

So you just have to set the fields :所以你只需要设置字段:

 int tm_sec; /* Seconds (0-60) */ int tm_min; /* Minutes (0-59) */ int tm_hour; /* Hours (0-23) */ int tm_mday; /* Day of the month (1-31) */ int tm_mon; /* Month (0-11) */ int tm_year; /* Year - 1900 */

tm_sec/tm_min/tm_hour can be 0, tm_mday and tm_month are the inputs you have to get (just decrement tm_month after) tm_sec/tm_min/tm_hour可以是 0, tm_mdaytm_month是你必须得到的输入(只是在tm_month之后递减)

The only missing part is the current year, but it is easy to set it, use time_t time(time_t *tloc);唯一缺少的部分是当前年份,但是很容易设置,使用time_t time(time_t *tloc); returning the current time then convert it to a struct tm using struct tm *localtime(const time_t *timep);返回当前时间,然后使用struct tm *localtime(const time_t *timep);将其转换为struct tm struct tm *localtime(const time_t *timep); , then set the others fields as describe above, then call mktime ,然后按照上面的描述设置其他字段,然后调用mktime

Now you can do your program现在你可以做你的程序了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM