如何解决创建日历,输出格式问题
尝试为学校的实验室创建日历,需要一些帮助。我的程序工作正常,就像显示月份(如果不是days年)中的日期一样。但是,在显示带有相应日期的日期时效果不佳。例如,我的输出是这样的:
Jan 1996
---------------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
如您所见,数字arent与日期对齐,我似乎无法正确地对其进行排列。我想了解这一点,所以如果有人可以帮助,请让我知道我做错了什么,而不是直截了当地告诉答案。
完整代码:
class Calendar {
public:
int Year;
int Month;
int Day;
string MonthNames[12] = { "Jan","Feb","March","April","May","June","July","August","Sept","Oct","Nov","Dec" };
string Name;
string DayNames[7] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
int AmountOfDays = 31; //base value
void AskForDate() {
cout << "Enter Year" << endl;
cin >> Year;
SetUpCalendarUI();
}
void SetUpCalendarUI()
{
if (LeapYear()) {
SetDaysUI();
}
else {
cout << "Not Leap year";
}
}
bool LeapYear() {
if (Year % 4 == 0 || Year % 400 == 0 && Year % 100 != 0) {
return true;
}
else {
return false;
}
}
int ChangeAmountOfDays(int Month) {
switch (Month) {
case 1: return 31;
case 2:
if (!LeapYear()) {
return 28;
}
else {
return 29;
}
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
}
return 0;
}
void SetDaysUI() {
int i;
//print out month names 1 by 1
for (int k = 0; k < 12; k++) {
AmountOfDays = ChangeAmountOfDays(k+1);
cout << "\t" << MonthNames[k] << " " << Year << "\n";
cout << "---------------------------------" << "\n";
//print out days 1 by 1
for (int DayOfWeek = 0; DayOfWeek < 7; DayOfWeek++) {
cout << DayNames[DayOfWeek] << setw(5);
}
cout << "\n";
//we want to set the amount of days == to the month.
//i = current Day put a space in between.
//j = Total Amount of days
for (i = 0; i < Day; i++)
cout << " ";
for (int j = 1; j <= AmountOfDays; j++) {
cout << j << setw(3);
if (i++ > 5) {
i = 0;
cout << "\n";
}
}
if (i) {
cout << "\n";
Day = i;
}
}
return;
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。