读书人

小弟我想请问下.小弟我这段程序是不是

发布时间: 2012-04-21 14:34:44 作者: rapoo

我想请教下..我这段程序是不是堆栈有点问题呢
#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;


}
else
{
whxy[nn][1]=w2;
}
}
else //取出的是"+"运算符, 两个模块上下相邻
{
whxy[n1][0]=nmh[n1][1]; // 模块n1不转
whxy[n1][1]=nmh[n1][2];
whxy[n2][0]=nmh[n2][1]; // 模块n2不转
whxy[n2][1]=nmh[n2][2];
whxy[nn][1]=h1+h2; // 新产生模块的高度
if(w1>w2) // 确定新产生模块的宽度
{
whxy[nn][0]=w1;
}
else
{
whxy[nn][0]=w2;
}
}
}
else if(mina==area[1])
{
if(x==-1) //取出的是"*"运算符, 两个模块左右相邻
{
whxy[n1][0]=nmh[n1][2]; // 模块n1转90度
whxy[n1][1]=nmh[n1][1];
whxy[n2][0]=nmh[n2][1]; // 模块n2不转
whxy[n2][1]=nmh[n2][2];
whxy[nn][0]=h1+w2; // 新产生模块的宽度
if(w1>h2)
{
whxy[nn][1]=w1; // 新产生模块的高度
}
else
{
whxy[nn][1]=h2; // 新产生模块的高度
}
}
else //取出的是"+"运算符, 两个模块上下相邻
{
whxy[n1][0]=nmh[n1][1]; // 模块n1不转
whxy[n1][1]=nmh[n1][2];
whxy[n2][0]=nmh[n2][2]; // 模块n2转90度
whxy[n2][1]=nmh[n2][1];
whxy[nn][1]=h1+w2; // 新产生模块的高度
if(w1>h2)
{
whxy[nn][0]=w1; // 新产生模块的宽度
}
else
{
whxy[nn][0]=h2; // 新产生模块的宽度
}
}
}
else if(mina==area[2])
{
if(x==-1) // 取出的是"*"运算符, 两个模块左右相邻
{
whxy[n1][0]=nmh[n1][1]; // 模块n1不转
whxy[n1][1]=nmh[n1][2];
whxy[n2][0]=nmh[n2][2]; // 模块n2转90度
whxy[n2][1]=nmh[n2][1];
whxy[nn][0]=w1+h2; // 新产生模块的宽度
if(w2>h1)
{
whxy[nn][1]=w2; // 新产生模块的高度
}
else
{
whxy[nn][1]=h1; // 新产生模块的高度
}
}
else //取出的是"+"运算符, 两个模块上下相邻
{
whxy[n1][0]=nmh[n1][2]; // 模块n1转90度
whxy[n1][1]=nmh[n1][1];
whxy[n2][0]=nmh[n2][1]; // 模块n2不转
whxy[n2][1]=nmh[n2][2];
whxy[nn][1]=w1+h2; // 新产生模块的高度
if(w2>h1)
{
whxy[nn][0]=w2; // 新产生模块的宽度
}
else
{
whxy[nn][0]=h1; // 新产生模块的宽度
}
}
}
else // (mina==a[3])
{
if(x==-1) //取出的是"*"运算符, 两个模块左右相邻
{
whxy[n1][0]=nmh[n1][1]; // 模块n1不转
whxy[n1][1]=nmh[n1][2];
whxy[n2][0]=nmh[n2][1]; // 模块n2不转
whxy[n2][1]=nmh[n2][2];
whxy[nn][0]=w1+w2; // 新产生模块的宽度
if(h1>h2)
{
whxy[nn][1]=h1; // 新产生模块的高度
}
else
{
whxy[nn][1]=h2; // 新产生模块的高度
}
}
else //取出的是"+"运算符, 两个模块上下相邻
{
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][1]=w1+w2; // 新产生模块的高度
if(h1>h2)
{
whxy[nn][0]=h1; // 新产生模块的宽度
}
else
{
whxy[nn][0]=h2; // 新产生模块的宽度
}
}
}






// if((fp1=fopen("D:\\My Documents\\MATLAB\\whxy.txt","w"))==NULL)
// {
// printf("cannot open whxy.txt file\n");
// exit(0);
// }

//printf("\n布局后的模块尺寸和坐标\n");
// for(i=0;i<npe_len;i++)
// {
// printf("%4d,",i);
// for (j=0;j<4;j++)
// {
// // fprintf(fp1,"%6d",whxy[i][j]);
// printf("%6d",whxy[i][j]);
// }
// //fprintf(fp1,"\n");
// printf("\n");
// }

printf("\n输出计算模块坐标的数组\n");
for(i=0;i<blocknum-1;i++) // 输出初始化的npe表达式
{
printf("%8d,%8d,%8d,%8d\n",op[i][0],op[i][1],op[i][2],op[i][3]);
}
//return result;
}

[解决办法]
代码残缺不全,没main函数。 nwh写出nmh。。。。
SeqStack没定义。 修正后能编译的版本如下:
(至于正确运行是什么样子的,我猜不到lz想做什么,所以只能修改到能编译的程度了。)

C/C++ code
#include "stdio.h"#include "stdlib.h"#define Stacklen 500 // 栈的最大长度typedef struct SeqStack{    int top;    int *data;}SeqStack;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=(SeqStack *)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]=nwh[n1][2]; // 模块n1转90度                whxy[n1][1]=nwh[n1][1];                whxy[n2][0]=nwh[n2][2]; // 模块n2转90度                whxy[n2][1]=nwh[n2][1];                whxy[nn][0]=h1+h2; // 新产生模块的宽度                if(w1>w2) // 确定新产生模块的高度                {                    whxy[nn][1]=w1;                }                else                {                    whxy[nn][1]=w2;                }            }            else //取出的是"+"运算符, 两个模块上下相邻            {                whxy[n1][0]=nwh[n1][1]; // 模块n1不转                whxy[n1][1]=nwh[n1][2];                whxy[n2][0]=nwh[n2][1]; // 模块n2不转                whxy[n2][1]=nwh[n2][2];                whxy[nn][1]=h1+h2; // 新产生模块的高度                if(w1>w2) // 确定新产生模块的宽度                {                    whxy[nn][0]=w1;                }                else                {                    whxy[nn][0]=w2;                }            }        }        else if(mina==area[1])        {            if(x==-1) //取出的是"*"运算符, 两个模块左右相邻            {                whxy[n1][0]=nwh[n1][2]; // 模块n1转90度                whxy[n1][1]=nwh[n1][1];                whxy[n2][0]=nwh[n2][1]; // 模块n2不转                whxy[n2][1]=nwh[n2][2];                whxy[nn][0]=h1+w2; // 新产生模块的宽度                if(w1>h2)                {                    whxy[nn][1]=w1; // 新产生模块的高度                }                else                {                    whxy[nn][1]=h2; // 新产生模块的高度                }            }            else //取出的是"+"运算符, 两个模块上下相邻            {                whxy[n1][0]=nwh[n1][1]; // 模块n1不转                whxy[n1][1]=nwh[n1][2];                whxy[n2][0]=nwh[n2][2]; // 模块n2转90度                whxy[n2][1]=nwh[n2][1];                whxy[nn][1]=h1+w2; // 新产生模块的高度                if(w1>h2)                {                    whxy[nn][0]=w1; // 新产生模块的宽度                }                else                {                    whxy[nn][0]=h2; // 新产生模块的宽度                }            }        }        else if(mina==area[2])        {            if(x==-1) // 取出的是"*"运算符, 两个模块左右相邻            {                whxy[n1][0]=nwh[n1][1]; // 模块n1不转                whxy[n1][1]=nwh[n1][2];                whxy[n2][0]=nwh[n2][2]; // 模块n2转90度                whxy[n2][1]=nwh[n2][1];                whxy[nn][0]=w1+h2; // 新产生模块的宽度                if(w2>h1)                {                    whxy[nn][1]=w2; // 新产生模块的高度                }                else                {                    whxy[nn][1]=h1; // 新产生模块的高度                }            }            else //取出的是"+"运算符, 两个模块上下相邻            {                whxy[n1][0]=nwh[n1][2]; // 模块n1转90度                whxy[n1][1]=nwh[n1][1];                whxy[n2][0]=nwh[n2][1]; // 模块n2不转                whxy[n2][1]=nwh[n2][2];                whxy[nn][1]=w1+h2; // 新产生模块的高度                if(w2>h1)                {                    whxy[nn][0]=w2; // 新产生模块的宽度                }                else                {                    whxy[nn][0]=h1; // 新产生模块的宽度                }            }        }        else // (mina==a[3])        {            if(x==-1) //取出的是"*"运算符, 两个模块左右相邻            {                whxy[n1][0]=nwh[n1][1]; // 模块n1不转                whxy[n1][1]=nwh[n1][2];                whxy[n2][0]=nwh[n2][1]; // 模块n2不转                whxy[n2][1]=nwh[n2][2];                whxy[nn][0]=w1+w2; // 新产生模块的宽度                if(h1>h2)                {                    whxy[nn][1]=h1; // 新产生模块的高度                }                else                {                    whxy[nn][1]=h2; // 新产生模块的高度                }            }            else //取出的是"+"运算符, 两个模块上下相邻            {                whxy[n1][0]=nwh[n1][2]; // 模块n1转90度                whxy[n1][1]=nwh[n1][1];                whxy[n2][0]=nwh[n2][2]; // 模块n2转90度                whxy[n2][1]=nwh[n2][1];                whxy[nn][1]=w1+w2; // 新产生模块的高度                if(h1>h2)                {                    whxy[nn][0]=h1; // 新产生模块的宽度                }                else                {                    whxy[nn][0]=h2; // 新产生模块的宽度                }            }        }// if((fp1=fopen("D:\\My Documents\\MATLAB\\whxy.txt","w"))==NULL)// {// printf("cannot open whxy.txt file\n");// exit(0);// }// printf("\n布局后的模块尺寸和坐标\n");// for(i=0;i<npe_len;i++)// {// printf("%4d,",i);// for (j=0;j<4;j++)// {// // fprintf(fp1,"%6d",whxy[i][j]);// printf("%6d",whxy[i][j]);// }// //fprintf(fp1,"\n");// printf("\n");// }        printf("\n输出计算模块坐标的数组\n");        for(i=0; i<blocknum-1; i++) // 输出初始化的npe表达式        {            printf("%8d,%8d,%8d,%8d\n",op[i][0],op[i][1],op[i][2],op[i][3]);        }//return result;    }}int main(){return 0;} 

读书人网 >C语言

热点推荐