表达式计算器(逆波兰)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define maxl 128
int pointer=0;
char judgement='y';
int shuzi(char ab[],int is); /*判断字符串ab中的字符是否都是合法字符*/
                             /*合法返回0,否则返回1*/
double change(char s[],int *st);
int kuohao(char s[],int is);/*判断字符串s中的左右括号个数是否匹配*/
                            /*匹配返回0,不匹配返回1*/
void inim(int ck[]);/*初始化合法字符列表ck*/
void calit(double s1[],int *to1,char s2[],int *to2,char tem);
void nospace(char s[]);/*去除字符串s中的空格*/
int importance[maxl];
main(){
 double ans=0.0;
 char pin[maxl],temp,stack2[maxl];/*stack2 为符号栈*/
 int i,top1,top2,lenth;
 double  stack1[maxl];/*操作数栈*/
 for (i=0;i<=maxl;++i) importance[i]=0;
 printf("\n***********************************\n");
 printf("* Welcome to use my calculator!!!!*\n");
 printf("*                                 *\n");
 printf("* You can input a representation  *\n");
 printf("* then the machine will get the   *\n");
 printf("* result.                         *\n");
 printf("* If you want to use the result   *\n");
 printf("* last time ,you can use a 'a' or *\n");
 printf("* a 'A',at the first time they are*\n");
 printf("* zero,be careful.                *\n");
 printf("***********************************\n");
 inim(importance);
 while((judgement=='y')||judgement=='Y')
 {
  printf("\n\nPlease input a representation\n");
  while(1)
  {
   pointer=0;i=0;top1=-1;top2=0;lenth=0;/*显式初始化*/
   stack2[0]='#';
   gets(pin);/*pin中存放的是输入的表达式*/
   nospace(pin);/*先对pin中的表达式去除空格*/
   lenth=strlen(pin)-1;
   if (shuzi(pin,lenth))/*shuzi(pin)判断pin中的表达式是否合法*/
   {
    printf("\nNot a right input,you must input again.\n");
    continue;
   }
   if (kuohao(pin,lenth))/*kuohao(pin)判断pin中的表达式中的括号是否能匹配*/
   {
    printf("\nyou may lost a'(' or a ')',please input again\n");
    continue;
   }
   if (pin[0]==45)
   {
    if (pin[1]=='(')
    {
     stack2[++top2]='-';
     stack1[++top1]=0;
     i=1;
    }
    else
    {
  ++i;
  stack1[++top1]=0-change(pin,&i);
  ++i;
    }
   }
   while(i<=lenth)
   {
    temp=pin[i];
 if(((temp<='9')&&(temp>='0'))||(temp=='.')||((temp=='-')&&(importance[pin[i-1]]!=(-3))))
    {
     stack1[++top1]=change(pin,&i);
    }
     else
      {
       if ((temp=='a')||(temp=='A'))
       {
        stack1[++top1]=ans;
       }
       else
       {
        if (importance[temp]>=importance[stack2[top2]])/*判断运算符的优先级*/
                                                       /*如果当前操作的优先级高于之前操作的优先级,压栈*/
        {
  stack2[++top2]=temp;
        }
        else
        {
 calit(stack1,&top1,stack2,&top2,temp);
  if (pointer) continue;
  stack2[++top2]=temp;
    if(temp==')') top2-=2;
        }
      }
     }
    ++i;
  }
  calit(stack1,&top1,stack2,&top2,'#');
  if (pointer) continue;
  printf("the result is \n");
  printf("%lf\n",ans=stack1[0]);
  break;
  }
  printf("\ncontinue(y/n)?");
  judgement=getch();
  while((judgement!='y')&&(judgement!='Y')
       &&(judgement!='n')&&(judgement!='N'))
  {
       printf("\nyou need to input again.\n");
       judgement=getch();
  }
 }
 printf("\n***********************************\n");
 printf("May you have a good time!!\nBye bye!!\n");
 printf("***********************************\n");
 getch();
 return 0;
}
double change(char s[],int *st)/*从字符串s中读取一个数,st是在s上的游标*/
                               /*即从st开始读出一个数,如果读不出来则返回0*/

{
 int count,q=1;
 char tm[maxl]=" ";
 if ((s[*st]=='-')&&(importance[s[(*st)-1]]>(-3)))
 {
  q=-1;
  ++(*st);
 }
 for (count=0;((s[*st]<='9')&&(s[*st]>='0'))||(s[*st]=='.');++(*st),++count)
  tm[count]=s[*st];
 tm[count]='\0';
 --(*st);
 return (atof(tm)*q);
}
void inim(int ck[])
{
 ck['+']=1;
 ck['-']=1;
 ck['*']=2;ck['^']=3;
 ck['/']=2;
 ck['(']=10;
 ck[')']=-1;
 ck['#']=-2;
 ck['a']=-3;ck[' ']=-3;ck['.']=-3;
 ck['1']=-3;ck['A']=-3;
 ck['2']=-3;
 ck['3']=-3; ck['7']=-3;
 ck['4']=-3; ck['8']=-3;
 ck['5']=-3; ck['9']=-3;
 ck['6']=-3; ck['0']=-3;

}
void calit(double s1[],int *to1,char s2[],int *to2,char tem)/*解析表达式*/
{
 for(;importance[s2[*to2]]>importance[tem];--(*to2))
 {
  if(s2[*to2]=='(')
  {
   return;
  }
  if((*to1)==0)
   {
    printf("what you have input was wrong,input again\n");
    pointer=1;
    return;
   }
  switch(s2[*to2])
  {
   case '+':
    --(*to1);
    s1[*to1]+=s1[(*to1)+1];
    break;
   case '-':
    --(*to1);
    s1[*to1]-=s1[(*to1)+1];
    break;
   case '*':
    --(*to1);
    s1[*to1]*=s1[(*to1)+1];
    break;
   case '/':
    --(*to1);
    if (s1[(*to1)+1]==0.0)
    {
     printf("0 is not a divisor,you must input again.\n");
     pointer=1;
     return;
    }
    else
    {
     s1[*to1]/=s1[(*to1)+1];
     break;
    }
   case '^':
    --(*to1);
    if ((s1[*to1]==0)&&(s1[(*to1)+1]==0))
    {
     printf("0^0 is not legal,input again");
     pointer=1;
     return;
    }
    s1[*to1]=pow(s1[*to1],s1[(*to1)+1]);
    break;
  }
 }
}
void nospace(char s[])/*去掉字符串中的空格*/
{
 int temi,len,si;
 len=strlen(s)-1;
 for(temi=0,si=0;si<=len;++si)
 {
  if(s[si]!=' ')
  {
   s[temi++]=s[si];
  }
 }
 s[temi]='\0';
}
int kuohao(char s[],int len)/*判断括号是否匹配,匹配返回0,不匹配返回1*/
{
 int tem=0,count;
 for (count=0;count<=len;++count)
 {
  if (s[count]=='(') ++tem;
  if (s[count]==')') --tem;
  if (tem<0) return 1;
 }
 if (tem>0) return 1;
 return 0;
}
int shuzi(char ab[],int is)/*判断ab中存储的字符是否都是合法字符,合法返回0,否则返回1*/
{
 int i;
 for(i=0;i<=is;++i)
  {
   if (!importance[ab[i]]) return 1;
  }
 return 0;
}


posted @ 2010-10-23 00:18  幻魇  阅读(227)  评论(0编辑  收藏  举报