當前位置:首頁 » 編程語言 » 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;
}
}