永发信息网

逆波兰式的生成程序

答案:5  悬赏:10  手机版
解决时间 2021-01-29 13:32
  • 提问者网友:凉末
  • 2021-01-28 20:20
c 语言编写 能直接运行

c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言c语言
最佳答案
  • 五星知识达人网友:鸠书
  • 2021-01-28 20:59


#include
#include      

#define MAXOP   100     
#define NUMBER  '0'     

int getOp(char []);
void push(double);
double pop(void);

main() {
    int type;
    double op2;
    char s[MAXOP];

    while ((type = getOp(s)) != EOF) {
        switch (type) {
            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.0)
                    push(pop() / op2);
                else
                    puts("error: zero divisor\n");
                break;
            case '\n':
                printf("\t%.8g\n", pop());
                break;
            default:
                printf("error: unknown command %s\n", s);
                break;
        }
    }
    return 0;
}



#define MAXVAL  100     

int sp = 0;             
double val[MAXVAL];     


void push(double f) {
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
}


double pop(void) {
    if (sp > 0)
        return val[--sp];
    else {
        puts("error: stack empty\n");
        return 0.0;
    }
}



#include

int getChar_localVersion(void);
void ungetChar_localVersion(int c);


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

    while ((s[0] = c = getChar_localVersion()) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.' && c != '-')
        return c;       
    i = 0;
    if (c == '-')       
        if(isdigit(s[1] = c = getChar_localVersion()))
            ++i;
        else {
            ungetChar_localVersion(c);
            s[1] = '\0';
            return s[0];
        }
    if (isdigit(c))     
        while (isdigit(s[++i] = c = getChar_localVersion()))
            ;
    if (c == '.')       
        while (isdigit(s[++i] = c = getChar_localVersion()))
            ;
    s[i] = '\0';
    if (c != EOF)
        ungetChar_localVersion(c);

    return NUMBER;
}



#define BUFSIZE 100

char buf[BUFSIZE];      
int bufp = 0;           


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


void ungetChar_localVersion(int c) {
    if (bufp >= BUFSIZE)
        puts("ungetChar_localVersion: too many characters\n");
    else
        buf[bufp++] = c;
}
全部回答
  • 1楼网友:躲不过心动
  • 2021-01-29 00:53
这个是C++的!需要改写和我联系好了1 #include #include #include #include #define maxbuffer 64 void main() { char display_out(char out_ch[maxbuffer], char ch[32]); //int caculate_array(char out_ch[32]); static int i=0; static int j=0; char ch[maxbuffer],s[maxbuffer],out[maxbuffer]; cout<<"请输入中缀表达式: "; cin>>ch; for(i=0;i
  • 2楼网友:梦中风几里
  • 2021-01-28 23:16
#include #include int i,j; struct thread { int id; int priority; int alltime; int startblock; int blocktime; int state;//0表示ready; 1表示run; 2表示block; int readycount; }thread[5]={{0,9,3,2,6,0,0},{1,38,3,-1,0,0,0},{2,30,6,-1,0,0,0},{3,30,3,-1,0,0,0},{4,0,4,-1,0,0,0}},t; void run() { cout<0) thread[i].startblock--; } void ready() { cout<
  • 3楼网友:愁杀梦里人
  • 2021-01-28 21:53
#include "stdio.h" #include "stdlib.h" # define maxsize 20 typedef struct{    //定义栈     char data[maxsize];     int top;     }sqstack;//sqstack int change(char *abc,sqstack s) {     char *p;int i,j=0;     for(p=&abc[0];*p!='\0';p=&abc[++j])     {     if(*p=='(')     s.data[++s.top]=*p;     else if(*p==')')     {     while(s.data[s.top]!='(')     {     printf("%c",s.data[s.top]);     s.top--;     }     s.top--;     }     else if(*p=='*'||*p=='/')     s.data[++s.top]=*p;     else if(*p=='+'||*p=='-')     {     for(i=s.top;i>=-1;i--)     if(s.data[i]=='(')     {     s.data[++s.top]=*p;     break;     }     else     {     while(s.top!=-1)     {     printf("%c",s.data[s.top]);     s.top--;     }     s.data[++s.top]=*p;     }     }     else     printf("%c",*p);         }     while(s.top!=-1)     {     printf("%c",s.data[s.top]);     s.top--;     }     return 1; } int main() {     char abc[maxsize];     sqstack s;     s.top=-1;     printf("请输入表达式:");     scanf("%s",abc);     printf("\n%s\n",abc);     printf("该表达式的逆波兰式为:");     change(abc,s);     fflush(stdin);     getchar();     return 0; }   这个你看看行不
  • 4楼网友:煞尾
  • 2021-01-28 21:13
#include #include int i,j; struct thread { int id; int priority; int alltime; int startblock; int blocktime; int state;//0表示ready; 1表示run; 2表示block; int readycount; }thread[5]={{0,9,3,2,6,0,0},{1,38,3,-1,0,0,0},{2,30,6,-1,0,0,0},{3,30,3,-1,0,0,0},{4,0,4,-1,0,0,0}},t; void run() { cout<0) thread[i].startblock--; } void ready() { cout<
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯