简体   繁体   English

C ++计算一周中的未来一天

[英]C++ Calculate a future day of the week

I am struggling with this problem. 我正在努力解决这个问题。

-->Create a program that calculates a future day of the week. ->创建一个程序来计算一周中的未来一天。 Your program should input two items: the current day represented as a string (such as “Monday” or “Friday”), and an integer of how many days to count into the future. 您的程序应输入两个项目:以字符串形式表示的当前日期(例如“ Monday”或“ Friday”),以及要计入未来天数的整数。 For example, Monday 7 would give the output Monday (since there are seven days in a week!) Wednesday 9 would give the output Friday, and Saturday 29 would give the output Sunday. 例如,星期一7将给出星期一的输出(因为一周中有7天!)星期三9将给出星期五的输出,而星期六29将给出星期日的输出。 Do not assume anything sneaky like leap years!<-- 不要像leap年那样偷偷摸摸!<-

I have approached it so far by finding the remainder of the number of days entered which should be from 0 to 7 and then adding that number to the current week day. 到目前为止,我已经找到了输入的剩余天数(应为0到7),然后将该数字添加到当前工作日中,从而达到了这一目标。 So Monday plus 7 is Monday. 所以星期一加7是星期一。

What I am struggling with is coding it all. 我正在努力编写所有代码。 I was thinking of using if statements but not sure. 我当时在考虑使用if语句,但不确定。

If the current day = monday and the remainder is 1 then the future day is tuesday and so on. 如果当前日期=星期一,余数为1,则将来的日期为星期二,依此类推。

It all makes sense in my head but I'm having a hard time converting it into c++. 这一切在我脑海中都是有道理的,但是我很难将其转换为c ++。

Anyone have any suggestions? 有人有什么建议吗? Also using an array has been suggested to me but we have not learned about those in class yet so I don't think I am supposed to use one for this. 还建议使用数组,但是我们还没有在课堂上了解到这些数组,所以我认为我不应该为此使用一个数组。

What I have so far is below. 到目前为止,我所拥有的如下。

// Riley
// Lab 3 Project 1
// Sources:
// This program will calculate a future day of the week.

#include <iostream>
#include <string>
using namespace std;

int main(){
// declare vars
string mon, tues, wed, thurs, fri, sat, sun;
string weekDay;
string futureDay;
int days = 0;
int remainder = 0;

// gather input
cout << "Please enter the current day and the number of days to count into the future: " << endl;
cin >> weekDay >> days;

// conpute
remainder = days % 8;

// print output
    cout << "The day of the week in " << days << " days is " << futureDay;

// pause and exit
getchar();
getchar();
return 0;

} }

You want number_to_day_of_week((days_in_future + day_of_week_to_number(day_of_week)) % 7) where the day of week to/from number functions use 0 to 6. 您想要number_to_day_of_week((days_in_future + day_of_week_to_number(day_of_week)) % 7) ,其中星期几/数字功能使用0到6。

So you could use: 因此,您可以使用:

std::string number_to_day_of_week(int day_of_week)
{
    static const char *const names[7] =
    {
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
    };
    day_of_week %= 7;
    if(day_of_week < 0) // handle negative inputs
        day_of_week += 7;
    return names[day_of_week];
}

int day_of_week_to_number(std::string name)
{
    static const char *const names[7] =
    {
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
    };
    for(int i = 0; i < sizeof(names) / sizeof(names[0]); i++)
    {
        if(name == names[i])
            return i;
    }
    throw std::runtime_error("invalid day of week");
}

You can use something like this 你可以用这样的东西

#include <iostream>
#include <string>

using namespace std; 
int main(void)
{         

string input = "Monday";                                              int daystoadd = 10;
int FUTUREDAY;
string Days[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"}; 

for( int i = 0; i < Days.length ; i++){
 If(input.compare(Days[i]) == 0){
         FUTUREDAY = (i  + 1 + daystoadd) % 7;
     }
}
/// display out date 
Days[FUTUREDAY]
}

Access array to print day 访问阵列以打印日期

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

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