工作确定后做的第一题,以后如果有心情做的话,一定以博客的形式保存一下
题目地址:http://poj.org/problem?id=1472
一个mini型的类pascal语言解释器,用递归下降的方法,练习了下递归的使用,做题的过程是写两行代码就去刷两下论坛,然后再写两行代码,再去看下新闻,如此反复...发现自己的心已经散了,没有了以前的那咱专注,我要去实习了,这样下去不行了...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int res[11];
char c;
void skipspace()
{
if(c==' '||c=='/n'||c==0)
{
scanf("%c",&c);
}
while(c==' '||c=='/n')
{
scanf("%c",&c);
}
}
void Statementlist(int m,int n)
{
skipspace();
while(c=='L'||c=='O')
{
if(c=='L')
{
char t;
scanf("OOP %c",&t);
c=0;
if(t=='n')
Statementlist(m,n+1);
else
{
int num;
ungetc(t,stdin);
scanf("%d",&num);
Statementlist(m*num,n);
}
skipspace();
if(c=='E')
{
scanf("ND");
c=0;
}
} else if(c=='O')
{
int t;
scanf("P %d",&t);
c=0;
res[n]+=m*t;
}
skipspace();
}
}
void Program()
{
skipspace();
if(c=='B')
{
scanf("EGIN");
c=0;
}
skipspace();
Statementlist(1,0);
skipspace();
if(c=='E')
{
scanf("ND");
c=0;
}
}
int main()
{
// freopen("data.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j,f,f1;
for(i=1;i<=n;i++)
{
f=1;
f1=1;
c=0;
memset(res,0,sizeof(res));
printf("Program #%d/nRuntime = ",i);
Program();
for(j=10;j>0;j--)
{
if(res[j]!=0)
{
f1=0;
if(f)
{
res[j]==1?(j==1?printf("n"):printf("n^%d",j)):(j==1?printf("%d*n",res[j]):printf("%d*n^%d",res[j],j));
}
else
{
res[j]==1?(j==1?printf("+n"):printf("+n^%d",j)):(j==1?printf("+%d*n",res[j]):printf("+%d*n^%d",res[j],j));
}
f=0;
}
}
if(res[0]!=0)
{
f1=0;
if(f) printf("%d",res[0]);
else printf("+%d",res[0]);
f=0;
}
if(f1) printf("0");
printf("/n/n");
}
return 0;
}