簡體   English   中英

"我的代碼中存在對齊問題和額外間距,但我不知道在哪里?"

[英]There is an Alignment Issue and Extra Spacing in my code, but I cannot figure out where?

我一直在做一個項目。 我僅限於使用幾個庫,因此任何其他庫都無濟於事。 我正在構建一個日歷,到目前為止我的日歷可以工作,但是每次 - 在第二行的數字變為兩位數之后 - 都會有一個轉變,我的日歷並沒有整齊地排列在下一行的頂部。 我不知道間距問題允許這種情況發生在哪里。 另外,當日歷完成時,我注意到最后一個數字后面有一個額外的空格。 如何解決此對齊問題和額外間距問題? 請幫忙! 這是輸出的示例圖片。 更新:感謝德魯幫助我對齊!<\/strong> 但是,我現在面臨最后一個額外間距的問題。<\/strong> 我該如何解決?<\/strong> 在此處輸入圖像描述<\/a>

#include <iostream>

int month, day, year, start = 0;

#define MONTHS_PER_YEAR 12

const unsigned short DAYS_PER_MONTH[MONTHS_PER_YEAR] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const char MONTH_NAMES[MONTHS_PER_YEAR][10] =
{"January",   "February", "March",    "April",
"May",       "June",     "July",     "August",
"September", "October",  "November", "December"};

/**
 * Returns true if indicated year is a leap year.
 * @param year: the year that user inputs.
 * @return true if year is a leap year, and false otherwise.
 */
bool leapYear(int year)
{
    return (((year % 400) == 0) || ((year % 4 == 0) && !(year % 100 == 0)));
}

/**
 * Iterates through months Janauray and February to find the starting day of
 *the month following the Gregorian caledner.
 * @returns extra day for leap year or not.
 */
int day_of()
{
   int subset_days[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
   if (month < 3)
   {
      year--;
   }
   return ((year + year / 4 - year / 100 + year / 400 + subset_days[month - 1] + day) % 7);
}

/**
 * Prints the calender and calculates how many days are in month and which months
 * day starts on.
 */
void printMonth()
{
   start = day_of();
   int count = 0;
   int days_per = DAYS_PER_MONTH[month - 1];
   if (leapYear(year + 1) && month == 2)
   {
     days_per = DAYS_PER_MONTH[month - 1] + 1;
   }
   if (start == 6)
   {
      start = -1;
      std::cout << " ";
   }
   for (count = 0; count <= start; count++)
   {
      if (count > 0)
      {
         std::cout << "   ";
      }
      else
      {
         std::cout << "    ";
      }
   }
   for (day = 1; day <= days_per; day++)
   {
      if (++count > 6)
      {
         count = 0;
         if (day > 9)
         {
            std::cout << day << '\n';
         }
         else
         {
            std::cout << day << '\n' << " ";
         }
      }
      else
      {
         if (day > 9)
         {
            std::cout << day;
         }
         else 
            std::cout << day << " ";
            std::cout << " ";
      }
   }
   std::cout << std::endl;
}

// Controls operation of the program.
int main()
{
   std::cout << "Enter the month: ";
   std::cin >> month;
   std::cout << "Enter the year: ";
   std::cin >> year;
   std::cout << MONTH_NAMES[month - 1] << " " << year << std::endl;
   std::cout << "Su" << "  "<< "M" << "  "<< "T"<< "  " << "W" << " " << "Th" << "  " << "F" << " " << "Sa\n";
   printMonth();
   return 0;
}

(day > 9)更改為(day >= 9 )

您正在使用該條件來決定數字應該出現多少空格。

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM