📄 expression.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define OK 1
typedef struct SqStack1
{
int *base;
int *top;
int stacksize;
}SqStack1;
typedef struct SqStack2
{
char *base;
char *top;
int stacksize;
}SqStack2;
void IntInitStack(SqStack1 *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)
exit(ERROR);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void CharInitStack(SqStack2 *S)
{
S->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S->base)
exit(ERROR);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
int IntGetTop(SqStack1 *S)
{
int e;
if((*S).top==(*S).base) return 0;
e=*((*S).top-1);
return e;
}
char CharGetTop(SqStack2 *S)
{
char e;
if((*S).top==(*S).base) return 0;
e=*((*S).top-1);
return e;
}
int IntPush(SqStack1 *S,int e)
{
*(*S).top++=e;
return OK;
}
int CharPush(SqStack2 *S,char e)
{
*(*S).top++=e;
return OK;
}
int IntPop(SqStack1 *S)
{
int e;
if((*S).top==(*S).base) return 0;
e=*--(*S).top;
return e;
}
int CharPop(SqStack2 *S)
{
char e;
if((*S).top==(*S).base) return 0;
e=*--(*S).top;
return e;
}
char Precede(char a,char b)
{
int i,j;
char Table[8][8]={ ' ','+','-','*','/','(',')','#',
'+','>','>','<','<','<','>','>',
'-','>','>','<','<','<','>','>',
'*','>','>','>','>','<','>','>',
'/','>','>','>','>','<','>','>',
'(','<','<','<','<','<','=',' ',
')','>','>','>','>',' ','>','>',
'#','<','<','<','<','<',' ','=',
};
for(i=0;i<8;i++)
if(Table[0][i]==a)
break;
for(j=0;j<8;j++)
if(Table[j][0]==b)
break;
return Table[j][i];
}
int Operate(int a,char theta,int b)
{
int c;
if(theta=='+') c=a+b;
else if(theta=='-') c=a-b;
else if(theta=='*') c=a*b;
else c=a/b;
return c;
}
int ReadNum(char s)
{
if(s>=49&&s<=57)
{
s-=48;
return s;
}
else
return 0;
}
int result(char *a,SqStack1 *OPND,SqStack2 *OPTR)
{
char theta;
int b,c,i=0;
IntInitStack(OPND);
CharInitStack(OPTR);
CharPush(OPTR,'#');
while(1)
{
if(ReadNum(a[i]))
IntPush(OPND,ReadNum(a[i++]));
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='#'||a[i]=='('||a[i]==')')
{
switch(Precede(a[i],CharGetTop(OPTR)))
{
case '<':CharPush(OPTR,a[i++]);break;
case '=':CharPop(OPTR);i++;break;
case '>':theta=CharPop(OPTR);
c=IntPop(OPND);
b=IntPop(OPND);
IntPush(OPND,Operate(b,theta,c));
break;
}
}
if(a[i]=='#'&&CharGetTop(OPTR)=='#')
{
printf("The result is %d.\n",IntGetTop(OPND));
return OK;
}
}
}
char enchange(char *a)
{
char ch,b[100];
int i=0,j=0;
SqStack2 r,*R;
R=&r;
CharInitStack(R);
CharPush(R,'#');
ch=a[i];
while(ch!='#')
{
if(ch=='(')
{
CharPush(R,ch);
ch=a[++i];
}
else if(ch==')')
{
if(CharGetTop(R)!='(')
b[j++]=CharPop(R);
CharPop(R);
ch=a[++i];
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
if(Precede(ch,CharGetTop(R))=='<')
CharPush(R,ch);
else
b[j++]=CharGetTop(R);
ch=a[++i];
}
else if(ch>=49&&ch<=57)
{
ch-=48;
b[j++]=ch;
ch=a[++i];
}
}
ch=CharPop(R);
while(ch!='#')
{
b[j]=ch;
ch=CharPop(R);j++;
}
b[j]='#';
printf("The changed expression is: ");
for(i=0;b[i]!='#';i++)
{
if(b[i]>=1&&b[i]<=9)
printf("%d",b[i]);
else
printf("%c",b[i]);
}
printf(".\n");
return OK;
}
void main()
{
char a[100];
SqStack1 s1,*OPND;
SqStack2 s2,*OPTR;
OPND=&s1;
OPTR=&s2;
printf("enter an expression end of '#'.\n");
printf("The Expression:");
scanf("%s",&a);
result(a,OPND,OPTR);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -