| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 494 人关注过本帖
标题:编译没问题,但无法运行!
只看楼主 加入收藏
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:4 
编译没问题,但无法运行!
图片附件: 游客没有浏览图片的权限,请 登录注册
我写了代码后,编译后运行时出现这样的情况,求高手指点!

[ 本帖最后由 烧包谷 于 2010-12-5 23:04 编辑 ]
搜索更多相关主题的帖子: 编译 运行 
2010-12-05 23:00
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
把代码贴出来
2010-12-05 23:02
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
收藏
得分:0 
回复 2楼 寒风中的细雨
程序代码:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int y,z;
char theta;
typedef struct{
    int *base;
    int *top;
    int stacksize;
}Sqstack1;
typedef struct S2{
    char *base;
    char *top;
    int stacksize;
}Sqstack2;
void Initstack1(Sqstack1 *S)
{
    S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
    if(!S->base)printf("内存分配错误");
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;
}
void Initstack2(Sqstack2 *Q)
{
    Q->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
    if(!Q->base)printf("内存分配错误");
        Q->top=Q->base;
    Q->stacksize=STACK_INIT_SIZE;
}
void Push1(Sqstack1 *S,int e)
{
    if(S->top-S->base>=S->stacksize)
    {
        S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
        if(!S->base)printf("从新分配内存错误");
        S->top=S->base+S->stacksize;
    }
    *S->top++=e;
}
void Push2(Sqstack2 *Q,char e)
{
    if(Q->top-Q->base>=Q->stacksize)
    {
        Q->base=(char *)realloc(Q->base,(Q->stacksize+STACKINCREMENT)*sizeof(char));
        if(!Q->base)printf("从新分配内存错误");
        Q->top=Q->base+Q->stacksize;
    }
    *Q->top++=e;
}
int Gettop1(Sqstack1 *S)
{
    int e;
    if(S->top=S->base)printf("栈已经为空,无法执行!");
    e=*(S->top-1);
    return e;
}
char Gettop2(Sqstack2 *Q)
{
    char c;
    if(Q->top=Q->base)printf("栈已经为空,无法执行!");
    c=*(Q->top-1);
    return c;
}
int Pop1(Sqstack1 *S)
{
    int e;
    if(S->top==S->base)printf("ERROR\n");
    e=*--S->top;
    return e;
}
char Pop2(Sqstack2 *Q)
{
    char c;
    if(Q->top==Q->base)printf("ERROR\n");
        c=*--Q->top;
    return c;
}
char Precede(char a1,char a2)
{
    if((a1=='+'||a1=='-')&&(a2=='+'||a2=='-'||a2==')'||a2=='#'))return '>';
    else if((a1=='+'||a1=='-')&&(a2=='*'||a2=='/'||a2=='('||a2=='#'))return '<';
    else if((a1=='*'||a1=='/')&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))return '>';
    else if((a1=='*'||a1=='/')&&a2=='(')return '<';
    else if(a1=='('&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2=='('))return '>';
    else if(a1=='('&&a2==')')return '=';
    else if(a1==')'&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))return '>';
    else if(a1=='#'&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2=='('))return '<';
    else if(a1=='#'&&a2=='#')return '=';
    else return(0);
}
Operate(int n,char c,int m)
{
    if(c=='+')return(n+m);
    else if(c=='-')return(n-m);
    else if(c=='*')return(n*m);
    else if(c=='/')return(n/m);
    else return(0);
}
void main()
{
    Sqstack1 OPND;
    Sqstack2 OPTR;
    int a,t=1,add=0,k=0;
    Initstack1(&OPND);
    Push2(&OPTR,'#');
    Initstack2(&OPTR);
    char c;
    c=getchar();
    while(c!='#'||Gettop2(&OPTR)!='#')
    {
        while(c>='0'&&c<='9')
        { a=c-48;
          add=add+a*t;
          t=t*10;
          k=1;
           c=getchar();
        };
        if(k==1)  Push1(&OPND,add);
        else
            switch(Precede(Gettop2(&OPTR),c)){
            case '<'://栈顶元素优先权低
                Push2(&OPTR,c);
                c=getchar();break;
            case '='://脱括号并接受下一字符
                Pop2(&OPTR);
                c=getchar();break;
            case '>'://退栈并将运算结果入栈
                theta=Pop2(&OPTR);
                y=Pop1(&OPND);z=Pop1(&OPND);
                Push1(&OPND,Operate(y,theta,z));break;
            default:break;
        }
        printf("***");
    }
    printf("\n结果是:%d",Gettop1(&OPND));
}




    





     











   






     











   






     











   


[ 本帖最后由 烧包谷 于 2010-12-5 23:14 编辑 ]
2010-12-05 23:07
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int y,z;
char theta;
typedef struct
{
    int *base;
    int *top;
    int stacksize;
}Sqstack;

void Initstack(Sqstack *S)
{
    S->base = (int *)malloc(STACK_INIT_SIZE*sizeof(int));
    if(!S->base)
        printf("内存分配错误");
    S->top = S->base;
    S->stacksize = STACK_INIT_SIZE;
}

void Push(Sqstack *S,int e)
{
    if(S->top-S->base >= S->stacksize)
    {
        S->base = (int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
        if(!S->base)
            printf("从新分配内存错误");
        S->top = S->base+S->stacksize;
    }
    *S->top++ = e;
}

int Gettop(Sqstack *S)
{
    if(S->top == S->base)
        printf("栈已经为空,无法执行!");

    return *(S->top-1);
   
}

int Pop(Sqstack *S)
{
    if(S->top==S->base)
        printf("ERROR\n");
    return *--S->top;
   
}
/*
*若为空则返回1 否则返回0
*/
int Isempty(Sqstack s)
{
    if( s.top == s.base )
        return 1;
    return 0;
}

char Precede(char a1,char a2)//a1 in, a2 out
{
    if( (a1=='+'||a1=='-') && (a2=='+'||a2=='-'||a2==')' || a2=='#' ))
        return '>';//pop
    else if( (a1=='+'||a1=='-') && (a2=='*'||a2=='/'||a2=='('))
        return '<';//push
    else if((a1=='*'||a1=='/')&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))
        return '>';//pop
    else if((a1=='*'||a1=='/') && a2=='(')
        return '<';
   else if(a1=='('&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'))
        return '<';
    else if(a1=='('&&a2==')')
        return '=';
//    else if(a1==')'&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))
//        return '>';
    else if(a1=='#'&& (a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2=='(') )
        return '<';
    else if(a1=='#' && a2=='#')
        return '=';
    else
        return(0);
}
int Operate(int n,char c,int m)
{
    if(c=='+') return(n+m);
    else if(c=='-') return(n-m);
    else if(c=='*') return(n*m);
    else if(c=='/') return(n/m);
    else return(0);
}
void main()
{
    Sqstack OPND, OPTR;
    int a = 0;
    Initstack(&OPND);//operand stack

    Initstack(&OPTR);//operator stack
    Push(&OPTR,'#');

    char c;
    c = getchar();
//    while(c!='#'||Gettop(&OPTR)!='#')
    while ( !Isempty(OPTR) )
    {
        a = 0;//initialize variable a
        while(c>='0' && c<='9')
        {
            a = a*10 + c - '0';
            c = getchar();
        };
        if( 0 != a )
            Push(&OPND,a);
        else
            switch(Precede(Gettop(&OPTR),c))
            {
            case '<'://栈顶元素优先权低
                Push(&OPTR,c);
                c=getchar();break;
            case '='://脱括号并接受下一字符
                Pop(&OPTR);
                c=getchar();
                break;
            case '>'://退栈并将运算结果入栈
                theta = Pop(&OPTR);
                //y=Pop(&OPND);z=Pop(&OPND);
                z = Pop(&OPND); y = Pop(&OPND);
                Push(&OPND,Operate(y,theta,z));
                break;
            default:break;
            }
    }
    printf("\n结果是:%d",Gettop(&OPND));
}

2010-12-06 12:14
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
收藏
得分:0 
回复 4楼 寒风中的细雨
谢谢啦!
2010-12-06 14:01
快速回复:编译没问题,但无法运行!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041603 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved