我试图写一个C++程序,将显示一个12小时和一个24小时的时钟与改变分钟,小时和秒的能力。这两个时钟需要被显示的不一致,用“*"框,像这样:enter image description here
我相当有信心,时钟本身将运作,因为他们应该,但我不能让他们格式正确。
以下是我目前为止的代码:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
// Function to create a string of a specified length with a specific character
string nCharString(size_t n, char c) {
return string(n, c);
}
// Function to format a number as 2 digits, with a leading 0 if needed
string twoDigitString(unsigned int n) {
if (n < 10) {
return "0" + to_string(n);
} else {
return to_string(n);
}
}
class TimeSim {
public:
int hour;
int minute;
int seconds;
};
class hour_12_format : public TimeSim {
public:
string meridian;
hour_12_format() {
time_t now = time(0);
tm* ltm = localtime(&now);
hour = ltm->tm_hour;
minute = ltm->tm_min;
seconds = ltm->tm_sec;
if (hour >= 12) {
meridian = "P M";
hour -= 12;
} else
meridian = "A M";
}
void add_hour(int h) {
hour += h;
if (hour > 12) {
hour -= 12;
if (meridian == "A M") {
meridian = "P M";
} else if (meridian == "P M") {
meridian = "A M";
}
}
}
void add_minute(int min) {
minute += min;
if (minute >= 60) {
int h = minute / 60;
minute -= 60 * h;
add_hour(h);
}
}
void add_seconds(int sec) {
seconds += sec;
if (seconds >= 60) {
int m = seconds / 60;
seconds -= 60 * m;
add_minute(m);
}
}
};
class hour_24_format : public TimeSim {
public:
hour_24_format() {
time_t now = time(0);
tm* ltm = localtime(&now);
hour = ltm->tm_hour;
minute = ltm->tm_min;
seconds = ltm->tm_sec;
}
void add_hour(int h) {
hour += h;
if (hour >= 24) {
hour -= 24;
}
}
void add_minute(int min) {
minute += min;
if (minute >= 60) {
int h = minute / 60;
minute -= 60 * h;
add_hour(h);
}
}
void add_seconds(int sec) {
seconds += sec;
if (seconds >= 60) {
int m = seconds / 60;
seconds -= 60 * m;
add_minute(m);
}
}
};
// Function to display the 12-hour clock within a rectangle
void display12HourClock(hour_12_format c1) {
// Display rectangle
int rectangleWidth = 26;
string timeStr = twoDigitString(c1.hour) + ":" + twoDigitString(c1.minute) + ":" + twoDigitString(c1.seconds) + " " + c1.meridian;
int padding = (rectangleWidth - timeStr.length()) / 2;
cout << nCharString(rectangleWidth, '*') << endl;
cout << "* 12-Hour Clock: " << nCharString(padding, ' ') << timeStr << nCharString(padding, ' ') << " *\n";
cout << nCharString(rectangleWidth, '*') << endl;
}
// Function to display the 24-hour clock within a rectangle
void display24HourClock(hour_24_format c2) {
// Display rectangle
int rectangleWidth = 26;
string timeStr = twoDigitString(c2.hour) + ":" + twoDigitString(c2.minute) + ":" + twoDigitString(c2.seconds);
int padding = (rectangleWidth - timeStr.length()) / 2;
cout << nCharString(rectangleWidth, '*') << endl;
cout << "* 24-Hour Clock: " << nCharString(padding, ' ') << timeStr << nCharString(padding, ' ') << " *\n";
cout << nCharString(rectangleWidth, '*') << endl;
}
int main() {
hour_12_format c1;
hour_24_format c2;
int choice = 0;
while (choice != 4) {
display12HourClock(c1);
display24HourClock(c2);
cout << "\n\n*************************\n";
cout << "* 1 - Add One Hour\t*\n";
cout << "* 2 - Add One Minute\t*\n";
cout << "* 3 - Add One Second\t*\n";
cout << "* 4 - Exit Program\t*\n";
cout << "*************************\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
c1.add_hour(1);
c2.add_hour(1);
break;
case 2:
c1.add_minute(1);
c2.add_minute(1);
break;
case 3:
c1.add_seconds(1);
c2.add_seconds(1);
break;
case 4:
exit(0);
break;
default:
cout << "Enter Correct Choice!!\n";
break;
}
}
return 0;
}
字符串
这就是我的输出结果:
**************************
* 12-Hour Clock: 08:23:55 P M *
**************************
**************************
* 24-Hour Clock: 20:23:55 *
**************************
*************************
* 1 - Add One Hour *
* 2 - Add One Minute *
* 3 - Add One Second *
* 4 - Exit Program *
*************************
型
我试着调整每一条线,但没有运气,也不知道我错在哪里。
1条答案
按热度按时间xj3cbfub1#
你想让时钟打印在一行上吗?还是像你链接的图片一样打印两行?
对于一行,没有大的空白:目前,您计算
padding
时没有考虑“标签”(12-Hour Clock
或24-Hour Clock
)。字符串是14个字符+ 3个左侧空格+ 1个边框,剩下
padding = (rectangleWidth - timeStr.length() - 18) / 2
。举例来说,您可以:
字符串
导致
型
请注意,我还删除了以
*
结尾的行的尾随空格。同样的方法应该解决菜单显示,这意味着确保,你的空白计数检查出来。