-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
typedef struct{ | ||
ElemType *base[2]; | ||
ElemType *top[2]; | ||
}BDStacktype; //双向栈类型 | ||
int Init_Stack(BDStacktype &tws,int m){ | ||
//初始化一个大小为m 的双向栈tws | ||
tws.base[0]=(Elemtype*)malloc(sizeof(Elemtype)); | ||
tws.base[1]=tws.base[0]+m; | ||
tws.top[0]=tws.base[0]; | ||
tws.top[1]=tws.base[1]; | ||
return 1; | ||
} | ||
int push(BDStacktype &tws,int i,Elemtype x){ | ||
//x 入栈,i=0 表示低端栈,i=1 表示高端栈 | ||
if(tws.top[0]>tws.top[1]) | ||
return 0; | ||
if(i==0) | ||
*tws.top[0]++=x; | ||
else if(i==1) | ||
*tws.top[1]--=x; | ||
else | ||
return 0; | ||
return 1; | ||
} | ||
int pop(BDStacktype &tws,int i,Elemtype &x){ | ||
//x 出栈,i=0 表示低端栈,i=1 表示高端栈 | ||
if(i==0){ | ||
if(tws.top[0]==tws.base[0]) | ||
return 0; | ||
x=*--tws.top[0]; | ||
} | ||
else if(i==1){ | ||
if(tws.top[1]==tws.base[1]) | ||
return 0; | ||
x=*++tws.top[1]; | ||
} | ||
else | ||
return 0; | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
void trainArrange(char *train){ | ||
//train±íʾÁгµ | ||
p=train; | ||
q=train; | ||
InitStack(s); | ||
while(*p){ | ||
if(*p=='H') | ||
push(s,*p); | ||
else | ||
*(q++)=*p; | ||
p++; | ||
} | ||
while(!StackEmpty(s)){ | ||
pop(s,c); | ||
*(q++)=c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
int isReverse(){ | ||
//判断输入的字符串中'&'前和'&'后部分的字符串是否互逆。 | ||
InitStack(s); | ||
while((e=getchar())!='&'){ | ||
if(e=='@') | ||
return 0; | ||
push(s,e); | ||
} | ||
while((e=getchar())1='@'){ | ||
if(StackEmpty(s)) | ||
return 0; | ||
pop(s,c); | ||
if(e!=c) | ||
return 0 | ||
} | ||
if(!StackEmpty(s)) | ||
return 0; | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
int bracketTest(char *str){ | ||
//判断表达式中的小括号是否匹配 | ||
count=0; | ||
for(p=str;*p;p++){ | ||
if(*p=='(') | ||
count++; | ||
else if(*p==')') | ||
count--; | ||
if(count<0) | ||
return 0; | ||
} | ||
if(count) | ||
return 0; | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
int allBracketsTest(char *str){ | ||
//判别表达式中三种括号是否匹配 | ||
InitStack(s); | ||
for(p=str;*p;p++){ | ||
if(*p=='(' || *p=='[' || *p=='{') | ||
push(s,*p); | ||
else if(*p==')' || *p==']' || *p=='}'){ | ||
if(StackEmpty(s)) | ||
return 0; | ||
pop(s,c); | ||
if(*p==')' && c!='(') | ||
return 0; | ||
if(*p==']' && c!= '[') | ||
return 0; | ||
if(*p=='}' && c!='{') | ||
return 0; | ||
} | ||
} | ||
if(!StackEmpty(s)) | ||
return 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
typedef struct{ | ||
int x; | ||
int y; | ||
}coordinate; | ||
int replaceColor(int g[m][n],int i,int j,int color){ | ||
//把点(i,j)相邻区域的点置换为color | ||
coordinate a; | ||
old=g[i][j]; | ||
a.x=i;a.y=j; | ||
InitiQueue(Q); | ||
EnQueue(Q,a); | ||
while(!QueueEmpty(Q)){ | ||
DeQueue(Q,a); | ||
x=a.x; | ||
y=a.y; | ||
if(x>1) | ||
if(g[x-1][y]==old){//修改左邻点的颜色 | ||
g[x-1][y]=color; | ||
a.x=x-1;a.y=y; | ||
EnQueue(Q,a); | ||
} | ||
if(y>1) | ||
if(g[x][y-1]==old){//修改上邻点的颜色 | ||
g[x][y-1]=color; | ||
a.x=x;a.y=y-1; | ||
EnQueue(Q,a); | ||
} | ||
if(x<m) | ||
if(g[x+1][y]==old){//修改右邻点的颜色 | ||
g[x+1][y]=color; | ||
a.x=x+1;a.y=y; | ||
EnQueue(Q,a); | ||
} | ||
if(y<n) | ||
if(g[x][y+1]==old){//修改下邻点的颜色 | ||
g[x][y+1]=color; | ||
a.x=x;a.y=y+1; | ||
EnQueue(Q,a); | ||
} | ||
} | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
char *RPExpression(char *e){ | ||
//将中缀表达式转化为后缀表达式 | ||
//参数为传入的中缀表达式,设置两个栈,分别存放操作符和逆波兰式 | ||
Stack s1,s2; | ||
InitStack(s1);//存放操作符 | ||
InitStack(s2);//存放逆波兰式 | ||
Push(s1,'#'); | ||
|
||
char *p=e,ch; | ||
int length=0;//表达式长度 | ||
for(*p!='\0';p++){ | ||
switch(*p){ | ||
//遇"("则直接入栈 | ||
case '(': | ||
push(s1,*p); | ||
break; | ||
//遇")"则将距离s1栈顶的最近的"("之间的运算符,逐个出栈,依次送入栈s2,并放弃"(" | ||
case ')': | ||
while(GetTop(s1)!='('){ | ||
pop(s1,ch); | ||
push(s2,ch); | ||
} | ||
pop(s1,ch); | ||
break; | ||
//若当前栈s1的栈顶元素是'(',则当前运算符直接压入栈s1 | ||
//否则,将当前运算符与栈s1的栈顶元素比较,若优先级较栈顶元素大,则直接压入栈s1中 | ||
// 否则将s1栈顶元素弹出,并压入栈s2中,直到栈顶运算符的优先级别低于当前运算符,然后再将当前运算符压入栈s1中 | ||
case '+': | ||
case '-': | ||
for(ch=GetTop(s1);ch!='#';ch=GetTop(s1)){ | ||
if(ch=='('){ | ||
break; | ||
}else{ | ||
pop(s1,ch); | ||
push(s2,ch); | ||
} | ||
} | ||
push(s1,*p); | ||
length++; | ||
break; | ||
case '*': | ||
case '/': | ||
for(ch=GetTop(s1);ch!='#'&&ch!='+'&&ch!='-';ch=GetTop(s1)){ | ||
if(ch=='('){ | ||
break; | ||
}else{ | ||
pop(s1,ch); | ||
push(s2,ch); | ||
} | ||
} | ||
push(s1,*p); | ||
length++; | ||
break; | ||
//遇操作数则直接压入栈s2中 | ||
default: | ||
push(s2,*p); | ||
length++; | ||
} | ||
} | ||
//若栈s1非空,则将栈中元素依次弹出并压入栈s2中 | ||
while(!StackEmpty(s1)&&GetTop(s1)!='#'){ | ||
pop(s1,ch); | ||
push(s2,ch); | ||
} | ||
//最后将栈s2输出,逆序排列成字符串; | ||
char *result; | ||
result=(char *) malloc(sizeof(char)*(length+1)); | ||
result+=length; | ||
*result='\0'; | ||
result--; | ||
for(;!StackEmpty(s2);result--){ | ||
pop(s2,ch); | ||
*result=ch; | ||
} | ||
++result; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
int getValueNiBoLan(char *str){ | ||
//对逆波兰表达式*str求值 | ||
p=str; | ||
InitStack(s);//s为操作数栈 | ||
while(*p){ | ||
if(*p>=48 &&*p<=57) | ||
push(s,*p); | ||
else{ | ||
pop(s,a); | ||
pop(s,b); | ||
r=compute(a,b,*p);//计算 | ||
push(s,r); | ||
} | ||
p++; | ||
} | ||
pop(s,r); | ||
return r; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
int NiBoLanToBoLan(char *str,char *new){ | ||
//将逆波兰表达式str转化为波兰表达式new | ||
p=str; | ||
InitStack(s); | ||
while(*p){ | ||
if(*p>=97 && *p<=122)//是字母 | ||
push(s,*p); | ||
else{ | ||
if(StackEmpty(s)) | ||
return 0; | ||
pop(s,a); | ||
if(StackEmpty(s)) | ||
return 0; | ||
pop(s,b); | ||
c=link(link(*p,b),a);//字母连接 | ||
push(s,c); | ||
} | ||
p++; | ||
} | ||
pop(s,*new); | ||
new++; | ||
if(!StackEmpty(s)) | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
int g(int m,int n){ | ||
if(m==0&&n>=0) | ||
s=0; | ||
else if(m>0&&n>=0) | ||
s=n+g(m-1,2*n); | ||
else | ||
return; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
int FRecursive(int n,int &s){ | ||
//µÝ¹éËã·¨ | ||
if(n<0) | ||
return 0; | ||
if(n==0) | ||
s=n+1; | ||
else{ | ||
FRecusive(n/2,r); | ||
s=n*r; | ||
} | ||
return s; | ||
} | ||
int FNonrecursive(int n,int s){ | ||
//·ÇµÝ¹éËã·¨ | ||
if(n<0) | ||
return 0; | ||
if(n==0) | ||
s=n+1; | ||
else{ | ||
InitStack(s); | ||
while(n!=0){ | ||
a=n; | ||
b=n/2; | ||
push(s,{a,b}); | ||
n=b; | ||
} | ||
s=1; | ||
while(!StackEmpty(s)){ | ||
pop(s,t); | ||
s*=t.a; | ||
} | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
float sqrtRecursive(float A,float p,float e){ | ||
//递归方法求平方根 | ||
if(abs(p*p-A)<=e) | ||
return p; | ||
else | ||
return sqrtRecursive(A,(p+A/p),e); | ||
} | ||
float sqrtNonRecursive(float A,float p,float e){ | ||
//非递归方法求平方根 | ||
while(abs(p*p-A)>=e) | ||
p=(p+A/p)/2; | ||
return p; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//递归算法 | ||
int akm(int m,int n){ | ||
if(m==0) | ||
akm=n+1; | ||
else if(n==0) | ||
akm=akm(m-1,1); | ||
else{ | ||
g=akm(m,n-1); | ||
akm=akm(m-1,g); | ||
} | ||
return akm; | ||
} | ||
//非递归算法 | ||
int akm(int m,int n){ | ||
//利用栈实现递归算法 | ||
top=0; | ||
s[top].mval=m; | ||
s[top].nval=n; | ||
do{ | ||
while(s[top].mval){ | ||
while(s[top].nval){ | ||
top++; | ||
s[top].mval=s[top-1].mval; | ||
s[top].nval=s[top-1].nval-1; | ||
} | ||
s[top].mval--; | ||
s[top].nval=1; | ||
} | ||
if(top>0){ | ||
top--; | ||
s[top].mval--; | ||
s[top].nval=s[top+1].nval+1; | ||
} | ||
}while(top!=0||s[top].mval1=0); | ||
akm=s[top].nval+1; | ||
top--; | ||
return akm; | ||
} |
Oops, something went wrong.