几道与数据结构有关的编程题目

时间:2013-05-29 12:05:59
【文件属性】:
文件名称:几道与数据结构有关的编程题目
文件大小:33KB
文件格式:DOC
更新时间:2013-05-29 12:05:59
数据结构 表达式求值答案 仅作参考 #include #include #include int kuohaopipei(char a[]) { int j,k=0; for(j=0;a[j]!='\0';j++) { if(a[j]=='(') k++; if(a[j]==')') k--; } if(k==0) return 1; else return 0; } int isoptr(char a) { int i; char b[]="+-*/()#"; for(i=0;b[i]!=a;i++); return i; } char youxianji(char a,char b) { char c[8][8]= { ">><<<>>", ">><<<>>", ">>>><>>", ">>>><>>", "<<<<<=0", ">>>>0>>", "<<<<<0=" }; return c[isoptr(a)][isoptr(b)]; } int caculate(char *a) { int i=0,base; char *base1,*base2,*top1,*top2; int *base3,*top3; char b; base1=(char*)malloc(100*sizeof(char)); base2=(char*)malloc(100*sizeof(char)); base3=(int*)malloc(100*sizeof(int)); top1=base1;top2=base2;top3=base3; *base1='#';top1++; a[strlen(a)]='#'; while(1) { if(a[i]=='#'&&*(top1-1)=='#') break; if(a[i]>='0'&&a[i]<='9') { *top2=a[i];top2++;i++; } else { base=1;*top3=0; while(top2!=base2) { *top3+=(*(top2-1)-'0')*base; base*=10;top2--; } top3++; b=youxianji(*(top1-1),a[i]); if(b=='<') {*top1=a[i];i++;top1++;} if(b=='='){top1--;i++;} if(b=='>') { if(*(top1-1)=='+'){*(top3-2)=(*(top3-2))+(*(top3-1));top3--;top1--;} if(*(top1-1)=='-'){*(top3-2)=(*(top3-2))-(*(top3-1));top3--;top1--;} if(*(top1-1)=='*'){*(top3-2)=(*(top3-2))*(*(top3-1));top3--;top1--;} if(*(top1-1)=='/'){*(top3-2)=(*(top3-2))/(*(top3-1));top3--;top1--;} } } } return *(top3-1); } int main() { char a[100]; int i,n,m; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s",a); m=kuohaopipei(a); if(m==0) printf("error\n"); else printf("%d\n",caculate(a)); } system("pause"); return 0; }

网友评论