| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 475 人关注过本帖
标题:运行结果不正确!查不出问题出在那!!求解答
取消只看楼主 加入收藏
付新忠
Rank: 1
等 级:新手上路
帖 子:20
专家分:8
注 册:2012-12-1
结帖率:80%
收藏
已结贴  问题点数:5 回复次数:0 
运行结果不正确!查不出问题出在那!!求解答
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR -1
typedef float status;

typedef struct
{
    char *base, *top;
    int stacksize;
}optrstack;

typedef struct
{
    float *base, *top;
    int stacksize;
}opndstack;

int optrinitstack(optrstack *s)
{
    s->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
    if (!s->base)
        exit(0);
    s->top = s->base;
    s->stacksize = STACK_INIT_SIZE;
    return 1;
}

int opndinitstack(opndstack *s)
{
    s->base = (float *)malloc(STACK_INIT_SIZE * sizeof(float));

    if (!s->base)
        exit(0);
    s->top = s->base;
    s->stacksize = STACK_INIT_SIZE;
    return 1;
}

int opndpush(opndstack *s, float e)
{
    if (s->top - s->base >= s->stacksize)
    {
        s->base = (float *)realloc(s->base, (s->stacksize + STACKINCREMENT)*sizeof(float));
        if (!s->base)exit(0);
        s->top = s->base + s->stacksize;
        s->stacksize += STACKINCREMENT;
    }
    *(s->top++) = e;
    return 1;
}

int optrpush(optrstack *s, char e)
{
    if (s->top - s->base >= s->stacksize)
    {
        s->base = (char *)realloc(s->base, (s->stacksize + STACKINCREMENT)*sizeof(char));
        if (!s->base)exit(0);
        s->top = s->base + s->stacksize;
        s->stacksize += STACKINCREMENT;
    }
    *(s->top++) = e;
    return 1;
}

int opndpop(opndstack *s, float *e)
{
    if (s->top == s->base)return 0;
    *e = *(--s->top);//加一个*号
    return 1;
}

int optrpop(optrstack *s, char *e)
{
    if (s->top == s->base) return ERROR;//ERROR未定义,我帮你宏定义了一个-1
    *e = *--s->top;//加一个*号
    return 1;
}

char optrgettop(optrstack s)
{
    char e;
    if (s.top == s.base)return 0;
    e = *(s.top - 1);
    return e;
}

float opndgettop(opndstack s)
{
    float e;
    if (*s.top = *s.base)return 0;
    e = *(s.top - 1);
    return e;
}

/*char Precede(char a, char b)  //判断运算符优先级
{
    int i, j;
    char Table[8][8] = {
        { ' ', '+', '-', '*', '/', '(', ')', '#'},
        { '+', '>', '>', '<', '<', '<', '>', '<'},
        { '-', '>', '>', '<', '<', '<', '>', '<'},
        { '*', '>', '>', '>', '>', '<', '>', '<'},
        { '/', '>', '>', '>', '>', '<', '>', '<'},
        { '(', '<', '<', '<', '<', '<', '=', '<'},
        { ')', '>', '>', '>', '>', '=', '>', '<'},
        { '#', '>', '>', '>', '>', '>', '>', '>'}
         
    };  //优先级表格
    for (i = 0; i < 7; i++)
    if (Table[0][i] == a)  //纵坐标寻找
        break;
    for (j = 0; j < 7; j++)  //横坐标寻找
    if (Table[j][0] == b)
        break;
    return Table[j][i];
}*/

 char Precede(char c1,char c2) {
    if(c1 = '(') {//compare (
        if(c2 == ')')return '=';
        else return '<';
    }
    else if(c1 == '#') {//compare #
        if(c2 != c1) {
            return '<';
        }
        else return '=';
    }
    else if(c1 == '*' || c1 == '/' ) {//compare */
        if(c2 == '(') {
            return '<';
        }
        else return '>';
    }
    else if(c1 == '+'||c1 == '-') {//compare + -
        if(c2 == '('|| c2 == '*' || c2 =='/') {
            return '<';
        }
        else
            return '>';
        }
    return ERROR;
   
}



float opreate(float a, char theta, float b)  //计算二元表达式的值
{
    float c;
    if (theta == '+')
        c = a + b;
    else if (theta == '-')
        c = a - b;
    else if (theta == '*')
        c = a*b;
    else
        c = a / b;
    return c;
}


float In(char ch)  //判断字符ch是否为运算符
{
    char ptr[10] = { '+', '-', '*', '/', '(', ')', '=' };
    int i;
    for (i = 0; i < 7; i++)
    {
        if (ch == ptr[i])
            return 1;
    }
    return 0;
}

float T()//取值符啊,因为你用的是指针啊!
{
    char x, theta, c;
    float a, b;
    optrstack optr;
    opndstack opnd;
    optrinitstack(&optr);//取值符
    optrpush(&optr, '#');//# -> '#' 和取值符
    opndinitstack(&opnd);//取值符
    c = getchar();
    while (c != '#' || optrgettop(optr) != '#')
    {
        if (!In(c)){ opndpush(&opnd, c); c = getchar(); }//取值符
        else
            switch (Precede(optrgettop(optr), c))//大写P
        {
            case'<':optrpush(&optr, c);//取值符
                c = getchar();
                break;
            case'=':
                optrpop(&optr, &x);//取值符
                c = getchar();
                break;
            case'>':
                optrpop(&optr, &theta);//取值符
                opndpop(&opnd, &b);//取值符
                opndpop(&opnd, &a);//取值符
                opndpush(&opnd, opreate(a, theta, b));//取值符
                break;
        }//switch
    }//while
    return opndgettop(opnd);
}
void main()
{
    float a;
    a = T();
    printf("%f\n", a);
}
搜索更多相关主题的帖子: include status 
2013-11-14 22:44
快速回复:运行结果不正确!查不出问题出在那!!求解答
数据加载中...
 
   



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

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