天下贰登陆音乐叫什么:紧急!!真心向各位数据结构高手求教

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 21:02:07
麻烦各位高手能帮帮小弟解读一下下面的程序,最好是能解释每个语句的意思,谢谢了!

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define POW 1
#define MUL 2
#define DIV 3
#define ADD 4
#define SUB 5
#define Lp 6
#define Rp 7
#define END 8
#define Epsilon 1e-7

typedef double NODE;
struct
{
char op;
int code;
}
opchTbl[]={{'*',2},{'/',3},{'+',4},{'-',5},{'(',6},{')',7},{'^',1},{'\n',8},{' ',-1}};

typedef struct node
{
NODE data;
struct node *link;
}
LNODE;
LNODE *optop , *numtop;

NONO(char *t,double sum)
{
int i;
char str[15];
FILE *wf;
wf=fopen("62346_4.dat","w");
for(i=0;i<62;i++)
{
if(*(t+i)!=0 && *(t+i)!=10 && *(t+i)!=13)
fprintf(wf,"%c",*(t+i));
}
sprintf(str, "=%f\n",sum);
for(i=0;i<14;i++)
if((int)str[i]!=48 && (int)str[i]!=15)
fprintf(wf,"%c",str[i]);
fclose(wf);
}

void l_push(NODE x,LNODE * *toppt)
{
LNODE *p=(LNODE *)malloc(sizeof(LNODE));
p->data=x;
p->link= *toppt;
*toppt=p;
}

int l_pop(NODE *cp,LNODE * *toppt)
{
LNODE *p= *toppt;
if(*toppt==NULL)return 1;
*cp=p->data;
*toppt=p->link;
free(p);
return 0;
}

int osp[]={5,3,3,2,2,5,1,1},
isp[]={4,3,3,2,2,0};

void synError()
{
double temp;
printf("syntax err!");
while(optop!=NULL) l_pop(&temp,&optop);
while(numtop!=NULL) l_pop(&temp,&numtop);
exit(0);
}

double eval(char tag,double left, double right)
{
int n;
double result;
switch(tag)
{
case POW : for(n=1,result=left;n<right; n++)
result *=left;
return result;
case ADD : return left+right;
case SUB : return left-right;
case MUL : return left*right;
case DIV : if(fabs(right)<=Epsilon)
{
printf("/0 err\n");
exit(1);
}
return left/right;
}
printf("expression err!/n");return 1.0;
}
int c=' ',k=0;
int t[61];

#define RADIX 10

int getToken(double *nump)
{
double dradix,num;
int i;
while(c==' '|| c=='\t')c=getchar();
if(c<'0' || c>'9')
{
for(i=0;opchTbl[i].code!=-1 && opchTbl[i].op!=c;i++);
if(opchTbl[i].code==-1)synError();
if(c!='\n')c=getchar();
t[k]=c;
k++;
return opchTbl[i].code;
}
num=0;
while(c>='0'&&c<='9')
{
num=RADIX *num+c-'0';
c=getchar();
t[k]=c;
k++;
}
if(c=='.')
{
dradix=1.0/RADIX;
c=getchar();
t[k]=c;
k++;
while(c>='0'&&c<='9')
{
num=num+(c-'0')*dradix;
dradix/=RADIX;
c=getchar();
t[k]=c;
k++;
}
}
*nump=num;
return 0;
}
void main()
{
double num , dop, operand1,operand2,res;
int type,i;
char ans,op;
do
{
printf("input syntax!\n");
optop=numtop=NULL;
l_push(Lp,&optop);
do
{
c=getchar();
t[k]=c;
k++;
}
while(c==' ' || c=='\n' || c=='\t');
while(1)
{
type=getToken(&num);
if(type==0)l_push(num,&numtop);
else
{ if(osp[type-1]>isp[(int)optop->data-1])
l_push((double)type,&optop);
else{while(osp[type-1]<=isp[(int)optop->data-1]&& optop->data<=5)
{if(l_pop(&dop,&optop))
synError();
op=(char)dop;
if(l_pop(&operand2,&numtop))
synError();
.....

这段代码是用来求一个简单数学表达式的值,node组成一个自定义的堆栈,通过比较运算符的优先级决定计算顺序,将较低优先级的运算压入堆栈中

你想问什么问题?
只见一篇代码!!!

这样该是你打得一个算法吧。够辛苦了。