如何实现循环以增加数据

时间:2019-02-14 02:23:48

标签: c++ loops for-loop

我正在尝试通过使用c ++中的循环使我的程序更智能。尝试解决此问题时遇到了障碍,因为我试图跟踪很多计数器。比正确的代码更重要的是,如何正确思考问题以得出正确的结论?

我已经尝试过for循环,并且认为嵌套的for循环可能是必需的,但是我无法弄清楚如何连续添加monthDays []值来正确检查输入并做出正确的决定。

输入是用户定义的数字,小于366但大于0。 该程序旨在返回月份名称和该月份过去的日期。

string monthNames[12] = {"January", "February", "March", "April"};
int monthDays[12] = {31, 28, 31, 30};   


if ((input - monthDays[0]) <=  0){

    cout << monthNames[0] << " " << input;
    }

      else if (input < (monthDays[0] + monthDays[1]) &&  
              (input > monthDays[0]){

        cout << monthNames[1] << " " << (input - monthDays[0]);
     }

        else if (input < (monthDays[0] + monthDays[1] + monthDays[2]) &&
                input > (monthDays[0] + monthDays[1])){

          cout << monthNames[2] << " " << (input - monthDays[0] - monthDays[2]);
        }

          else if (input < (monthDays[0] + monthDays[1] + monthDays[2] + monthDays[3]) &&      
                  input > (monthDays[0] + monthDays[1] + monthDays[2])){

       cout << monthNames[2] << " " << (input - monthDays[0] - monthDays[2] - monthDays[3]);
           }

该程序有效,但可以简化。我应该如何改变思维过程以实现循环?

3 个答案:

答案 0 :(得分:1)

我假设您要打印“月日”,因此可以将逻辑简化为:

string monthNames[12] = {"January", "February", "March", "April", /*and so on*/}; 
int monthDays[12] = {31, 28, 31, 30, /*same here*/}; 
int i = 0;
/*While your input is bigger than the days of the month*/    
while(input > monthDays[i] && i < 12)
{
    input -= monthDays[i]; // subtract it
    i++;                   // and go to next month
}
//Then print the month and the day
cout << monthNames[i] << " " << input;

答案 1 :(得分:0)

以下是循环示例:

    std::string monthNames[12] = {"January", "February", "March", "April"};
    int monthDays[12] = {31, 28, 31, 30};

    int dayInYear = 50; // this is your 'input' variable
    int daysPassed = 0;    
    for( int i=0; i<12; i++ ) {
        int daysOnStartOfMonth = daysPassed;
        daysPassed += monthDays[i];
        if( dayInYear <= daysPassed ) {
            std::cout << monthNames[i] << " " << (dayInYear - daysOnStartOfMonth);
            break;  // We found the month, so we can stop the loop
        }
    }

您可以在http://cpp.sh/8mahf

尝试一下

我使用变量'dayInYear'作为计数器,在该计数器中添加了特定月份的天数。然后就是将这个变量与您的输入变量进行比较(我将其称为“ dayInYear”,因为它是变量的更具描述性的名称)。

答案 2 :(得分:0)

您可以通过让代码为您完成所有工作来整理很多。一个简单的while循环就可以做到这一点:

#include <string>
#include <iostream>
#include <stdlib.h>

const std::string monthNames[12] = {"January", "February", "March", "April"};
const int monthDays[12] = {31, 28, 31, 30};

const std::string& monthName(int index) {
  // Start testing at month index 0
  int month = 0;

  while (index > monthDays[month] && month < 12) {
    // Subtract the number of days in this month if moving on...
    index -= monthDays[month];

    // ...to the next month.
    ++month;
  }

  // Return the month found.
  return monthNames[month];
}

int main(int argc, char** argv) {
  std::cout << monthName(atoi(argv[1])) << "\n";

  return 0;
}

请注意,这需要一个完整的月份表才能工作,并且需要两个表来容纳leap年。