| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3216 人关注过本帖
标题:逆波兰式计算器
只看楼主 加入收藏
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
结帖率:100%
收藏
 问题点数:0 回复次数:7 
逆波兰式计算器
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define  MAX      100
#define  NUM     '0'
#define  SMAX     200
#define  AGAIN    -2
double pop(void);
void push(double);
int getst();
int get(void);
void unget(int);
double stac[MAX];
char  suf[SMAX];
char string[SMAX];
char   s[MAX];
int  point,poi;
void main()
{
     int type;
     double  mid,midans;
     point=poi=0;
     printf("input 'flash'meaning flash the stack:\n");
     while((type=getst())!=EOF)
     {
                switch(type)
                {
                        case     NUM:
                                        push(atof(s));


                                        break;
                        case     '+':   midans=pop()+pop();
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '*':
                                        midans=pop()*pop();
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '-':

                                        mid=pop();
                                        midans=pop()-mid;
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '/':
                                        mid=pop();
                                        if(mid==0)
                                        {
                                                printf("ERROR!the location of the 0 is wrong!\n");
                                                getch();
                                                break;
                                        }
                                        else
                                        {
                                               midans=pop()/mid;
                                               push(midans);
                                               printf("%f\n",midans);
                                               getch();
                                        }
                                        break;
                        case     '%' :
                                        mid=pop();
                                        if(mid==0)
                                        {
                                                printf("ERROR!the location of the 0 is wrong!\n");
                                                getch();
                                                break;
                                        }
                                        else
                                        {
                                                midans=(int)pop()%(int)mid;
                                                push(midans);
                                                printf("%d\n",midans);
                                                getch();
                                        }
                                        break;
                        case     '\n':
                                        printf("%f\n",pop());
                                        printf("Please input :\n");
                                        getch();

                                        break;
                         case    AGAIN :
                                        break;


                        default      :
                                        printf("%d\n",type);
                                        printf("ERROR!The get is wrong !\n");
                                        break;
                }
     }
     getch();
}
double pop(void)
{
        if(point<=0)
        {
                printf("The array is empty!\n");

                return    0;
        }
        else
                return   stac[--point];
}
void  push(double f)
{
        if(point<MAX)
                stac[point++]=f;
        else
        {
                printf("The memory is limited!\n");
        }
}
int getst()
{
        int type,i,c,k,length,m;
        i=1;
        k=0;
        if(poi<1)
        {
                aa:     gets(string);
                if(!strcmp(string,"flash"))
                {
                        while(k<MAX)
                        {
                                stac[k]=0;
                                k++;
                        }
                                return AGAIN;

                }
                poi=length=strlen(string);
                if(length>SMAX-1)
                {
                        printf("the memory of the suf is limited!\n");
                        printf("Please input again!\n");
                        goto  aa;
                }
                for(m=0,length--;length>=0;length--,m++)
                        suf[m]=string[length];
                suf[m]='\0';
        }
        for(;(s[0]=c=get())==' '||c=='\t';)
                 ;
        s[1]='\0';
        if(!isdigit(c)&&c!='.'&&c!='-')

                return  c;
        if(isdigit(c)||'-'==c)
        {
                while(isdigit(s[i++]=c=get()))
                        ;
                if(!isdigit(c)&&2==i&&s[0]=='-')
                {
                        unget(c);
                        return  '-';

                 }
                if(c=='.')
                {

                        while(isdigit(s[i++]=c=get()))
                                ;
                }
                unget(c);
                s[--i]='\0';


                return  NUM;
        }
        else if(c=='\n')
                return   '\n';
        else
        {
                printf("ERROR!");
                printf("The char cant't be recoganized!\n");

         }
}
int get()
{
        char c;
        if(poi>0)
                return  suf[--poi];
        else
               if((c=getchar())=='\t')
               {
                        printf("Please input !not the '\t'");
                        return       AGAIN;
                }
}
void unget(int c)
{
        if(poi>=SMAX)
        {
                printf("ERROR!The memory is limited !\n");
                getch();
                exit(0);
        }
        else
                suf[poi++]=c;
}
大家看哈啊:一起努力啊!
搜索更多相关主题的帖子: 计算器 flash 波兰 
2011-02-11 12:53
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
灌水啊
MACHINE3.rar (17.56 KB)


/*************我就是程序;程序就是我!******************/
2011-02-11 12:55
wujieru
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:1
帖 子:1108
专家分:1939
注 册:2010-10-9
收藏
得分:0 
有意思吗???就算是波兰又怎么样??? 你就算了吧
2011-02-11 12:59
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
收藏
得分:0 
什么情况
2011-02-11 13:37
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
支持楼主

编译原理里面有一个更好的技术
就是用映射表,很快…

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2011-02-11 13:59
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define  MAX     100                                           /*stac的容量*/
#define  NUM     '0'                                           /*数字类型标识符*/
#define  SMAX     200                                          /*缓冲池的容量*/
#define  AGAIN    -2                                           /*接着再接受输入的标识符*/
#define  SIN      -3                                           /*sin函数的标识符*/
#define  COS      -4                                           /*cos函数的标识符*/
#define  EXP      -5                                           /*exp函数的标识符*/
#define  POW      -6                                           /*pow函数的标识符*/
double pop(void);
void   push(double);
int    getst();
int    get(void);
void   unget(int);
double stac[MAX];
char   suf[SMAX];
char   string[SMAX];
char   s[MAX];
int    point,poi;
void main()
{
     int type;
     double  mid,midans;
     point=poi=0;
     printf("Welcome to my calculator!\n ");
     printf("Illustration:\n");
     printf("Inverse Poland type calculator:\n");
     printf("This calculator includes the 'sin','cos','exp','pow':");
     printf("Please input in the right order!\n");
     printf("input 'flash'meaning flash the stack:\n");
     while((type=getst())!=EOF)                                /*循环判断输入的情况*/
     {           switch(type)
                {
                        case     SIN:                          /*求正弦*/
                                        midans=sin(pop());
                                        push(midans);          /*回收结果*/
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case    COS:
                                        midans=cos(pop());
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     EXP:
                                        midans=exp(pop());
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     POW:
                                        mid=pop();
                                        midans=pow(mid,pop());
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     NUM:
                                        push(atof(s));
                                        break;
                        case     '+':   midans=pop()+pop();
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '*':
                                        midans=pop()*pop();
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '-':
                                        mid=pop();
                                        midans=pop()-mid;
                                        push(midans);
                                        printf("%f\n",midans);
                                        getch();
                                        break;
                        case     '/':
                                        mid=pop();
                                        if(mid==0)
                                        {
                                                printf("ERROR!the location of the 0 is wrong!\n");
                                                getch();
                                                break;
                                        }
                                        else
                                        {
                                               midans=pop()/mid;
                                               push(midans);
                                               printf("%f\n",midans);
                                               getch();
                                        }
                                        break;
                        case     '%' :
                                        mid=pop();
                                        if(mid==0)
                                        {
                                                printf("ERROR!the location of the 0 is wrong!\n");
                                                getch();
                                                break;
                                        }
                                        else
                                        {
                                                midans=(int)pop()%(int)mid;
                                                push(midans);
                                                printf("%d\n",midans);
                                                getch();
                                        }
                                        break;
                        case     '\n':
                                        printf("%f\n",pop());
                                        printf("Please input :\n");
                                        getch();

                                        break;
                         case    AGAIN :
                                        break;
                         default      :
                                        printf("%d\n",type);
                                        printf("ERROR!The get is wrong !\n");
                                        break;
                }
     }
     getch();
}
double pop(void)                                               /*出栈*/
{
        if(point<=0)
        {
                printf("The array is empty!\n");

                return    0;
        }
        else
                return   stac[--point];
}
void  push(double f)
{                                                              /*入栈*/
        if(point<MAX)
                stac[point++]=f;
        else
        {
                printf("The memory is limited!\n");
        }
}
int getst()
{                                                              /*获得输入的类型,并返回类型*/
        int type,i,c,k,length,m;
        i=1;
        k=0;
        if(poi<1)
        {
                aa:     gets(string);
                if(!strcmp(string,""))
                {
                        return  '\n';
                }
                if(!strcmp(string,"flash"))
                {
                        while(k<MAX)
                        {
                                stac[k]=0;
                                k++;
                        }
                                return AGAIN;
                }
                if(!strcmp(string,"sin"))
                        return     SIN;
                if(!strcmp(string,"cos"))
                        return     COS;
                if(!strcmp(string,"exp"))
                        return     EXP;
                if(!strcmp(string,"pow"))
                        return     POW;
                poi=length=strlen(string);
                if(length>SMAX-1)
                {
                        printf("the memory of the suf is limited!\n");
                        printf("Please input again!\n");
                        goto  aa;
                }
                for(m=0,length--;length>=0;length--,m++)         /*倒着装入缓冲池*/
                        suf[m]=string[length];
                suf[m]='\0';
        }
        for(;(s[0]=c=get())==' '||c=='\t';)
                 ;
        s[1]='\0';
        if(!isdigit(c)&&c!='.'&&c!='-')
                return  c;
        if(isdigit(c)||'-'==c)
        {
                while(isdigit(s[i++]=c=get()))
                        ;
                if(!isdigit(c)&&2==i&&s[0]=='-')
                {
                        unget(c);
                        return  '-';

                 }
                if(c=='.')
                        while(isdigit(s[i++]=c=get()))
                                ;
                if(!(poi==0))
                {
                        unget(c);
                        s[--i]='\0';
                }
                return  NUM;
        }
        else if(c=='\n')
                return   '\n';
        else
        {
                printf("ERROR!");
                printf("The char cant't be recoganized!\n");

         }
}
int get()                                                        /*得到一个字符*/
{
        if(poi>0)
                return  suf[--poi];
        else
                if(poi==0)
                        return       AGAIN;
}
void unget(int c)                                                /*若字符类型不符合,再放进缓冲池*/
{
        if(poi>=SMAX)
        {
                printf("ERROR!The memory is limited !\n");
                getch();
                exit(0);
        }
        else
                suf[poi++]=c;
}


/*************我就是程序;程序就是我!******************/
2011-02-12 08:33
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
3楼的兄弟啊:指点哈啊:谢了:


/*************我就是程序;程序就是我!******************/
2011-02-12 08:34
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
MACHINE3.rar (18.42 KB)

谢谢:


/*************我就是程序;程序就是我!******************/
2011-02-12 08:35
快速回复:逆波兰式计算器
数据加载中...
 
   



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

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