课程代码仓库初始化:Part1 (144) + Part3 (29) 全量生成与验证
把川大 wiki「02.Qt方向」课程代码整理为 CMake 管理的可编译运行项目。
Part1 C/C++ 强化 (p01/, 144 目标, host gcc/g++ 构建, 全绿):
- 修复全部编译失败:补 <cstring>/<fstream>/<iomanip>/<vector> 等缺失头
(common_fix + infer_stl_includes 自动检测注入)、修正 pause() 壳注入
(改检测已转换后的 pause 调用)、namespace 提升出函数体、const 成员函数
正确性、dedupe 不再破坏函数重载集、K&R 隐式 int / 动态异常规范等
C++17 移除特性的可编译等价改写。
- 目录采用深缩写 p01/ch04/s03/ (原 part1_cpp/ch04_class_object/03_ctor_dtor)。
Part3 Qt 桌面 (p03/, 29 目标, 隔离 Qt 5.14.2 构建, 全绿):
- gen_part3.py: Qt 外壳模板 (main+QApplication+show, QTimer 自动退出便于
offscreen 验证)、AUTOMOC + .moc include、rewrite_* 处理散文混入/重载/
资源依赖、生成占位 png/gif + .qrc + rundata 样例数据。
- 修正 QLable 笔误、全角分号、.→->、Windows 路径、QApplication 阻塞事件
循环改为 QCoreApplication 等。
任务1 概念/版本验证:
- tools/std_probe.py: 跨 -std= 编译探针 (--pedantic-errors 让已废除特性硬失败)。
- tools/demo_versions/: K&R 隐式 int、三目左值、void* 转换、enum 赋整、
const 经指针、动态异常规范 共 6 个跨版本演示。
- docs/VERSION_NOTES.md: 每条「原文说法→C17/C++17 是否成立→何版本成立→已验证」。
文档: 顶层 README、docs/SOURCE_PAGES.md (页面→目录映射)、
docs/ERRATA.md (Part1 修正)、docs/ERRATA_part3.md (Part3 修正)。
另修复 tools/run.sh / run_qt.sh 的 ${1:?...} 引号语法 bug。
隔离 Qt 验证: ldd 0 系统 Qt 引用; 173/173 目标全绿; p03 offscreen 运行通过。
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# Auto-generated by tools/gen_part1.py — do not edit by hand.
|
||||
# 章节:p01/ch08
|
||||
add_executable(p1c08_01 standard_input_stream.cpp)
|
||||
set_target_properties(p1c08_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c08_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c08_01 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c08_02 character_output.cpp)
|
||||
set_target_properties(p1c08_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c08_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c08_02 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c08_03 formatted_output.cpp)
|
||||
set_target_properties(p1c08_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c08_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c08_03 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c08_04 ascii_file_io.cpp)
|
||||
set_target_properties(p1c08_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c08_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c08_04 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c08_05 binary_file_io.cpp)
|
||||
set_target_properties(p1c08_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c08_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c08_05 PRIVATE -Wall -Wextra)
|
||||
add_custom_target(all_p01_ch08 DEPENDS p1c08_01 p1c08_02 p1c08_03 p1c08_04 p1c08_05)
|
||||
@@ -0,0 +1,46 @@
|
||||
// ============================================================================
|
||||
// p1c08_04 — 8.4.4 C++对ASCII文件的读写操作
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include <cstdio>
|
||||
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
|
||||
|
||||
// --- 原文片段 [58954476:3] 8.4.4 C++对ASCII文件的读写操作 ---
|
||||
int main(){
|
||||
|
||||
char* sourceFileName = "./source.txt";
|
||||
char* targetFileName = "./target.txt";
|
||||
//创建文件输入流对象
|
||||
ifstream ism(sourceFileName, ios::in);
|
||||
//创建文件输出流对象
|
||||
ofstream osm(targetFileName,ios::out);
|
||||
|
||||
if (!ism){
|
||||
cout << "文件打开失败!" << endl;
|
||||
}
|
||||
|
||||
while (!ism.eof()){
|
||||
char buf[1024] = { 0 };
|
||||
ism.getline(buf,1024);
|
||||
cout << buf << endl;
|
||||
osm << buf << endl;
|
||||
}
|
||||
|
||||
//关闭文件流对象
|
||||
ism.close();
|
||||
osm.close();
|
||||
|
||||
pause();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,69 @@
|
||||
// ============================================================================
|
||||
// p1c08_05 — 8.4.5 C++对二进制文件的读写操作
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
|
||||
#include <cstdio>
|
||||
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
|
||||
|
||||
// --- 原文片段 [58954476:4] 8.4.5 C++对二进制文件的读写操作 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){} // 补默认构造:二进制读入需先默认构造占位对象
|
||||
Person(char* name,int age){
|
||||
strcpy(this->mName, name);
|
||||
this->mAge = age;
|
||||
}
|
||||
public:
|
||||
char mName[64];
|
||||
int mAge;
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
char* fileName = "person.txt";
|
||||
//二进制模式读写文件
|
||||
//创建文件对象输出流
|
||||
ofstream osm(fileName, ios::out | ios::binary);
|
||||
|
||||
Person p1("John",33);
|
||||
Person p2("Edward", 34);
|
||||
|
||||
//Person对象写入文件
|
||||
osm.write((const char*)&p1,sizeof(Person));
|
||||
osm.write((const char*)&p2, sizeof(Person));
|
||||
|
||||
//关闭文件输出流
|
||||
osm.close();
|
||||
|
||||
//从文件中读取对象数组
|
||||
ifstream ism(fileName, ios::in | ios::binary);
|
||||
if (!ism){
|
||||
cout << "打开失败!" << endl;
|
||||
}
|
||||
|
||||
Person p3;
|
||||
Person p4;
|
||||
|
||||
ism.read((char*)&p3, sizeof(Person));
|
||||
ism.read((char*)&p4, sizeof(Person));
|
||||
|
||||
cout << "Name:" << p3.mName << " Age:" << p3.mAge << endl;
|
||||
cout << "Age:" << p4.mName << " Age:" << p4.mAge << endl;
|
||||
|
||||
//关闭文件输入流
|
||||
ism.close();
|
||||
|
||||
pause();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,53 @@
|
||||
// ============================================================================
|
||||
// p1c08_02 — 8.4.1 字符输出
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954476:1] 8.4.1 字符输出 ---
|
||||
//cout.flush 刷新缓冲区,linux下有效
|
||||
void test01(){
|
||||
cout << "hello world";
|
||||
//刷新缓冲区
|
||||
cout.flush();
|
||||
}
|
||||
|
||||
//cout.put 输出一个字符
|
||||
void test02(){
|
||||
|
||||
cout.put('a');
|
||||
//链式编程
|
||||
cout.put('h').put('e').put('l');
|
||||
}
|
||||
|
||||
//cout.write 输出字符串 buf,输出多少个
|
||||
void test03(){
|
||||
|
||||
//char* str = "hello world!";
|
||||
//cout.write(str, strlen(str));
|
||||
char* str = "*************";
|
||||
for (int i = 1; i <= strlen(str); i ++){
|
||||
cout.write(str, i);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
for (int i = strlen(str); i > 0; i --){
|
||||
cout.write(str, i);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test02();
|
||||
test03();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// ============================================================================
|
||||
// p1c08_03 — 8.4.2 格式化输出
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
// --- 原文片段 [58954476:2] 8.4.2 格式化输出 ---
|
||||
//通过流成员函数
|
||||
void test01(){
|
||||
|
||||
int number = 99;
|
||||
cout.width(20);
|
||||
cout.fill('*');
|
||||
cout.setf(ios::left);
|
||||
cout.unsetf(ios::dec); //卸载十进制
|
||||
cout.setf(ios::hex);
|
||||
cout.setf(ios::showbase);
|
||||
cout.unsetf(ios::hex);
|
||||
cout.setf(ios::oct);
|
||||
cout << number << endl;
|
||||
|
||||
}
|
||||
|
||||
//使用控制符
|
||||
void test02(){
|
||||
|
||||
int number = 99;
|
||||
cout << setw(20)
|
||||
<< setfill('~')
|
||||
<< setiosflags(ios::showbase)
|
||||
<< setiosflags(ios::left)
|
||||
<< hex
|
||||
<< number
|
||||
<< endl;
|
||||
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test02();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// ============================================================================
|
||||
// p1c08_01 — 8.3 标准输入流
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <string>
|
||||
|
||||
// --- 原文片段 [58954476:0] 8.3 标准输入流 ---
|
||||
//cin.get
|
||||
void test01(){
|
||||
#if 0
|
||||
char ch = cin.get();
|
||||
cout << ch << endl;
|
||||
|
||||
cin.get(ch);
|
||||
cout << ch << endl;
|
||||
|
||||
|
||||
//链式编程
|
||||
char char1, char2, char3, char4;
|
||||
cin.get(char1).get(char2).get(char3).get(char4);
|
||||
|
||||
cout << char1 << " " << char2 << "" << char3 << " " << char4 << " ";
|
||||
#endif
|
||||
|
||||
char buf[1024] = { 0 };
|
||||
//cin.get(buf.1024);
|
||||
cin.getline(buf,1024);
|
||||
cout << buf;
|
||||
}
|
||||
|
||||
//cin.ignore
|
||||
void test02(){
|
||||
|
||||
char buf[1024] = { 0 };
|
||||
cin.ignore(2); //忽略缓冲区当前字符
|
||||
cin.get(buf,1024);
|
||||
cout << buf << endl;
|
||||
}
|
||||
|
||||
//cin.putback 将数据放回缓冲区
|
||||
void test03(){
|
||||
|
||||
//从缓冲区取走一个字符
|
||||
char ch = cin.get();
|
||||
cout << "从缓冲区取走的字符:" << ch << endl;
|
||||
//将数据再放回缓冲区
|
||||
cin.putback(ch);
|
||||
char buf[1024] = { 0 };
|
||||
cin.get(buf,1024);
|
||||
cout << buf << endl;
|
||||
|
||||
}
|
||||
|
||||
//cin.peek 偷窥
|
||||
void test04(){
|
||||
|
||||
//偷窥下缓冲区的数据
|
||||
char ch = cin.peek();
|
||||
cout << "偷窥缓冲区数据:" << ch << endl;
|
||||
char buf[1024] = { 0 };
|
||||
cin.get(buf, 1024);
|
||||
cout << buf << endl;
|
||||
}
|
||||
|
||||
//练习 作业 使用cin.get和putback完成类似功能
|
||||
void test05(){
|
||||
|
||||
cout << "请输入一个数字或者字符串:" << endl;
|
||||
char ch = cin.peek();
|
||||
if(ch >= '0' && ch <= '9'){
|
||||
int number;
|
||||
cin >> number;
|
||||
cout << "数字:" << number << endl;
|
||||
}
|
||||
else{
|
||||
char buf[64] = { 0 };
|
||||
cin.getline(buf, 64);
|
||||
cout << "字符串:" << buf << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test02();
|
||||
test03();
|
||||
test04();
|
||||
test05();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user