⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8.cpp

📁 8、对中缀表达式(如(2+3.25)*4=)求组
💻 CPP
字号:
#define Max 20
#include "math.h"
#include "stdio.h"
typedef struct
{
 float List[Max];
 int top,bot;
}ORS;
typedef struct
{
 char a;
 int b;
}Dype;
typedef struct
{
 Dype  List1[Max];
 int top,bot;
}OSS;
typedef struct
{
 char List[Max];
 int n;
}SeqList;
SeqList *L[Max];
int k,j=0;
void Insert(SeqList *L,char a,int i)
{

 L->List[i]=a; if(a=='.') { j=i;L->List[j]=48;}  k=i;
}
float Ret(SeqList *L)
{
  int i=0,a;float t=0;
  if(j==0)  while(i<=k) {t=t+(L->List[i]-48)*(float)(pow(10,k-i));i++;}
  else{ while(i<=k)
       {if((a=j-i-1)>=0) a=j-i-1;
       else a=j-i;
       t=t+(L->List[i]-48)*(float)(pow(10,a));
       i++;
       }
      }
 return t;
}
Makempty2(OSS *s) {s->bot=0;s->top=-1;}
Makempty1(ORS *s) {s->bot=0;s->top=-1;}
Empty2(OSS *s)    {if(s->top==-1) return 1;else return 0; }
Empty1(ORS *s)    {if(s->top==-1) return 1;else return 0; }
char Gettop2(OSS *s)
{
 if(Empty2(s))   printf("\nOSS empty\n");
 else return  s->List1[s->top].a;
}
int Gettop21(OSS *s)
{
 if(Empty2(s))  printf("\nOSS empty\n");
 else return s->List1[s->top].b ;
}
float Gettop1(ORS *s)
{
 if(Empty1(s))  printf("\nORS empty\n");
 else return(s->List[s->top]);
}
Pop2(OSS *s)
{
 if(Empty2(s)) printf("\nOSS empty\n");
 else s->top--;
}
Pop1(ORS *s)
{
 if(Empty1(s)) printf("\nORS empty\n");
 else s->top--;
}
Push2(OSS *s,char x,int y)
{
 s->top++; s->List1[s->top].a=x;
 s->List1[s->top].b=y;
}
Push1(ORS *s,float x)
{
s->top++;
s->List[s->top]=x;
}
float Calcul(char *A)
{
 OSS *q;ORS *p; char *s=A,c; float a,b,f;int i=0,h,w; p=(OSS *)malloc(sizeof(OSS));q=(ORS *)malloc(sizeof(ORS));
 Makempty1(p); Makempty2(q);Push2(q,';',0);
 while(*s!='\0')
 {
  if(*s==';'||*s=='('||*s==')'||*s=='-'||*s=='+'||*s=='/'||*s=='*'||*s=='%'||*s=='^')
  {
   switch(*s)
   {
    case ';': w=0; break;
    case '(':
    case ')': w=1; break;
    case '-':
    case '+': w=2; break;
    case '/':
    case '*':
    case '%': w=3; break;
    case '^': w=4; break;
   }
   if(*s=='('||w>Gettop21(q))  { Push2(q,*s,w); *s++;}
   else if(*s==';'&&Gettop2(q)==';') { return(Gettop1(p));*s++;}
	else if(*s==')'&&Gettop2(q)=='(')  { Pop2(q); *s++;}
	      else if(w<=Gettop21(q))
		   { b=Gettop1(p);Pop1(p);a=Gettop1(p);Pop1(p);c=Gettop2(q);Pop2(q);
		    switch(c)
		    {
		     case '+' :   f=a+b;break;
		     case '-' :   f=a-b;break;
		     case '*' :   f=a*b;break;
		     case '/' :   f=a/b;break;
		    }
		    Push1(p,f);
		   }
}
else { h=0; L[i]=(SeqList*)malloc(sizeof(SeqList));
       while(*s>=48&&*s<=57||*s==46)      { Insert(L[i],*s,h++);*s++;}
       Push1(p,Ret(L[i]));i++;
      }
 }
}
void main()
{
 char a[Max];
 printf("\ninput a string:end with ';',(**--^)\n");
 gets(a);
 printf("\nthe answer is %f",Calcul(a));   getchar();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -