程序代码.docx

上传人:b****9 文档编号:25266917 上传时间:2023-06-06 格式:DOCX 页数:18 大小:18.02KB
下载 相关 举报
程序代码.docx_第1页
第1页 / 共18页
程序代码.docx_第2页
第2页 / 共18页
程序代码.docx_第3页
第3页 / 共18页
程序代码.docx_第4页
第4页 / 共18页
程序代码.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

程序代码.docx

《程序代码.docx》由会员分享,可在线阅读,更多相关《程序代码.docx(18页珍藏版)》请在冰豆网上搜索。

程序代码.docx

程序代码

程序,表达式

#include

#include

#include

charA[200];

intp;

intstack_d[200];

inttop_d=-1;

charstack_o[200];

inttop_o=-1;

intprec(charch1,charch2)

{

switch(ch1)

{

case'#':

{

switch(ch2)

{

case'#':

return0;

case'+':

return-1;

case'-':

return-1;

case'*':

return-1;

case'/':

return-1;

case'(':

return-1;

case')':

return0;

case'^':

return-1;

}

}

case'+':

{

switch(ch2)

{

case'#':

return1;

case'+':

return1;

case'-':

return1;

case'*':

return-1;

case'/':

return-1;

case'(':

return-1;

case')':

return1;

case'^':

return-1;

}

}

case'-':

{

switch(ch2)

{

case'#':

return1;

case'+':

return1;

case'-':

return1;

case'*':

return-1;

case'/':

return-1;

case'(':

return-1;

case')':

return1;

case'^':

return-1;

}

}

case'*':

{

switch(ch2)

{

case'#':

return1;

case'+':

return1;

case'-':

return1;

case'*':

return1;

case'/':

return1;

case'(':

return-1;

case')':

return1;

case'^':

return-1;

}

}

case'/':

{

switch(ch2)

{

case'#':

return1;

case'+':

return1;

case'-':

return1;

case'*':

return1;

case'/':

return1;

case'(':

return-1;

case')':

return1;

case'^':

return-1;

}

}

case'(':

{

switch(ch2)

{

case'#':

return-1;

case'+':

return-1;

case'-':

return-1;

case'*':

return-1;

case'/':

return-1;

case'(':

return-1;

case')':

return0;

case'^':

return-1;

}

}

case'^':

{

switch(ch2)

{

case'#':

return1;

case'+':

return1;

case'-':

return1;

case'*':

return1;

case'/':

return1;

case'(':

return-1;

case')':

return1;

case'^':

return-1;

}

}

}

}

intisdigit(charch)

{

if(ch>='0'&&ch<='9')

return1;

else

return0;

}

intextract()

{

ints=0;

while(isdigit(A[p]))

{

s=s*10+A[p]-'0';

p++;

}

returns;

}

intcompute(intx,inty,charch)

{

inti,s=1;

switch(ch)

{

case'+':

returnx+y;

case'-':

returnx-y;

case'*':

returnx*y;

case'/':

returnx/y;

case'^':

{

for(i=1;i<=y;i++)

s=s*x;

returns;

}

}

}

intmain()

{

inti,j,k,len,x,y;

charch;

scanf("%s",A);

len=strlen(A);

A[len]='#';

stack_o[++top_o]='#';

p=0;

while(p<=len)

{

i=isdigit(A[p]);

if(i==1)

{

j=extract();

stack_d[++top_d]=j;

}

else

{

j=prec(stack_o[top_o],A[p]);

switch(j)

{

case-1:

stack_o[++top_o]=A[p];

p++;

break;

case0:

--top_o;

p++;

break;

default:

y=stack_d[top_d--];

x=stack_d[top_d--];

ch=stack_o[top_o--];

k=compute(x,y,ch);

stack_d[++top_d]=k;

}

}

}

printf("%d",stack_d[0]);

system("pause");

return0;

}

Threading程序

#include

#include

#include

//A(B(D,E(F(J,),G)),C(,H))

structTnode

{

chardata;

structTnode*lchild,*rchild;

};

charA[2000];

intlen;

structTnode*root=0;

charstack[100];

inttop=-1;

voidcreateTree(structTnode*&p,intl,intr)

{

inti,j,k;

if(l>r)

{

return;

}

p=(structTnode*)malloc(sizeof(structTnode));

p->data=A[l];

p->lchild=0;

p->rchild=0;

if(l

{

if(A[l+2]==',')

{

createTree(p->rchild,l+3,r-1);

return;

}

elseif(A[r-1]==',')

{

createTree(p->lchild,l+2,r-2);

return;

}

elseif(A[l+3]==',')

{

createTree(p->lchild,l+2,l+2);

createTree(p->rchild,l+4,r-1);

return;

}

stack[++top]=A[l+3];

i=l+4;

while(i

{

if(A[i]=='(')

{

stack[++top]=A[i];

}

elseif(A[i]==')')

{

top--;

if(top<0)

break;

}

i++;

}

createTree(p->lchild,l+2,i);

createTree(p->rchild,i+2,r-1);

}

}

voidpreorder(structTnode*p)

{

printf("%c",p->data);

if(p->lchild)

{

preorder(p->lchild);

}

if(p->rchild)

{

preorder(p->rchild);

}

}

voidbfs(structTnode*p)

{

structTnode*que[1000];

structTnode*q;

intfront=0,rear=0;

que[rear++]=p;

while(front!

=rear)

{

q=que[front++];

printf("%c",q->data);

if(q->lchild)

{

que[rear++]=q->lchild;

}

if(q->rchild)

{

que[rear++]=q->rchild;

}

}

}

structTnode*stack_i[100];

inttop_i=-1;

voidgofarleft(structTnode*p)

{

structTnode*q=p;

while(q->lchild)

{

stack_i[++top_i]=q;

q=q->lchild;

}

stack_i[++top_i]=q;

}

voidinorder(structTnode*p)

{

structTnode*q;

gofarleft(p);

while(top_i>=0)

{

q=stack_i[top_i--];

printf("%c",q->data);

if(q->rchild)

{

gofarleft(q->rchild);

}

}

}

intmain()

{

inti,j,k;

freopen("input.txt","r",stdin);

freopen("output.txt","w",stdout);

gets(A);

len=strlen(A);

createTree(root,0,len-1);

//preorder(root);

//bfs(root);

inorder(root);

system("pause");

return0;

}

NCHOOSEK

#include

#include

__int64nchoosek(intn,intk)

{

inti,j;

__int64m=1;

for(i=1;i<=k;i++)

{

m=m*(n-i+1);

m=m/i;

}

returnm;

}

intmain()

{

__int64m;

m=nchoosek(13,3)*nchoosek(13,1)

*nchoosek(13,2)*nchoosek(13,4);

printf("%I64d\n",m);

system("pause");

return0;

}

REATION

#include

#include

intF[5001];

intfind(intx)

{

intt;

if(F[x]==x)

returnx;

t=find(F[x]);

F[x]=t;

returnt;

}

intmain()

{

inti,j,k,x,y,fx,fy;

intn,m,p;

scanf("%d%d%d",&n,&m,&p);

for(i=1;i<=n;i++)

{

F[i]=i;

}

for(i=1;i<=m;i++)

{

scanf("%d%d",&x,&y);

fx=find(x);

fy=find(y);

if(fx!

=fy)

{

F[fx]=fy;

}

}

for(i=1;i<=p;i++)

{

scanf("%d%d",&x,&y);

fx=find(x);

fy=find(y);

if(fx!

=fy)

{

printf("NO\n");

}

else

{

printf("YES\n");

}

}

system("pause");

return0;

}POJ1240

#include

#include

#include

intm,len;

charA[100],B[100];

__int64sum=1;

__int64nchoosek(intn,intk)

{

inti,j;

__int64m=1;

for(i=1;i<=k;i++)

{

m=m*(n-i+1);

m=m/i;

}

returnm;

}

voidcount(intprel,intprer,intpostl,intpostr)

{

inti,j,k=0,u;

if(prel==prer)

{

return;

}

i=prel+1;

u=postl;

while(i<=prer)

{

j=u;

while(B[j]!

=A[i])

{

j++;

}

k++;

count(i,j-u+i,u,j);

i=j-u+i+1;

u=j+1;

}

sum=sum*nchoosek(m,k);

}

intmain()

{

scanf("%d%s%s",&m,A,B);

len=strlen(A);

count(0,len-1,0,len-1);

printf("%I64d\n",sum);

system("pause");

return0;

}

KMP算法

#include

#include

#include

charS[1000];

charT[100];

intlens,lent;

intnext[100];

voidmakenext()

{

inti,j;

i=0;j=-1;

next[i]=j;

while(i

{

if(j==-1||T[i]==T[j])

{

i++;

j++;

next[i]=j;

}

else

{

j=next[j];

}

}

}

intgetIndex()

{

inti,j;

i=0;

j=0;

while(i

{

if(j==-1||S[i]==T[j])

{

i++;j++;

}

else

{

j=next[j];

}

}

if(j==lent)

{

returni-j;

}

else

return-1;

}

intmain()

{

inti,j,k;

gets(S);

gets(T);

lens=strlen(S);

lent=strlen(T);

k=getIndex();

printf("%d",k);

system("pause");

return0;

}

KMP1

#include

#include

#include

charS[1000],T[100];

intnext[100];

intlens,lent;

voidmakenext()

{

inti,j;

next[0]=-1;

i=0;j=-1;

while(i

{

if(j==-1||T[i]==T[j])

{

i++;

j++;

next[i]=j;

}

else

j=next[j];

}

}

intgetIndex()

{

inti,j;

i=0;

j=0;

while(i

{

if(j==-1||S[i]==T[j])

{

i++;

j++;

}

else

{

j=next[j];

}

}

if(j==lent)

{

returni-j;

}

else

return-1;

}

intmain()

{

inti,j,k;

gets(S);

gets(T);

lens=strlen(S);

lent=strlen(T);

makenext();

k=getIndex();

printf("%d\n",k);

system("pause");

return0;

}

Native

#include

#include

#include

charS[1000];

charT[100];

intlens,lent;

intgetIndex()

{

inti,j,k;

i=0;

j=0;

while(i

{

if(S[i]==T[j])

{

i++;

j++;

}

else

{

i=i-j+1;

j=0;

}

}

if(j==lent)

{

returni-j;

}

else

return-1;

}

intmain()

{

inti,j,k;

gets(S);

gets(T);

lens=strlen(S);

lent=strlen(T);

k=getIndex();

printf("%d",k);

system("pause");

return0;

}

creatTree

#include

#include

#include

//A(B(D,E(F(J,),G)),C(,H))

structTnode

{

chardata;

structTnode*lchild,*rchild;

};

charA[2000];

intlen;

structTnode*root=0;

charstack[100];

inttop=-1;

voidcreateTree(structTnode*&p,intl,intr)

{

inti,j,k;

if(l>r)

{

return;

}

p=(structTnode*)malloc(sizeof(structTnode));

p->data=A[l];

p->lchild=0;

p->rchild=0;

if(l

{

if(A[l+2]==',')

{

createTree(p->rchild,l+3,r-1);

return;

}

elseif(A[r-1]==',')

{

createTree(p->lchild,l+2,r-2);

return;

}

elseif(A[l+3]==',')

{

createTree(p->lchild,l+2,l+2);

createTree(p->rchild,l+4,r-1);

return;

}

stack[++top]=A[l+3];

i=l+4;

while(i

{

if(A[i]=='(')

{

stack[++top]=A[i];

}

elseif(A[i]==')')

{

top--;

if(top<0)

break;

}

i++;

}

createTree(p->lchild,l+2,i);

createTree(p->rchild,i+2,r-1);

}

}

voidpreorder(structTnode*p)

{

printf("%c",p->data);

if(p->lchild)

{

preorder(p->lchild);

}

if(p->rchild)

{

preorder(p->rchild);

}

}

voidbfs(structTnode*p)

{

structTnode*que[1000];

structTnode*q;

intfront=0,rear=0;

que[rear++]=p;

while(front!

=rear)

{

q=que[front++];

printf("%c",q->data);

if(q->lchild)

{

que[rear++]=q->lchild;

}

if(q->rchild)

{

que[rear++]=q->rchild;

}

}

}

structTnode*stack_i[100];

inttop_i=-1;

voidgofarleft(structTnode*p)

{

structTnode*q=p;

while(q->lchild)

{

stack_i[++top_i]=q;

q=q->lchild;

}

stack_i[++top_i]=q;

}

voidinorder(structTnode*p)

{

structTnode*q;

gofarleft(p);

while(top_i>=0)

{

q=stack_i[top_i--];

printf("%c",q->data);

if(q->rchild)

{

gofarleft(q->rchil

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1