diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..32a7a09 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,32 @@ +{ + "configurations": [ + { + "name": "C/C++: gcc build and debug active file", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ], + "preLaunchTask": "C/C++: gcc build active file", + "miDebuggerPath": "/usr/bin/gdb", + "internalConsoleOptions": "neverOpen" + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..37f31b3 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,25 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": "build", + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/3/fabo b/3/fabo new file mode 100644 index 0000000..ee9309b Binary files /dev/null and b/3/fabo differ diff --git a/3/fabo.c b/3/fabo.c new file mode 100644 index 0000000..8c252a8 --- /dev/null +++ b/3/fabo.c @@ -0,0 +1,37 @@ +#include +#include + +int main(int argc, char *argv[]) { + int n, i; + long long a = 0, b = 1, c; + + // 从命令行参数或键盘获取n + if (argc > 1) { + n = atoi(argv[1]); + } + else { + printf("请输入一个整数: "); + scanf("%d", &n); + } + + // 验证输入 + if (n <= 0) { + printf("请输入一个正整数\n"); + return 1; + } + + // 生成并输出斐波那契数列的前n项 + printf("%lld\n", a); // 第1项: 0 + if (n > 1) { + printf("%lld\n", b); // 第2项: 1 + } + + for (i = 3; i <= n; i++) { + c = a + b; + printf("%lld\n", c); + a = b; + b = c; + } + + return 0; +} diff --git a/3/faboexec b/3/faboexec new file mode 100644 index 0000000..7854f1e Binary files /dev/null and b/3/faboexec differ diff --git a/3/faboexec.c b/3/faboexec.c new file mode 100644 index 0000000..05109af --- /dev/null +++ b/3/faboexec.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int pipefd[2]; // 管道文件描述符 + pid_t pid; + int status; + char buf[1024]; + long long sum = 0; + long long num; + + // 获取输入n + int n; + if (argc > 1) { + n = atoi(argv[1]); + } + else { + printf("请输入一个整数: "); + scanf("%d", &n); + } + + // 创建管道 + if (pipe(pipefd) == -1) { + perror("pipe"); + exit(EXIT_FAILURE); + } + + // 创建子进程 + pid = fork(); + + if (pid < 0) { // 创建子进程失败 + perror("fork"); + exit(EXIT_FAILURE); + } + else if (pid == 0) { // 子进程 + // 关闭管道读端 + close(pipefd[0]); + + // 将标准输出重定向到管道写端 + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + + // 执行fabo程序,传入参数n + char n_str[20]; + sprintf(n_str, "%d", n); + execl("./fabo", "fabo", n_str, NULL); + + // 如果execl失败,则执行下面的代码 + perror("execl"); + exit(EXIT_FAILURE); + } + else { // 父进程 + // 关闭管道写端 + close(pipefd[1]); + + // 从管道读取子进程输出的每个斐波那契数,并计算总和 + FILE *fp = fdopen(pipefd[0], "r"); + if (fp == NULL) { + perror("fdopen"); + exit(EXIT_FAILURE); + } + + while (fgets(buf, sizeof(buf), fp) != NULL) { + num = strtoll(buf, NULL, 10); + sum += num; + } + + // 关闭文件和管道读端 + fclose(fp); + + // 等待子进程结束 + waitpid(pid, &status, 0); + + // 打印总和 + printf("斐波那契数列前%d项的总和为: %lld\n", n, sum); + } + + return 0; +}