用户工具

站点工具


paste

这是本文档旧的修订版!


Copy and Paste

沉浸式翻译
saladict

This page is used for copy and paste sharing.

In yaml

#define MAXOP 20
#define NUMBER '0'
#define TOOBIG '9'
#define EOF 0
 
main()
{
    int type;
    char s[MAXOP];
    double op2, atof(), pop(), push();
 
    while ((type = getop(s, MAXOP)) != EOF)
        switch (type){
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop() + pop());
            break;
        case '*':
            push(pop() * pop());
            break;
        case '-':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() - op2);
            break;
        case '/':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("zero divisor popped\n");
            break;
        case '=':
            printf("\t%f\n", push(pop()));
            break;
        case 'c':
            clear();
        case TOOBIG:
            printf("%.20s ... is too long\n", s);
            break;
        default:
            printf("unknow command %c\n", type);
            break;
}
 
#define MAXVAL 100
 
int sp = 0;
double val[MAXVAL];
 
double push(f)
double f;
{
    if(sp < MAXVAL)
        return(val[sp++] = f);
    else {
        printf("error: stack full\n");
        clear();
        return(0);
    }
}
 
double pop()
{
    if(sp > 0)
        return(val[--sp]);
    else{
        printf("error: stack empty\n");
        clear();
        return(0);
    }
}
 
clear()
{
    sp = 0;
}
 
char mygetchar();
 
getop(s, lim)
char s[];
int lim;
{
    int i, c;
 
    while ((c = getch()) == ' ' || c == '\t'  || c == '\n')
        ;
    if(c != '.' && (c < '0' || c > '9'))
        return(c);
    s[0] = c;
    for (i = 1; getCharAndInRange(&c, mygetchar, '0', '9'); i++)
    /* Same as for (i = 1; (c = getchar()) >= '0' && c <= '9'; i++)*/
    /* 这里为了写出函数指针,强行弄了个函数getCharAndInRange*/
        if(i < lim)
            s[i] = c;
    if (c == '.') {
        if (i < lim)
            s[i] = c;
        for(i++; (c = getchar()) >= '0' && c <= '9'; i++)
            if (i < lim)
                s[i] = c;
    }
    if (i < lim) {
        ungetch(c);
        s[i] = '\0';
        return(NUMBER);
    } else {
        while (c != '\n' && c != EOF)
            c = getchar();
        s[lim -1] = '\0';
        return(TOOBIG);
    }
}
 
#define BUFSIZE 100
 
char buf[BUFSIZE];
int bufp = 0;
 
getch()
{
    return((bufp>0) ? buf[--bufp] : getchar());
}
 
ungetch(c)
int c;
{
    if(bufp > BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}
 
char mygetchar()
{
    return(getchar());
}
 
getCharAndInRange(pchar, getCharFunc, start, end)
char *pchar;
int (*getCharFunc)(), start, end;
{
    *pchar = (*getCharFunc)(110);
    return(*pchar >= start && *pchar <= 9);
}

Ignore me

paste.1696669590.txt.gz · 最后更改: root