如何使用C++更改系统时钟的值



我一直在想如何将系统时钟用于类项目。目标是显示系统的当前时间,然后通过1-4的输入添加一小时、分钟或秒,然后再次显示时钟。我想我已经制定了基本框架,但我不知道如何正确显示系统时间以及更改时间本身。我研究了一些要使用的库,它对指针和打印日期的方式感到非常困惑。我还是C++的新手,所以我的代码不是最好的,尤其是在格式化显示的函数时。感谢您的帮助。

当我运行程序时,我想从运行它的PC上打印本地时间,然后接收用户的输入(按键1-4(,然后再次打印新的时间。我不想实际更改电脑上的时间。例如,假设我电脑的当前本地时间是08:22:14,它将打印到屏幕上。在输入选择2之前,我等待任意时间。新时间将打印08:23:14。

#define _CRT_SECURE_NO_WARNINGS
#include <time.h>       /* time_t, struct tm, time, localtime */
#include <iostream> // std::cout, std::endl
#include <iomanip>  // std::setfill, std::setw
#include <stdlib.h> // system(CLS);
#include <Windows.h>
#include <ctime>
using namespace std;
int DisplayClocks(int time) {   // Function to write both clocks to screen  
std::cout << std::setfill('*') << std::setw(26) << "     " << std::setfill('*') << std::setw(26) << " " << endl;    // First line of "*"
std::cout << "*" << std::setfill(' ') << std::setw(16) << "12 Hour Clock" << std::setfill(' ') << std::setw(4) << "  *" << "     "
<< "*" << std::setfill(' ') << std::setw(18) << "24 Hour Clock" << std::setfill(' ') << std::setw(6) << "  *" << endl;
// 12 hour clock
std::cout << "*" << std::setfill(' ') << std::setw(6) << " " << time << std::setfill(' ') << std::setw(6) << "  *" << "     "
// 24 hour clock
<< "*" << std::setfill(' ') << std::setw(8) << " " << std::setfill(' ') << std::setw(8) << "  *" << endl;
std::cout << std::setfill('*') << std::setw(26) << "     " << std::setfill('*') << std::setw(26) << " " << endl;    // Last line of "*"

return 0;
}
void DisplaySelection() {   // Function to display selection menu for user

std::cout << std::setfill('*') << std::setw(26) << " " << endl;
std::cout << "*" << std::setfill(' ') << std::setw(19) << "1 - Add One Hour" << std::setfill(' ') << std::setw(5) << "  *" << endl;
std::cout << "*" << std::setfill(' ') << std::setw(20) << "2 - Add One Minute" << std::setfill(' ') << std::setw(4) << "  *" << endl;
std::cout << "*" << std::setfill(' ') << std::setw(20) << "3 - Add One Second" << std::setfill(' ') << std::setw(4) << "  *" << endl;
std::cout << "*" << std::setfill(' ') << std::setw(19) << "4 - Exit Program" << std::setfill(' ') << std::setw(5) << "  *" << endl;
std::cout << std::setfill('*') << std::setw(26) << " " << endl;
}
void main()
{

time_t now = time(0);
system("CLS");

string userVal;

DisplayClocks(now); // Call displayClocks on program start
DisplaySelection(); // Call DisplaySelection after display clocks
cin >> userVal; // Take user input to modify clock display
while (!( userVal == "Exit")) {
// FIX ME: Add functionality to clear screen every second
// FIX ME: Add displayClock to relevant if statements
if (userVal == "1") {
// Add One Hour to Clocks
// FIX ME: Functionality for Displaying 12 and 24 hour clocks
system("CLS"); // Clear screen test... working...
DisplayClocks(now); // Call displayClocks on program start
DisplaySelection(); // Call DisplaySelection after display clocks
cout << "1" << endl;
cin >> userVal;
}
else if (userVal == "2") {
// Add One Minute to Clocks
// FIX ME: Functionality for Displaying 12 and 24 hour clocks
system("CLS"); // Clear screen test... working...
DisplayClocks(now); // Call displayClocks on program start
DisplaySelection(); // Call DisplaySelection after display clocks
cout << "2" << endl;
cin >> userVal;
}
else if (userVal == "3") {
// Add One Second to Clocks
// FIX ME: Functionality for Displaying 12 and 24 hour clocks
system("CLS"); // Clear screen test... working...
DisplayClocks(now); // Call displayClocks on program start
DisplaySelection(); // Call DisplaySelection after display clocks
cout << "3" << endl;
cin >> userVal;
}
else if (userVal == "4") {
// Exit Program
// FIX ME: Functionality for Displaying 12 and 24 hour clocks
cout << "Program Ended" << endl;
break;
}
else{
// Prompt user to input correct selection when not userVal ! (1-4)
system("CLS"); // Clear screen test... working...
DisplayClocks(now); 
DisplaySelection();
cout << "Error: Enter a selection 1 - 4." << endl;
cin >> userVal;
}   
}
}

当你问"如何更改系统<任何>在C++中&";,关键字是";系统";。

这意味着它依赖于平台,因此它需要一个SYSTEM调用-我说的不是system函数,而是对操作系统的API(在您的情况下是Win32(的调用。这些功能中的许多都需要提升才能在Windows和Linux上工作。。。不幸的是,在两个操作系统上,更改系统日期和时间都是一个函数,而且不允许用标准帐户调用它。

此外,事实上,它与C++没有任何关系。在Windows上,您需要调用SetSystemTime,它不是C++功能,而是通过sysinfoapi.h头导入的kernel32.dll函数(在Windows SDK中找到(。在几乎任何编程语言中,你都会得到完全相同的答案:"对CCD_ 5"进行系统调用

最新更新