[交流]我自己的程序


/*初学c时整的,这是一个计算器的程序清单*/

#define MAX 50
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>

char OP[10]={'+','-','*','/','^','(',')','='};
int m;

typedef struct
{float q[MAX];
int top;
}NUMBER;

typedef struct
{char q[MAX];
int top;
}CHARACTOR;

void initstack1(NUMBER *s1);
void initstack2(CHARACTOR *s2);

int stackemty1(NUMBER s1);
int stackemty2(CHARACTOR s2);

void push1(NUMBER *s1,float e);
void push2(CHARACTOR *s2,char e);

void pop1(NUMBER *s1,float *e);
void pop2(CHARACTOR *s2,char *e);

int isp(char x);
int iesp(char x);

void drawpicture(void);

void getcher(char exp[]);

float evaluate(char exp[]);

void Help(void);

void main()
{ char exp[MAX]={0};
  char j;
  float v;
  int k;
  fflush(stdin);
  do{
  clrscr();
  drawpicture();
  gotoxy(1,19);
  printf("ress the 'F1' to get the help information!");
  gotoxy(2,3);
  getcher(exp);
  v=evaluate(exp);

  printf("=%f\n",v);
  gotoxy(1,19);
  clreol();
  printf("continue?y/nput y to continue,n to out)");
  j=getch();
  fflush(stdin);
}while(j=='y');
}

void initstack1(NUMBER *s1)
{ (*s1).top=-1;}

void initstack2(CHARACTOR *s2)
{ (*s2).top=-1;}

int stackemty1(NUMBER s1)
{if(s1.top==-1)return 1;else return 0;}

int stackemty2(CHARACTOR s2)
{if(s2.top==-1)return 1;else return 0;}

void push1(NUMBER *s1,float e)
{if((*s1).top==MAX-1)
{printf("FULL\n");return;}
  (*s1).q[++((*s1).top)]=e;
}

void push2(CHARACTOR *s2,char e)
{if((*s2).top==MAX-1)
{printf("FULL\n");return;}
(*s2).q[++((*s2).top)]=e;
}

void pop1(NUMBER *s1,float *e)
{if((*s1).top==-1){printf("the stack is empty\n");return;}
*e=(*s1).q[(*s1).top--];
}

void pop2(CHARACTOR *s2,char *e)
{if((*s2).top==-1){printf("the stack is empty\n");return;}
*e=(*s2).q[(*s2).top--];
}

int isp(char x)
{int i;
if(x=='^') {i=3;}
else if(x=='*'||x=='/') {i=2;}
else if(x=='+'||x=='-') {i=1;}
else if(x=='(') {i=0;}
return i;
}

int iesp(char x)
{int i;
if(x=='^') {i=4;}
else if(x=='*'||x=='/') {i=2;}
else if(x=='+'||x=='-') {i=1;}
else if(x=='(') {i=4;}
return i;
}

void drawpicture(void)
{
");
printf("        图略      ");
gotoxy(2,3);

}

void Help(void)
{
  gotoxy(1,20);
  printf("HELP");
  printf("1.put the express like this:3*2^(4+2*2-6)-5;\n");
  printf("2.You can use '0' to '9' and '+','_','*','/','^','.'\n");
  printf("3.press the <- and -> to add and delete the number;\n");
  printf("4.put the 'backpace' to delete the numbber\n");
  printf("4.when you put off the express,push the 'Enter' to calulate!\n");
  printf("5.you can press 'Esc' to end the program!\n");
}

int In(char c)
{
int i=0;
while(i<8)
  {
   if(c==OP[i++])
   return 1;
  }
return 0;
}

void getcher(char exp[])
{ int i=0,j=0,k;
  char I;
  int count1=0,count2=0;

  for(;i!=0x1c0d;)
  { i=bioskey(0);
   I=i;
  if(i==0xe08||i==0x5300)
  { j--;
     printf("\b \b");
  continue;}
   else if((i&0x00ff)==27)
  {
   exit(0);
  }
  else if(i==0x3b00) //f1
  {
   Help();
   gotoxy(2+j,3);
   continue;
  }
  else if(j>=0)
  {
  if(((i&0x00ff)!=0)&&(i!=0x1c0d))
  {
   exp[j++]=I;
   printf("%c",I);
  }

else
{ if(i==0x4b00)
   {
  gotoxy(1+j--,3);
  }
  else if(i==0x4d00)
  {gotoxy(1+j++,3);}

  else if(i==0x1c0d)
  {exp[j++]='=';}
  else
  {clrscr();
       printf("Wrong! Please check");break;
  }//else
}//else
}// else if
}//for
for(i=0;i<j;i++)
{
  if(exp=='(')
   count1++;
  if(exp==')')
   count2++;
}
if(count1!=count2)
{
  printf("the (,) are not match.The program will exit in 200 ms!");
  delay(2000);
  exit(-1);
}
m=j;
}//getchar


float Operate(float a,char e,float b)
{
float t;
switch(e)
{
  case '+':t=a+b;break;
  case '-':t=a-b;break;
  case '*':t=(a)*(b);break;
  case '/':t=(a)/(b);break;
  case '^':t=pow(a,b);break;
}
return t;
}


float evaluate(char exp[])
{
NUMBER s1;
CHARACTOR s2;

int n,j=0;
int count;
float x,a,b,v;
char c,e,r;
initstack1(&s1);
initstack2(&s2);
push2(&s2,'(');


   while(1)
{
  n=j;
  for(j=n;j<m;j++)
  {
   r=exp[j];
   if(!In(r))
   {
    sscanf(exp+j,"%f",&x);
    while(1)
    {
     j++;
     if(In(exp[j]))
      break;

    } //while
   push1(&s1,x);
    break;
   } //if

   else
   {
    sscanf(exp+j,"%c",&c);
    switch(c)
    {
     case ')':while(s2.q[s2.top]!='(')
               {
                pop2(&s2,&e);
                pop1(&s1,&b);
                pop1(&s1,&a);
                push1(&s1,Operate(a,e,b));
               } //while
               pop2(&s2,&c);
               break;

case '=': while(s2.top!=0)
           {
               pop2(&s2,&e);
               pop1(&s1,&b);
               pop1(&s1,&a);
               push1(&s1,Operate(a,e,b));
           }
              pop1(&s1,&v);
              return v;
  default:
          while(iesp(c)<=isp(s2.q[s2.top]))
          {
           pop2(&s2,&e);
           pop1(&s1,&b);
           pop1(&s1,&a);
           push1(&s1,Operate(a,e,b));
          }
          push2(&s2,c);
          break;
    }//switch
   } //else
  }//for
}//while


}



(有点 长了阿,算法还需要改进)
呵呵