在前一节,使用大模型工具文心一言等可以帮助分析代码,加快理解。
信息时代→智能时代,系统学习转为碎片学习。
发散思维的能力在智能时代尤为重要。
同样我们也可以借助智能化工具修改代码,提出自己的需求。
所有的云课五分钟,不仅演示可行案例,也同样重视在尝试过程中的失败,并且也会全部展示出来。
一次又一次在失败中站起来,这也许就是人区别于机器人所具有的独特成长历程,弥足珍贵。
视频与文字并非对应关系,类似互补关系,同时又相互独立。
云课五分钟-05一段代码修改-AI修改C++
usleep(30000); 实现什么功能
usleep(30000);
实现的功能是使程序暂停30000微秒(也就是0.03秒或30毫秒)。
在这段代码中,usleep
函数用于在每次循环迭代之间添加一个小的延迟。这样做有几个目的:
usleep
可以用于控制系统的更新速度或者动画的帧率。简单来说,usleep(30000);
就是用来在每次循环迭代中创建一个小的延迟,以提供更好的用户体验并降低CPU的使用率。
- #include
- #include
- #include
- #include
-
- int main() {
- float A = 0, B = 0;
- float i, j;
- int k;
- float z[1760];
- char b[1760];
- printf("\x1b[2J");
- for (;;) {
- memset(b, 32, 1760);
- memset(z, 0, 7040);
- for (j = 0; j < 6.28; j += 0.07) {
- for (i = 0; i < 6.28; i += 0.02) {
- float c = sin(i);
- float d = cos(j);
- float e = sin(A);
- float f = sin(j);
- float g = cos(A);
- float h = d + 2;
- float D = 1 / (c * h * e + f * g + 5);
- float l = cos(i);
- float m = cos(B);
- float n = sin(B);
- float t = c * h * g - f * e;
- int x = 40 + 30 * D * (l * h * m - t * n);
- int y = 12 + 15 * D * (l * h * n + t * m);
- int o = x + 80 * y;
- int N = 8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n);
- if (22 > y && y > 0 && x > 0 && 80 > x && D > z[o]) {
- z[o] = D;
- b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
- }
- }
- }
- printf("\x1b[H");
- for (k = 0; k < 1761; k++) {
- putchar(k % 80 ? b[k] : 10);
- A += 0.00004;
- B += 0.00002;
- }
- usleep(30000);
- }
- return 0;
- }
修改如上代码,使得动态效果变慢一倍。
如果你想让动态效果变慢一倍,你可以简单地将 usleep(30000);
这行代码修改为 usleep(60000);
。这将使得程序在每次循环迭代后暂停60000微秒(也就是0.06秒或60毫秒),是原来的两倍时间。
所以修改后的代码如下:
- #include
- #include
- #include
- #include
-
- int main() {
- float A = 0, B = 0;
- float i, j;
- int k;
- float z[1760];
- char b[1760];
- printf("\x1b[2J");
- for (;;) {
- memset(b, 32, 1760);
- memset(z, 0, 7040);
- for (j = 0; j < 6.28; j += 0.07) {
- for (i = 0; i < 6.28; i += 0.02) {
- // ... (unchanged code)
- }
- }
- printf("\x1b[H");
- for (k = 0; k < 1761; k++) {
- putchar(k % 80 ? b[k] : 10);
- A += 0.00004;
- B += 0.00002;
- }
- usleep(60000); // Changed from usleep(30000)
- }
- return 0;
- }
这样做将使得动态图形的更新速度变慢,使得其变化更加易于观察。
对于简单的修改要求,AI基本都能完成。
但是,要求多了,需要细节描述好,同时,也有概率出现无法解决的情况。
能否修改如上代码,显示彩色的动态效果。
在终端中显示彩色文本通常需要用到ANSI转义码。你可以通过修改b[o]
的赋值部分来实现彩色效果。
首先,你需要定义一个颜色数组,这个数组包含你想要用的ANSI颜色码。然后,你可以像之前一样使用".,-~:;=!*#$@"
数组来选择颜色。
以下是一个简单的修改版本,这里我仅展示了如何将b[o]
修改为彩色,我使用了10种不同的颜色来循环:
- #include
- #include
- #include
- #include
-
- // ANSI color codes for the colors
- const char* colors[] = {
- "\x1b[31m", // Red
- "\x1b[32m", // Green
- "\x1b[33m", // Yellow
- "\x1b[34m", // Blue
- "\x1b[35m", // Magenta
- "\x1b[36m", // Cyan
- "\x1b[31;1m", // Bold Red
- "\x1b[32;1m", // Bold Green
- "\x1b[33;1m", // Bold Yellow
- "\x1b[37m" // White
- };
-
- int main() {
- float A = 0, B = 0;
- float i, j;
- int k;
- float z[1760];
- char b[1760];
- printf("\x1b[2J");
- for (;;) {
- memset(b, 32, 1760);
- memset(z, 0, 7040);
- for (j = 0; j < 6.28; j += 0.07) {
- for (i = 0; i < 6.28; i += 0.02) {
- // ... (unchanged code)
- int color_index = N > 0 ? N % 10 : 0; // Cycle colors if there are more points than colors
- b[o] = colors[color_index]; // Set the color for this point
- // You might want to append the actual character here like: b[o] = colors[color_index] + ".";
- // It depends on what visual effect you're aiming for.
- }
- }
- printf("\x1b[H");
- for (k = 0; k < 1761; k++) {
- putchar(k % 80 ? b[k] : 10);
- A += 0.00004;
- B += 0.00002;
- }
- usleep(60000);
- }
- return 0;
- }
注意: 上述代码片段假定N
的值是变化的,并且可以提供足够的颜色变化。如果N
的变化不足以提供所有颜色的变化,你可能需要找到另一种方式来映射点到颜色。同时,要记住在最后的putchar
之后重置颜色,可以使用\x1b[0m
来重置到默认颜色。如果字符和颜色是分开的(即使用了注释中的append方法),你可能需要在字符后面添加这个重置码。
在编译如上代码时,出现报错,错误如下:code03.cpp: In function ‘int main()’: code03.cpp:52:42: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] b[o] = colors[color_index]; // Set the color for this point ^ code03.cpp:66:1: error: expected ‘}’ at end of input }
这里有两个问题:
b[o]
被定义为char
,但你试图将const char*
(字符串)赋给它,这就是第一个错误的原因如果你想存储ANSI颜色代码,你应该将b
数组的类型从char
改为const char*
,这样可以存储字符串而不是单个字符但是,这样做的话你需要确保其他部分的代码与这个类型兼容
代码修改如下:
cpp
const char* b[1760]; | |
// ... | |
b[o] = colors[color_index]; |
确保所有的代码块都有正确的结束大括号}
你需要在报错的位置检查前面的代码块,确保每个开始的{
都有对应的结束}
根据提供的代码片段,这两个修改应该能够解决你的问题但是,如果这些修改后仍然存在问题,请提供更多的代码上下文,以便我能更全面地理解和解决问题
这段代码看起来像是一个基于终端的动画,使用 ASCII 字符来显示一种动态模式。它已经在做一些相当复杂的数学计算来生成这个动画。这段代码使用了正弦和余弦函数来生成形状,并使用了循环来持续更新画面。
你可以尝试以下几种方式来让这段代码变得更有趣:
在修改和增加功能的过程中,需要注意的是保持代码的清晰和可读性。同时,因为这段代码涉及到大量的数学运算,所以在增加功能的同时也要考虑性能的影响。
当然!以下是一个修改后的示例,增加了颜色和交互性。请注意,这个示例需要终端支持ANSI escape codes来显示颜色。
- #include
- #include
- #include
- #include
-
- // 定义ANSI escape codes
- #define COLOR_RESET "\033[0m"
- #define COLOR_RED "\033[31m"
- #define COLOR_GREEN "\033[32m"
- #define COLOR_YELLOW "\033[33m"
- #define COLOR_BLUE "\033[34m"
-
- int main() {
- float A = 0, B = 0;
- float i, j;
- int k;
- float z[1760];
- char b[1760];
-
- printf("\x1b[2J"); // 清屏
-
- bool running = true;
- int colorIndex = 0;
-
- while (running) {
- memset(b, 32, 1760);
- memset(z, 0, 7040);
-
- for (j = 0; j < 6.28; j += 0.07) {
- for (i = 0; i < 6.28; i += 0.02) {
- // ... (保持不变的部分)
- float c = cos(i);
- float d = sin(j);
- float e = cos(A);
- float f = cos(j);
- float g = sin(A);
- float h = d + 2;
- float D = 1 / (c * h * e + f * g + 5);
- float l = sin(i);
- float m = sin(B);
- float n = cos(B);
- float t = c * h * g - f * e;
- int x = 40 + 30 * D * (l * h * m - t * n);
- int y = 12 + 15 * D * (l * h * n + t * m);
- int o = x + 80 * y;
- int N = 8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n);
- // 在这里增加颜色
- const char* colorCode;
- switch (colorIndex) {
- case 0:
- colorCode = COLOR_RED;
- break;
- case 1:
- colorCode = COLOR_GREEN;
- break;
- case 2:
- colorCode = COLOR_YELLOW;
- break;
- case 3:
- colorCode = COLOR_BLUE;
- break;
- default:
- colorCode = COLOR_RESET;
- break;
- }
-
- if (22 > y && y > 0 && x > 0 && 80 > x && D > z[o]) {
- z[o] = D;
- b[o] = colorCode[0]; // 使用颜色代码作为字符的前缀
- b[o + 1] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
- }
- }
- }
-
- printf("\x1b[H"); // 移动光标到屏幕左上角
-
- for (k = 0; k < 1761; k++) {
- putchar(k % 80 ? b[k] : 10); // 输出字符或换行
- A += 0.00004;
- B += 0.00002;
- }
-
- // 增加交互性:根据用户输入改变颜色
- char input;
- if (scanf(" %c", &input) == 1) {
- if (input == 'r') { // 'r' 键重置颜色索引
- colorIndex = 0;
- } else if (input >= '1' && input <= '4') { // 数字键1-4更改颜色索引
- colorIndex = input - '1';
- } else if (input == 'q') { // 'q' 键退出程序
- running = false;
- }
- }
-
- usleep(30000); // 暂停一段时间,控制动画速度
- }
- return 0;
- }
这个警告是因为你试图将一个字符串常量(这里是COLOR_RED
)赋值给一个非const的char*
(这里是colorCode
)。在C++中,字符串常量被视为const char类型,因此它们不能被赋值给一个非const的char*。
为了解决这个问题,你应该将colorCode
声明为const char*
。如果你的代码里的其它地方需要修改colorCode
指向的内容,那你就需要重新考虑你的设计。一般来说,字符串常量(比如这里的颜色代码)应该是只读的。
所以你应该修改你的代码,使得colorCode
被声明为const char*
。例如:
- const char* colorCode;
- switch (colorIndex) {
- case 0:
- colorCode = COLOR_RED;
- break;
- case 1:
- colorCode = COLOR_GREEN;
- break;
- case 2:
- colorCode = COLOR_YELLOW;
- break;
- case 3:
- colorCode = COLOR_BLUE;
- break;
- default:
- colorCode = COLOR_RESET;
- break;
- }
cpp
const char* colorCode; | |
switch (colorIndex) { | |
case 0: | |
colorCode = COLOR_RED; | |
break; | |
case 1: | |
colorCode = COLOR_GREEN; | |
break; | |
case 2: | |
colorCode = COLOR_YELLOW; | |
break; | |
case 3: | |
colorCode = COLOR_BLUE; | |
break; | |
default: | |
colorCode = COLOR_RESET; | |
break; | |
} |
然后,在你的代码中使用colorCode
时,如果之前的代码试图修改它指向的字符串内容(比如通过strcpy
或者[]
操作符),那么你需要修复那些代码,使其不再试图修改字符串常量。如果这些代码需要修改字符串,那么他们应该使用堆或者栈上分配的(非const)字符串,而不是字符串常量。