订餐程序涉及知识

记录了一下项目制作过程中 所涉及的不熟悉的部分源码可至GitHub查看

相关知识

1. ctime 头文件

1.1 time_t time(time_t *timer): 返回自纪元以来经过的时间,以秒为单位

1
2
time_t currentTime;
time(& currentTime);

1.2 struct tm *localtime(const time_t *timer): 将 time_t 格式的时间转换为本地时间,返回一个指向 tm 结构体的指针

1
2
3
time_t currTime;
time(&currTime);
struct tm* timeTmp = locatime(&currTime);

1.3 char *asctime(const struct tm *timeptr): 将 tm 结构体表示的时间转换为字符串形式的时间,并返回指向静态字符串的指针

1
2
3
4
time_t currTime;
time(&currTime);
struct tm* timeTmp = locatime(&currTime);
char *timeString = asctime(&timeTmp);

1.4 tm结构体(包含在ctime头文件中)

1
2
3
4
5
6
7
8
9
10
11
12
struct tm {
int tm_sec; // 秒(0-59)
int tm_min; // 分钟(0-59)
int tm_hour; // 小时(0-23)
int tm_mday; // 一个月中的日期(1-31)
int tm_mon; // 月份(0-11)
int tm_year; // 年份自1900年起
int tm_wday; // 一周中的天数(0-6,星期日为0)
int tm_yday; // 一年中的天数(0-365,1月1日为0)
int tm_isdst; // 夏令时标志(>0 表示是夏令时,0 表示不是夏令时,-1 表示夏令时信息不可用)
};

1.5 使用

先使用time()函数获取自1970年1月1日午夜(格林威治时间)起算的秒数,然后使用localtime()函数将其转化为本地时间 (使用tm结构体存储),最后根据需要进行打印

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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now;
time(&now);
struct tm* lt = localtime(&now);
cout << "Local time is: " << lt->tm_year + 1900 << "-" << lt->tm_mon + 1 << "-" << lt->tm_mday << " " << lt->tm_hour << ":" << lt->tm_min << ":" << lt->tm_sec << endl;

char* timeStr = asctime(lt);
cout << "Formatted time is: " << timeStr << endl;

char buffer[255];
strftime(buffer, 255, "%Y-%m-%d %H:%M:%S", lt);
//sprintf(buffer,"%04d-%02d-%02d %02d:%02d:%02d", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,lt->tm_min,lt->tm_sec);
cout << "Formatted time is: " << buffer << endl;
return 0;
}

----------输出结果-----------------
Local time is: 2024-4-28 17:15:42
Formatted time is: Sun Apr 28 17:15:42 2024
Formatted time is: 2024-04-28 17:15:42

2. CMake简单使用

2.1 创建一个CMakeLists.txt文件

2.2 创建一个名为build的文件夹

避免编译产物与代码文件混在一起

2.3 在CMakeLists.txt中写入内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 设置 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.20)

# 定义项目名称
project(dinnerSystem)

# 添加可执行文件,并指定源文件
# 亦可写成 add_executable(${PROJECT_NAME})
add_executable(dinnerSystem)

# 使用file函数 globbing 匹配来获取所有的 .cpp 和 .h 文件,并将文件保存在sources变量中
file(GLOB sources CONFIGURE_DEPENDS *.cpp *.h)

# 将获取的源文件与目标文件相关联
#target_sources(${PROJECT_NAME} PBULIC ${sources})
target_sources(dinnerSystem PUBLIC ${sources})

2.4 在build文件夹中打开cmd

输入cmake ..

cmake --build .

3. 解决中文乱码

使用UTF-8 无BOM 可以在VS2022下载插件Force UTF-8(No BOM)

4.Qt的基础使用

见另一篇笔记