利用栈实现整数的十进制转八进制
#include "stack_.h"
Stack* Stack_Create(int maxlen)
{
Stack* stk = (Stack*)malloc(sizeof(Stack));
stk->data = (T*)malloc(sizeof(T)*maxlen);
stk->max = maxlen;
stk->top = -1;
return stk;
}
void Stack_Free(Stack* stk)
{
free(stk->data);
free(stk);
}
void Stack_MakeEmpty(Stack* stk)
{
stk->top = -1;
}
bool Stack_IsEmpty(Stack* stk)
{
return -1 == stk->top;
}
bool Stack_IsFull(Stack* stk)
{
return stk->top == stk->max-1;
}
T Stack_Top(Stack* stk)
{
return stk->data[stk->top];
}
T Stack_Push(Stack* stk, T e)
{
if(Stack_IsFull(stk)) {
printf("Stack_IsFull(): stack full error when push element to the stack!\n");
Stack_Free(stk);
exit(0);
}
else{
stk->top += 1;
stk->data[stk->top] = e;
return Stack_Top(stk);
}
}
T Stack_Pop(Stack* stk)
{
if(Stack_IsEmpty(stk)) {
printf("Stack_IsEmpty(): stack empty error when pop element of the stack top!\n");
Stack_Free(stk);
exit(0);
}
else{
T topE = Stack_Top(stk);
stk->top -= 1;
return topE;
}
}
void Stack_Print(Stack* stk)
{
if (Stack_IsEmpty(stk)) {
printf("The stack is empty.\n");
return;
}
for (int i=stk->top; i>=0; i--) {
printf("%d", stk->data[i]);
}
printf("\n");
}
void Decimal_Conversion_Octal(T e)
{
Stack *a=Stack_Create(32);
while(e)
{
Stack_Push(a,e%8);
e=e/8;
}
while(!Stack_IsEmpty(a))
{
e=Stack_Pop(a);
printf("%d",e);
}
}
利用栈判断字符串括号是否匹配
#include "stack_.h"
Stack* Stack_Create(int maxlen)
{
Stack* stk = (Stack*)malloc(sizeof(Stack));
stk->data = (T*)malloc(sizeof(T)*maxlen);
stk->max = maxlen;
stk->top = -1;
return stk;
}
void Stack_Free(Stack* stk)
{
free(stk->data);
free(stk);
}
void Stack_MakeEmpty(Stack* stk)
{
stk->top = -1;
}
bool Stack_IsEmpty(Stack* stk)
{
return -1 == stk->top;
}
bool Stack_IsFull(Stack* stk)
{
return stk->top == stk->max-1;
}
T Stack_Top(Stack* stk)
{
return stk->data[stk->top];
}
T Stack_Push(Stack* stk, T e)
{
if(Stack_IsFull(stk)) {
printf("Stack_IsFull(): stack full error when push element to the stack!\n");
Stack_Free(stk);
exit(0);
}
else{
stk->top += 1;
stk->data[stk->top] = e;
return Stack_Top(stk);
}
}
T Stack_Pop(Stack* stk)
{
if(Stack_IsEmpty(stk)) {
printf("Stack_IsEmpty(): stack empty error when pop element of the stack top!\n");
Stack_Free(stk);
exit(0);
}
else{
T topE = Stack_Top(stk);
stk->top -= 1;
return topE;
}
}
void Stack_Print(Stack* stk)
{
if (Stack_IsEmpty(stk)) {
printf("The stack is empty.\n");
return;
}
for (int i=stk->top; i>=0; i--) {
printf("%d", stk->data[i]);
}
printf("\n");
}
void Bracket_Match(T* str, int len)
{
Stack *zhan=Stack_Create(len);
int i; char c;
bool flag=1;
Stack_IsFull(zhan);
for(i=0;i<len;++i)
{
switch(str[i])
{
case'(':
case'[':
case'{':Stack_Push(zhan,str[i]);
break;
case')':
case']':
case'}':Stack_Top(zhan);
c=zhan->data[zhan->top];
if((c=='('&&str[i]==')')||(c=='['&&str[i]==']')||(c=='{'&&str[i]=='}')){
Stack_Pop(zhan);
}
else{
flag=0;
}
}
}
if(zhan->top!=-1)
flag=0;
if(flag==1) printf("YES\n");
else printf("NO\n");
}
利用栈判断字符串是否为回文串
#include "stack_.h"
Stack* Stack_Create(int maxlen)
{
Stack* stk = (Stack*)malloc(sizeof(Stack));
stk->data = (T*)malloc(sizeof(T)*maxlen);
stk->max = maxlen;
stk->top = -1;
return stk;
}
void Stack_Free(Stack* stk)
{
free(stk->data);
free(stk);
}
void Stack_MakeEmpty(Stack* stk)
{
stk->top = -1;
}
bool Stack_IsEmpty(Stack* stk)
{
return -1 == stk->top;
}
bool Stack_IsFull(Stack* stk)
{
return stk->top == stk->max-1;
}
T Stack_Top(Stack* stk)
{
return stk->data[stk->top];
}
T Stack_Push(Stack* stk, T e)
{
if(Stack_IsFull(stk)) {
printf("Stack_IsFull(): stack full error when push element to the stack!\n");
Stack_Free(stk);
exit(0);
}
else{
stk->top += 1;
stk->data[stk->top] = e;
return Stack_Top(stk);
}
}
T Stack_Pop(Stack* stk)
{
if(Stack_IsEmpty(stk)) {
printf("Stack_IsEmpty(): stack empty error when pop element of the stack top!\n");
Stack_Free(stk);
exit(0);
}
else{
T topE = Stack_Top(stk);
stk->top -= 1;
return topE;
}
}
void Stack_Print(Stack* stk)
{
if (Stack_IsEmpty(stk)) {
printf("The stack is empty.\n");
return;
}
for (int i=stk->top; i>=0; i--) {
printf("%d", stk->data[i]);
}
printf("\n");
}
void Palindrome(T* str, int len)
{
if(len==1){printf("YES\n");}
else
{
Stack *z=Stack_Create(100);
if(len%2!=0)
{
int i;
for(i=len/2;i<len;i++)
{
str[i]=str[i+1];
}
for(i=0;i<len-1;i++)
{
char st;
st=Stack_Top(z);
if(st==str[i])
{
Stack_Pop(z);
}
else
{
Stack_Push(z,str[i]);
}
}
if(Stack_IsEmpty(z))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
else
{
for(int i=0;i<len;i++)
{
char st=Stack_Top(z);
if(st==str[i])
{
Stack_Pop(z);
}
else
{
Stack_Push(z,str[i]);
}
}
if(Stack_IsEmpty(z))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
}
算术表达式求值
#include<stdio.h>
#include<stdlib.h>
#define defaultsize 10
#define increasesize 5
typedef struct
{
char *base;
char *top;
int stacksize;
}OPRTstack;
typedef struct
{
double *base;
double *top;
int stacksize;
}NUMstack;
int createStack(OPRTstack*s)
{
s->base=(char*)malloc(sizeof(char)*defaultsize);
if(!s->base)return 0;
s->top=s->base;
s->stacksize=10;
return 1;
}
int pop(OPRTstack *s,char *e)
{
if(s->top==s->base)return 0;
s->top--;
*e=*(s->top);
return 1;
}
int push(OPRTstack*s,char e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(char*)realloc(s->base,sizeof(char)*(s->stacksize+increasesize));
if(!s->base)return 0;
s->top=s->base+s->stacksize;
s->stacksize+=increasesize;
}
*(s->top)=e;
s->top++;
}
int isEmpty(OPRTstack *s)
{
if(s->top==s->base)return 1;
else return 0;
}
char GetTop(OPRTstack *s)
{
if(!isEmpty(s))
{
char*temp=s->top;
temp--;
return *(temp);
}
else return '!';
}
void showStack(OPRTstack*s)
{
if(isEmpty(s))return ;
for(int i=0;i<s->top-s->base;i++)
{
printf("%c ",s->base[i]);
}
}
int createStack(NUMstack*s)
{
s->base=(double*)malloc(sizeof(double)*defaultsize);
if(!s->base)return 0;
s->top=s->base;
s->stacksize=10;
return 1;
}
int pop(NUMstack *s,double *e)
{
if(s->top==s->base)return 0;
s->top--;
*e=*(s->top);
return 1;
}
int push(NUMstack*s,double e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(double*)realloc(s->base,sizeof(double)*(s->stacksize+increasesize));
if(!s->base)return 0;
s->top=s->base+s->stacksize;
s->stacksize+=increasesize;
}
*(s->top)=e;
s->top++;
}
int isEmpty(NUMstack *s)
{
if(s->top==s->base)return 1;
else return 0;
}
double GetTop(NUMstack *s)
{
if(!isEmpty(s))
{
double *temp=s->top;
temp--;
return *(temp);
}
else return -1;
}
void showStack(NUMstack*s)
{
if(isEmpty(s))return ;
for(int i=0;i<s->top-s->base;i++)
{
printf("%f ",s->base[i]);
}
printf(" ");
}
int isOPRT(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')return 1;
else return 0;
}
char compare(char a,char b)
{
if(a=='+')
{
if(b=='*'||b=='/'||b=='(') return '<';
else return '>';
}
else if(a=='-')
{
if(b=='*'||b=='/'||b=='(') return '<';
else return '>';
}
else if(a=='*')
{
if(b=='(')return '<';
else return '>';
}
else if(a=='/')
{
if(b=='(')return '<';
else return '>';
}
else if(a=='(')
{
if(b==')')return '=';
else if(b=='#') return '!';
else return '<';
}
else if(a==')')
{
if(b=='(')return '!';
else return '>';
}
else if(a=='#')
{
if(b==')')return '!';
if(b=='#')return '=';
else return '<';
}
}
double calculate(double left,double right, char operators)
{
switch(operators)
{
case '+':
return left+right;
case '-':
return 1.0*left-right;
case '*':
return left*right;
case '/':
return 1.0*left/right;
}
}
int main()
{
OPRTstack oprt;
NUMstack num;
NUMstack temp;
int build=0;
double index;
int complex=1;
char operators;
double left,right;
createStack(&num);
createStack(&oprt);
createStack(&temp);
push(&oprt,'#');
char c=getchar();
int error=0;
while(c!='#'||GetTop(&oprt)!='#')
{
while(!isOPRT(c))
{
push(&temp,c-'0');
c=getchar();
}
while(!isEmpty(&temp))
{
pop(&temp,&index);
build+=(index*complex);
complex*=10 ;
}
complex=1;
if(build)push(&num,double(build));
build=0;
if(isOPRT(c))
{
switch(compare(GetTop(&oprt),c)){
case '<':
push(&oprt,c);
c=getchar();
break;
case '=':
pop(&oprt,&operators);
c=getchar();
break;
case '>':
pop(&oprt,&operators);
pop(&num,&right);
pop(&num,&left);
push(&num,calculate(left,right,operators));
break;
case '!':
printf("Syntax Error!");
error=1;
break;
}
}
if(error)break;
}
if(!error)printf("%d",int(GetTop(&num)));
return 0;
}
迷宫求解
#include <stdio.h>
#include<stdlib.h>
int mg[11][10]= {{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,1,1},{1,0,1,1,1,1,1,1,0,1},{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,1,1},{1,1,1,1,1,0,0,0,1,1},{1,1,1,0,0,0,1,0,1,1},{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}
};
int M=9;
int N=8;
typedef struct
{
int i;
int j;
int di;
} Box;
typedef struct
{
Box data[110];
int top;
} StType;
bool mgpath(int xi,int yi,int xe,int ye)
{
int i,j,k,di,find;
StType st;
st.top=-1;
st.top++;
st.data[st.top].i=xi;
st.data[st.top].j=yi;
st.data[st.top].di=-1;
mg[xi][yi]=-1;
while(st.top>-1)
{
i=st.data[st.top].i;
j=st.data[st.top].j;
di=st.data[st.top].di;
if(i==xe&&j==ye)
{
printf("有通路:");
for(k=0;k<=st.top;k++)
{
printf("(%d,%d)",st.data[k].i,st.data[k].j);
}
return true;
}
find=0;
while(di<5&&find==0)
{
di++;
switch(di)
{
case 0:
i=st.data[st.top].i-1;
j=st.data[st.top].j;
break;
case 1:
i=st.data[st.top].i;
j=st.data[st.top].j+1;
break;
case 2:
i=st.data[st.top].i+1;
j=st.data[st.top].j;
break;
case 3:
i=st.data[st.top].i;
j=st.data[st.top].j-1;
break;
}
if(mg[i][j]==0)
find=1;
}
if(find==1)
{
st.data[st.top].di=di;
st.top++;
st.data[st.top].i=i;
st.data[st.top].j=j;
st.data[st.top].di=-1;
mg[i][j]=-1;
}
else
{
mg[st.data[st.top].i][st.data[st.top].j]=0;
st.top--;
}
}
return false;
}
int main()
{
if(!mgpath(1,1,M,N))
printf("无解");
return 0;
}