#include <stdio.h> #include <string.h> #include <stdlib.h> char res[30] = {'\0'}; int find(char* a, int length, char x) { for (int i = 0; i < length; i++) { if (x == a[i]) return i + 1; } return 0; } void solve(char* post, char* in, int posts, int poste, int ins, int ine, int length) { if (posts > poste || ins > ine) { return; } if (posts == poste || ins == ine) { printf("%c", in[ins - 1]); int t ; t= strlen(res); res[t] = in[ins - 1]; return; } char root = post[poste - 1]; int t; t= strlen(res); res[t] = post[poste - 1]; printf("%c", root); int pos = find(in, length, root); solve(post, in, posts, posts + pos - ins - 1, ins, pos - 1, length); solve(post, in, posts + pos - ins, poste - 1, pos + 1, ine, length); } int main() { char b[30] = {'\0'},c[30] = {'\0'}; while (scanf("%s", b) != EOF && scanf("%s", c) != EOF) { int m; m = (int)strlen(b); solve(b, c, 1, m, 1, m, m); printf("\n"); memset(b, 0, 30); memset(c, 0, 30); memset(res, 0, 30); } return 0; }
时间: 2023-06-25 19:06:44 浏览: 98
这段代码是一个二叉树的后序遍历和中序遍历的输出,它的实现方式是先找到后序遍历的最后一个元素作为根节点,然后在中序遍历中找到根节点的位置,将中序遍历分成左子树和右子树,再递归的对左右子树进行同样的操作,最后输出二叉树的后序遍历结果。
具体实现方式是通过递归函数solve来实现,输入参数为后序遍历序列post、中序遍历序列in,以及post和in序列的起始和结束位置。如果post和in序列都为空,则直接返回;如果只有一个元素,则输出该元素并返回。否则,取post序列的最后一个元素作为根节点,在in序列中找到根节点的位置pos,将in序列分成左右两部分,递归调用solve函数对左右两部分进行同样的操作,最后输出根节点。
值得注意的是,在递归过程中,代码还会记录每个节点的值,将其存储在res数组中,最后输出res数组即可得到二叉树的后序遍历结果。
相关问题
请帮我解释以下程序关键句含义#include<stdlib.h>#include<unistd.h>#include<fcntl.h>#include<string.h>#include<stdio.h>int main(int argc,char* argv[]){ char* filepath=argv[1]; char* operate=argv[2]; int fd=open(filepath,O_CREAT|O_RDWR); char buff[50]; if(strcmp("write",operate)==0){ printf("Please input:\n"); read(0,buff,30); write(fd,buff,30); }else if(strcmp("read",operate)==0){ size_t res=read(fd,buff,30); if(res<0){ printf("fail to read"); } printf("the content of the file is "); printf("%s",buff); }}
这段程序是一个简单的文件读写程序,其主要功能是根据用户的输入对指定的文件进行读写操作。程序的关键句如下:
```
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
int main(int argc,char* argv[]){
char* filepath=argv[1];
char* operate=argv[2];
int fd=open(filepath,O_CREAT|O_RDWR);
char buff[50];
if(strcmp("write",operate)==0){
printf("Please input:\n");
read(0,buff,30);
write(fd,buff,30);
}else if(strcmp("read",operate)==0){
size_t res=read(fd,buff,30);
if(res<0){
printf("fail to read");
}
printf("the content of the file is ");
printf("%s",buff);
}
}
```
程序的第一行到第五行是包含所需的头文件。这些头文件中包含了一些函数和宏定义,用于文件操作、字符串操作和输入输出等操作。
程序的主函数是`main`,它有两个参数,一个是整型参数`argc`,表示命令行参数的数量,另一个是字符指针数组`argv[]`,表示命令行参数的具体内容。
程序的第六行和第七行用来获取命令行参数中的文件路径和操作类型。其中,`argv[1]`表示第二个参数,即文件路径;`argv[2]`表示第三个参数,即操作类型。
程序的第八行通过`open`函数打开指定路径的文件,并返回一个文件描述符`fd`。`O_CREAT`和`O_RDWR`是文件打开的模式,其中`O_CREAT`表示如果文件不存在,则自动创建该文件,`O_RDWR`表示以读写模式打开文件。
程序的第九行定义了一个字符数组`buff`,用于存储文件读写的内容。
程序的第十行到第十六行是根据用户输入的操作类型,进行文件的读写操作。如果操作类型是`write`,则通过`printf`函数提示用户输入内容,然后通过`read`函数从标准输入(键盘)读取用户输入的内容,并将其写入文件中。如果操作类型是`read`,则通过`read`函数从文件中读取内容,并通过`printf`函数将读取到的内容输出到屏幕上。如果读取失败,则通过`printf`函数提示用户读取失败。
修改这段代码,让它能够在gcc编译器中顺利的运行#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> char message[50] = "Hello World"; void *thread_function(void *arg) { printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg); sleep(5); strcpy(message, "HELLO FATHER!"); pthread_exit("===Thank you for your CPU time!"); } int main() { int res; pthread_t threadCH; void *thread_result; res = pthread_create(&threadCH, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed!"); exit(EXIT_FAILURE); } printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n"); res = pthread_join(threadCH, &thread_result); if (res != 0) { perror("Thread join failed!\n"); exit(EXIT_FAILURE); } printf("CHILD THREAD returned %s\n", (char *)thread_result); printf("Message NOW is %s\n", message); exit(EXIT_FAILURE); }//main
这段代码在我的gcc编译器中可以顺利运行,这里我提供一些修改的建议:
1. 在调用pthread_exit()函数时,需要传递一个void*类型的指针作为线程的返回值,而不是直接传递一个字符串。因此,需要定义一个指针变量,将字符串的地址赋值给它,然后再将指针变量作为参数传递给pthread_exit()函数。
2. 在printf()函数中,需要将换行符“\n”放在双引号内,否则会出现编译错误。
下面是修改后的代码:
```
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
char message[50] = "Hello World";
void *thread_function(void *arg) {
printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg);
sleep(5);
strcpy(message, "HELLO FATHER!");
char *ret = "===Thank you for your CPU time!";
pthread_exit(ret);
}
int main() {
int res;
pthread_t threadCH;
void *thread_result;
res = pthread_create(&threadCH, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed!");
exit(EXIT_FAILURE);
}
printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n");
res = pthread_join(threadCH, &thread_result);
if (res != 0) {
perror("Thread join failed!\n");
exit(EXIT_FAILURE);
}
printf("CHILD THREAD returned %s\n", (char *)thread_result);
printf("Message NOW is %s\n", message);
exit(EXIT_SUCCESS);
}
```