小弟我想请问下.小弟我这段程序是不是堆栈有点有关问题呢
我想请教下..我这段程序是不是堆栈有点问题呢
#include "stdio.h"
#define Stacklen 500 // 栈的最大长度
int Empty_SeqStack(SeqStack *s)
{
if (s->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(SeqStack *s, int x)
{
if (s->top==Stacklen)
return 0; /*栈满不能入栈*/
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Top_SeqStack(SeqStack *s)
{
if ( Empty_SeqStack(s))
return 0; /*栈空*/
else
return (s->data[s->top] );
}
int Pop_SeqStack(SeqStack *s, int *x)
{
if(Empty_SeqStack(s) )
return 0; /* 栈空不能出栈 */
else
{
*x=s->data[s->top];
s->top--;
return 1;
} /*栈顶元素存入*x,返回*/
}
double AreaCalc(int *npe,int nwh[500][3], double whxy[][4],int n)
{
int i,j,k;
double mina; // 两个模块不同方向组合下的最小面积
double area[4]; // 两个模块不同方向组合时4种可能的面积
int x; // 从正则表达式中依次取出的每个元素
//double whmax,whmin,w1,w2,h1,h2; // whmax:
double w1,w2,h1,h2;
double w1min,w1max,h1min,h1max; // 从堆栈中弹出的两个模块的长宽参数
double w2min,w2max,h2min,h2max;
//double A1,A2;
double W,H; // 布局后得到总的宽度和高度
int n1,n2; // 从堆栈中弹出的两个模块的编号
SeqStack *number; // 用来保存模块编号栈
// double result; // 返回布局后的总面积(对最小面积归一化)
int op[600][4]; // 定义一个数组,第一、二行分别保存两个参与运算模块的编号
// 第三行保存运算符(*/+),第四行存运算后得到的中间模块编号
int nn; // 运算产生的中间模块编号,从g->n开始
double minarea; // 测试电路的各个模块的面积和
int blocknum,npe_len; // 保持测试电路模块数,正则表达式的长度
blocknum=n;
npe_len=2*blocknum-1;
//minarea=g->totalarea;
number=malloc(sizeof(SeqStack)); // 开辟保存模块编号的栈
//number->data=(int *) malloc(blocknum*sizeof(int));
// number->top=-1;
k=0; // 二维数组op 的序号,代表第几列
nn=n-1; // 中间运算产生临时模块的起始编号
for(i=0;i<npe_len;i++) // 遍历整个正则表达式
{
x=npe[i];
if(x>=0) // 取出的不是运算符,将取出的模块编号、宽度和高度分别压入3个堆栈
{
Push_SeqStack (number,x);
}
else // 取出的是运算符,提取模块编号
{
nn=nn+1;
Pop_SeqStack(number,&n2);
Pop_SeqStack(number,&n1);
op[k][0]=n1;op[k][1]=n2; // 填二维数组op
op[k][2]=x; op[k][3]=nn;
k++;
Push_SeqStack(number,nn); // 压入将产生的新模块编号
}
w1=nwh[n1][1]; // 硬模块1的宽度
h1=nwh[n1][2]; // 硬模块1的高度
w2=nwh[n2][1]; // 硬模块2的宽度
h2=nwh[n2][2]; // 硬模块2的高度
area[0]=(w1>w2?w1:w2)*(h1+h2);
area[1]=(w1>h2?w1:h2)*(h1+w2);
area[2]=(h1>w2?h1:w2)*(h2+w1);
area[3]=(h1>h2?h1:h2)*(w1+w2);
mina=area[0];
for (j=0;j<=3;j++) //求出面积的最小值
{
if(area[j]<mina)
mina=area[j];
}
if(mina==area[0])
{
if(x==-1) // 取出的是"*"运算符, 两个模块左右相邻
{
whxy[n1][0]=nmh[n1][2]; // 模块n1转90度
whxy[n1][1]=nmh[n1][1];
whxy[n2][0]=nmh[n2][2]; // 模块n2转90度
whxy[n2][1]=nmh[n2][1];
whxy[nn][0]=h1+h2; // 新产生模块的宽度
if(w1>w2) // 确定新产生模块的高度
{
whxy[nn][1]=w1;
#include "stdio.h"
#define Stacklen 500 // 栈的最大长度
int Empty_SeqStack(SeqStack *s)
{
if (s->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(SeqStack *s, int x)
{
if (s->top==Stacklen)
return 0; /*栈满不能入栈*/
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Top_SeqStack(SeqStack *s)
{
if ( Empty_SeqStack(s))
return 0; /*栈空*/
else
return (s->data[s->top] );
}
int Pop_SeqStack(SeqStack *s, int *x)
{
if(Empty_SeqStack(s) )
return 0; /* 栈空不能出栈 */
else
{
*x=s->data[s->top];
s->top--;
return 1;
} /*栈顶元素存入*x,返回*/
}
double AreaCalc(int *npe,int nwh[500][3], double whxy[][4],int n)
{
int i,j,k;
double mina; // 两个模块不同方向组合下的最小面积
double area[4]; // 两个模块不同方向组合时4种可能的面积
int x; // 从正则表达式中依次取出的每个元素
//double whmax,whmin,w1,w2,h1,h2; // whmax:
double w1,w2,h1,h2;
double w1min,w1max,h1min,h1max; // 从堆栈中弹出的两个模块的长宽参数
double w2min,w2max,h2min,h2max;
//double A1,A2;
double W,H; // 布局后得到总的宽度和高度
int n1,n2; // 从堆栈中弹出的两个模块的编号
SeqStack *number; // 用来保存模块编号栈
// double result; // 返回布局后的总面积(对最小面积归一化)
int op[600][4]; // 定义一个数组,第一、二行分别保存两个参与运算模块的编号
// 第三行保存运算符(*/+),第四行存运算后得到的中间模块编号
int nn; // 运算产生的中间模块编号,从g->n开始
double minarea; // 测试电路的各个模块的面积和
int blocknum,npe_len; // 保持测试电路模块数,正则表达式的长度
blocknum=n;
npe_len=2*blocknum-1;
//minarea=g->totalarea;
number=malloc(sizeof(SeqStack)); // 开辟保存模块编号的栈
//number->data=(int *) malloc(blocknum*sizeof(int));
// number->top=-1;
k=0; // 二维数组op 的序号,代表第几列
nn=n-1; // 中间运算产生临时模块的起始编号
for(i=0;i<npe_len;i++) // 遍历整个正则表达式
{
x=npe[i];
if(x>=0) // 取出的不是运算符,将取出的模块编号、宽度和高度分别压入3个堆栈
{
Push_SeqStack (number,x);
}
else // 取出的是运算符,提取模块编号
{
nn=nn+1;
Pop_SeqStack(number,&n2);
Pop_SeqStack(number,&n1);
op[k][0]=n1;op[k][1]=n2; // 填二维数组op
op[k][2]=x; op[k][3]=nn;
k++;
Push_SeqStack(number,nn); // 压入将产生的新模块编号
}
w1=nwh[n1][1]; // 硬模块1的宽度
h1=nwh[n1][2]; // 硬模块1的高度
w2=nwh[n2][1]; // 硬模块2的宽度
h2=nwh[n2][2]; // 硬模块2的高度
area[0]=(w1>w2?w1:w2)*(h1+h2);
area[1]=(w1>h2?w1:h2)*(h1+w2);
area[2]=(h1>w2?h1:w2)*(h2+w1);
area[3]=(h1>h2?h1:h2)*(w1+w2);
mina=area[0];
for (j=0;j<=3;j++) //求出面积的最小值
{
if(area[j]<mina)
mina=area[j];
}
if(mina==area[0])
{
if(x==-1) // 取出的是"*"运算符, 两个模块左右相邻
{
whxy[n1][0]=nmh[n1][2]; // 模块n1转90度
whxy[n1][1]=nmh[n1][1];
whxy[n2][0]=nmh[n2][2]; // 模块n2转90度
whxy[n2][1]=nmh[n2][1];
whxy[nn][0]=h1+h2; // 新产生模块的宽度
if(w1>w2) // 确定新产生模块的高度
{
whxy[nn][1]=w1;