当前位置:首页 » 编程语言 » instr函数c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

instr函数c语言

发布时间: 2023-03-06 17:17:23

1. C语言 编写递归函数

标个记号准备上传对大神的源码分析。好了,我分析了上楼大神的代码实现,具体参考他的代码,分享下。注:可以看看《算法精解》Kyle Loudon着 或者《数据结构》主编 安训国他们说的堆栈原理。

#include<stdio.h>

char*dg(char*instr,char*outstr,char*outstr2)
{
if(*instr==0)
{
*outstr=0;
returnoutstr2;
}
*(outstr+1)=*instr;
outstr=dg(instr+1,outstr+2,outstr2);
/*下两句一直不执行,直到outstr=dg(instr+5,outstr+10,outstr2)返回后才执行,其后不断执行后三句!*/
*outstr=*instr-32;
returnoutstr+2;
}

intmain()
{
charbuf[50];
dg("aybdx",buf,buf);
puts(buf);
return0;
}

2. C语言题目

#include
#include
#include
#include
#include

//定义二叉树节点数据结构
typedef
struct
node{
struct
node
*lchild;
char
data;
struct
node
*rchild;
}bitnode,*bitree;
//构造截取子串函数,start从零开始.
char
*get_substr(char
*strDest,int
start,int
end)
{
if(start>end)
return
NULL;
//如果左子树或右子树为空则返回空串
char
*strSub;
//字串指针
strSub=(char*)malloc((end-start+2)*sizeof(char));
int
i;
for(i=start;i<=end;i++)
strSub[i-start]=strDest[i];
strSub[end-start+1]='\0';
return
strSub;
}
//前序遍历二叉树,并输出
void
preorder_traverse(bitree
bt){
if(bt
!=
NULL)
{
printf("%c",bt->data);
preorder_traverse(bt->lchild);
preorder_traverse(bt->rchild);
}
}
//根据中序和后序遍历结果递归构造二叉树,并返回根指针
bitree
create_bitree(char
*inorder,char
*postorder)
//inorder和postoeder分别为中序和后序遍历的结果字符串
{
if(inorder==NULL
||
postorder==NULL)
return
NULL;
//空串则返回空树
char
root_data;//根节点字符
char
*lchild_instr,*lchild_postr,*rchild_instr,*rchild_postr;//左子树和右子树中序及后序字符串
int
i,len;
len=strlen(inorder);
//中序和后序字符串长度相等
bitree
new_bitree=(bitree)malloc(sizeof(bitnode));
root_data=postorder[len-1];//树的根节点必然为中序遍历的最后一个字符
new_bitree->data=root_data;
for(i=0;i<len;i++)
//找到根节点在中序遍历中位置
{
if(inorder[i]==root_data)
break;
}
//确定左子树和右子树中序及后序字符串
lchild_instr=get_substr(inorder,0,i-1);
lchild_postr=get_substr(postorder,0,i-1);
rchild_instr=get_substr(inorder,i+1,len-1);
rchild_postr=get_substr(postorder,i,len-2);
//递归构造子树
new_bitree->lchild=create_bitree(lchild_instr,lchild_postr);
new_bitree->rchild=create_bitree(rchild_instr,rchild_postr);
return
new_bitree;
}
void
main(){
char
inorder[20]="BDCEAFHG";
char
postorder[20]="DECBHGFA";
bitree
root;
root=create_bitree(inorder,postorder);
//输出前序遍历结果
printf("the
binary
tree
in
preorder
is:\n");
preorder_traverse(root);
_getch();
}
//满意的话别忘了多加点分哈

3. C语言作业问题,为什么会错求大虾求解!!!

第一个程序存在以下语法问题:
1、Findmax函数返回值应该为float,即float Findmax(float a[],int m,int *n);
2、Findmax函数体中,指针变量n重复定义了,参数已经有n,函数体里面就不需要再定义了。或者是你本来想给*n赋值为0的(也应该这么做),结果误接到i的定义后面了;
3、Findmax函数的调用问题,其中数组参数只需要写数组名就行了,即Findmax(b, 10, &i);
另外,程序中定义的变量最好都初始化个值,如main函数中的i和max,赋个0初始值比较安全

第二个程序:
1、函数在调用前若没定义则要有前置声明,即在main函数前面加个void InvStr(char *s, int n);声明;
2、函数调用错误,应该为InvStr(s, n);同时s和n要先定义,如char s[128]; int n = 0;
3、InStr函数并没有用到函数的参数,而是在函数里自己又定义了个字符数组来进行操作。我想你本意应该是想通过这个函数来修改外面的字符串。大概给你修改下代码,尽量保留了你代码原味的:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void InvStr(char *s, int n);//使用前要前置函数声明
void main()
{
char s[128];
InvStr(s, strlen(s));//函数的调用
printf ("%s\n",s);
system("pause");
}

void InvStr(char *s, int n)
{
char ch = 0;
char *p = NULL, *q = NULL;
gets (s);//操作的是从外面传进来的s
p = s;
q=p+strlen(p) - 1;
while (p<q)
{
ch=*p;
*p++=*q;
*q--=ch;
}
}