This commit is contained in:
xixu-me committed 2025-04-02 16:07:18 +08:00
1 parent a603de5e5f
commit e5f6233672
6 files changed
+177

No files matched your search

+32
View File
@@ -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"
}
+25
View File
@@ -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"
}
BIN
View File
Binary file not shown.
+37
View File
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
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;
}
BIN
View File
Binary file not shown.
+83
View File
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
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;
}