📄 bds.cpp
字号:
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define M 100
int m;
void push1(char a[100],char e,int top) //把字符押入栈//
{
a[top]=e;
return;
}
void push2(int b[100],int e,int top) //把字符押入栈//
{
b[top]=e;
return;
}
int operate (int a,char theta,int b) //A和B做运算//
{
switch (theta)
{
case '+' : m=a+b; break;
case '-' : m=a-b; break;
case '/' : m=a/b; break;
case '*' : m=a*b; break;
default :printf ("error\n");
}
return(m);
}
int precede(char c,char d) //运算符做比较//
{ int I,J,f;
int p,q;
int a[6]={5,4,2,0,7,0};
int b[6]={6,3,1,7,0,0};
switch(c)
{
case '^' : I=0; break;
case '*' : I=1; break;
case '/' : I=1; break;
case '+' : I=2; break;
case '-' : I=2; break;
case '(' : I=3; break;
case ')' : I=4; break;
case '#' : I=5; break;
default :printf ("error\n");
}
p=I;
switch(d)
{
case '^' : J=0; break;
case '*' : J=1; break;
case '/' : J=1; break;
case '+' : J=2; break;
case '-' : J=2; break;
case '(' : J=3; break;
case ')' : J=4; break;
case '#' : J=5; break;
default : printf ("error\n");
}
q=J;
return( f= a[p]-b[q]);
}
void main()
{
int A;
int J,I,n;
int top1,top2; //栈顶指针
int i,j,k;
char str1[M];
char f,u;
char str[3];
int h1,h2,h3,h4;
char r,s;
char a[100];
int b[100];
char c[100],d[100];
int F;
char theta;
int p,q;
printf("请输入表达式(以#号结束):\n");
scanf("%s",str1);
top1=0; top2=0;
push1(a,'#',top1);
top1++;
push2(b,0,top2);
top2++;
r='#'; J=0;
for(i=0;(f=str1[i])!='\0';i++)
{ j=0; k=0;
if (((f=str1[i])!= '0') && ((f=str1[i])!= '1') && ((f=str1[i])!= '2') && ((f=str1[i])!= '3' )&&((f=str1[i])!= '4' )&&((f=str1[i])!= '5' )&&((f=str1[i])!= '6' )&&((f=str1[i])!= '7' )&&((f=str1[i])!= '8')&& ((f=str1[i])!= '9'))
{
while (((f=str1[i])!= '+' )&& ((f=str1[i])!='-') && ((f=str1[i])!='*') && ((f=str1[i])!='/') && ((f=str1[i])!='(') && ((f=str1[i])!=')') && ((f=str1[i])!='^') && ((f=str1[i])!='#'))
{
i++;
if (((f=str1[i])!='+') && ((f=str1[i])!='-') && ((f=str1[i])!='*') && ((f=str1[i])!='/') && ((f=str1[i])!='(') && ((f=str1[i])!=')') && ((f=str1[i])!='^') && ((f=str1[i])!='#') )
{ --i;
str[j]=str1[i];
j++; i++;
str[j]=str1[i];
}
else if ((((f=str1[i])=='+') || ((f=str1[i])=='-') ||((f=str1[i])=='*') || ((f=str1[i])=='/') || ((f=str1[i])=='(') || ((f=str1[i])==')') || ((f=str1[i])=='^') || ((f=str1[i])=='#')) && j==0)
{
--i;
u=str1[i];
printf("请输入%c的值:%c=",u,u);
scanf("%d",&h1);
push2(b,h1,top2);
top2++;
i++;
}
else if((((f=str1[i])=='+') || ((f=str1[i])=='-') || ((f=str1[i])=='*') ||((f=str1[i])=='/') || ((f=str1[i])=='(') || ((f=str1[i])==')') || ((f=str1[i])=='#') || ((f=str1[i])=='^')) && j>0)
{
printf ("请输入值");
str[j+1]='\0';
printf("%s=",str);
scanf ("%d",&h2);
push2(b,h2,top2);
top2++;
j=0;
}
}
}
else
{
while (((f=str1[i])!= '+' )&& ((f=str1[i])!='-') && ((f=str1[i])!='*') && ((f=str1[i])!='/') && ((f=str1[i])!='(') && ((f=str1[i])!=')') && ((f=str1[i])!='^') && ((f=str1[i])!='#'))
{
i++;
if (((f=str1[i])!='+') && ((f=str1[i])!='-') && ((f=str1[i])!='*') && ((f=str1[i])!='/') && ((f=str1[i])!='(') && ((f=str1[i])!=')') && ((f=str1[i])!='^') && ((f=str1[i])!='#') )
{ --i;
c[k]=str1[i];
k++; i++;
c[k]=str1[i];
}
else if ((((f=str1[i])=='+') || ((f=str1[i])=='-') ||((f=str1[i])=='*') || ((f=str1[i])=='/') || ((f=str1[i])=='(') || ((f=str1[i])==')') || ((f=str1[i])=='^') || ((f=str1[i])=='#')) && k==0)
{
--i;
d[0]=str1[i];
h3=atoi(d);
push2(b,h3,top2);
top2++;
i++;
}
else if((((f=str1[i])=='+') || ((f=str1[i])=='-') || ((f=str1[i])=='*') ||((f=str1[i])=='/') || ((f=str1[i])=='(') || ((f=str1[i])==')') || ((f=str1[i])=='#') || ((f=str1[i])=='^')) && k>0)
{
c[k+1]='\0';
h4=atoi(c);
push2(b,h4,top2);
top2++;
k=0;
}
}
}
s=str1[i];
if (r!='#')
{
F=precede(r,s);
if(F<0)
{
r=s;
push1(a,r,top1);
top1++;
}
else if (F==0)
{
top1--;
r=a[top1];
}
else
{
theta=r;
top2--;
p=b[top2];
top2--;
q=b[top2];
if (r=='^')
{
n=q;
for (I=1;I<p;I++)
n=n*q;
m=n;
}
else
{
m=operate(q,theta,p);
}
push2(b,m,top2);
top2++;
top1--;
top1--;
r=a[top1];
top1++;
J=0;
while(r!='#' && J==0)
{
F=precede(r,s);
if(F<0)
{
r=s;
push1(a,r,top1);
top1++;
J=1;
}
else if (F==0)
{
top1--;
top1--;
r=a[top1];
top1++;
J=1;
}
else
{
theta=r;
top2--;
p=b[top2];
top2--;
q=b[top2];
if (r=='^')
{
n=q;
for (I=1;I<p;I++)
n=n*q;
m=n;
}
else
{
m=operate(q,theta,p);
}
push2(b,m,top2);
top2++;
top1--;
top1--;
r=a[top1];
top1++;
}
}
}
}
if (r=='#' && s!='#')
{
r=s;
push1(a,r,top1);
top1++;
}
else if(r!='#' && s!='#' && s!=')')
{
r=s;
}
else if (r=='#' && s=='#')
{
printf("最后的结果是:");
printf("%d",m);
printf("\n");
}
else
A=1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -