鱼C论坛

 找回密码
 立即注册
查看: 2763|回复: 3

万能的论坛,请帮我解答这个问题吧

[复制链接]
发表于 2017-12-10 11:03:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pop函数定义那会报错,说是冲突类型(这是一个逆波兰计算器)
源代码如下:

#include <stdio.h>
#include <math.h>
#include <ctype.h>

#define MAXOP 100
#define NUMBER '0'
#define MAXVAL 100
#define BUFSIZE 100

char buf[BUFSIZE];
int bufp = 0;
int sp = 0;
double val[MAXVAL];

int main(int argc, char *argv[])
{
    char s[MAXOP];
    double op2;

    while(--argc > 0)
    {
        ungets(" ");
        ungets(*++argv);           //把一个空参数和一个参数写入缓冲区
        switch(getop(s))
        {
            case NUMBER:push(atof(s));
                        break;
            case '+':push(pop() + pop());
                     break;
            case '*':push(pop() * pop());
                     break;
            case '-':op2 = pop();
                     push(pop() - op2);
                     break;
            case '/':op2 = pop();
                     if(op2 == 0)
                     {
                         printf("除数不可为零\n");
                     }
                     else
                     {
                         push(pop() / op2);
                     }
                     break;
            default:printf("位置命令%s\n", s);
                    argc = 1;
                    break;
        }
    }

    printf("\t%.8g\n", pop());

    return 0;
}

void push(double f);
void push(double f)
{
    if(sp < MAXVAL)
    {
        val[sp++] = f;
    }
    else
    {
        printf("栈满,无法入栈%g\n", f);
    }
}

double pop(void);
double pop(void)
{
    if(sp > 0)
    {
        return val[--sp];
    }
    else
    {
        printf("栈为空\n");
        return 0;
    }
}

int getop(char s[]);
int getop(char s[])
{
    int i, c;

    while((s[0] = c = getch()) == ' ' || c == '\t')
    {
        ;
    }
    s[1] = '\0';
    if(!isdigit(c) && c != '.')
    {
        return c;
    }
    i = 0;
    if(isdigit(c))
    {
        while(isdigit(s[++i] = c = getch()))
        {
            ;
        }
    }
    if(c == '.')
    {
        while(isdigit(s[++i] = c = getch()))
        {
            ;
        }
    }
    s[i] = '\0';
    if(c != EOF)
    {
        ungetch(c);
    }
    return NUMBER;
}

int getch(void);
int getch(void)
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c);
void ungetch(int c)
{
    if(bufp >= BUFSIZE)
    {
        printf("以超出最多可承受的字符数量\n");
    }
    else
    {
        buf[bufp++] = c;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-10 11:38:16 From FishC Mobile | 显示全部楼层
函数代码本身没什么问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-11 09:43:53 | 显示全部楼层
BngThea 发表于 2017-12-10 11:38
函数代码本身没什么问题

可是他就是报错了,我也不知道哪里错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-11 09:56:31 | 显示全部楼层
neil123 发表于 2017-12-11 09:43
可是他就是报错了,我也不知道哪里错了

错误提示?准确一点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 03:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表