#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 50
char ops[7]={‘+’,'-’,'*’,'/’,'(‘,’)',’#'}; //运算符数组
//用来进行比较运算符优先级的矩阵,3代表’=',2代表’>’,1代表’<’,0代表不可比
int cmp[7][7]={
{2,2,1,1,1,2,2},
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{2,2,2,2,0,2,2},
{1,1,1,1,1,0,3}};
//运算符 栈的定义
typedef struct
{
char elem[Stack_Size];
int top;
}SeqStack;
//运算数 栈的定义
typedef struct
{
int elem[Stack_Size];
int top;
}SeqStackOperand;
//初始化运算符栈
void InitStack(SeqStack *S)
{
S->top =-1;
}
//初始化运算数栈
void InitStacknOperand(SeqStackOperand *S)
{
S->top =-1;
}
//判断栈S为空栈时返回值为真, 反之为假
int IsEmpty(SeqStack *S)
{
return(S->top==-1?TRUE:FALSE);
}
//判断栈S为空栈时返回值为真, 反之为假
int IsEmptynOperand(SeqStackOperand *S)
{
return(S->top==-1?TRUE:FALSE);
}
//判断栈S为满栈时返回值为真, 反之为假
int IsFull(SeqStack *S)
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
//判断栈S为满栈时返回值为真, 反之为假
int IsFullOperand(SeqStackOperand *S)
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
//运算符栈入栈函数
int Push(SeqStack *S, char x)
{
if (S->top==Stack_Size-1)
{
printf(“Stack is full!\n”);
return FALSE;
}
else
{
S->top++;
S->elem[S->top]=x;
return TRUE;
}
}
//运算数栈 入栈函数
int PushOperand(SeqStackOperand *S, int x)
{
if (S->top==Stack_Size-1)
{
printf(“Stack is full!\n”);
return FALSE;
}
else
{
S->top++;
S->elem[S->top]=x;
return TRUE;
}
}
//运算符栈 出栈函数
int Pop(SeqStack *S, char *x)
{
if (S->top==-1)
{
printf(“运算符栈空!\n”);
return FALSE;
}
else
{
*x=S->elem[S->top];
S->top–;
return TRUE;
}
}
//运算数栈 出栈函数
int PopOperand(SeqStackOperand *S, int *x)
{
if (S->top==-1)
{
printf(“运算符栈空!\n”);
return FALSE;
}
else
{
*x=S->elem[S->top];
S->top–;
return TRUE;
}
}
//运算符栈取栈顶元素函数
char GetTop(SeqStack *S)
{
if (S->top ==-1)
{
printf(“运算符栈为空!\n”);
return FALSE;
}
else
{
return (S->elem[S->top]);
}
}
//运算数栈取栈顶元素函数
int GetTopOperand(SeqStackOperand *S)
{
if (S->top ==-1)
{
printf(“运算符栈为空!\n”);
return FALSE;
}
else
{
return (S->elem[S->top]);
}
}
//判断输入字符是否为运算符函数, 是返回TRUE, 不是返回FALSE
int Isoperator(char ch)
{
int i;
for (i=0;i<7;i++)
{
if(ch==ops
return TRUE;
}
return FALSE;
}
//比较运算符优先级函数
char Compare(char ch1, char ch2)
{
int i,m,n;
char pri;
int priority;
for(i=0;i<7;i++) //找到相比较的两个运算符在比较矩阵里的相对位置
{
if(ch1==ops
m=i;
if (ch2==ops
n=i;
}
priority = cmp[m][n];
switch(priority)
{
case 1:
pri=’<’;
break;
case 2:
pri=’>’;
break;
case 3:
pri=’=';
break;
case 0:
pri=’$';
printf(“表达式错误!\n”);
break;
}
return pri;
}
//运算函数
int Execute(int a, char op, int b)
{
int result;
switch(op)
{
case ‘+’:
result=a+b;
break;
case ‘-’:
result=a-b;
break;
case ‘*’:
result=a*b;
break;
case ‘/’:
result=a/b;
break;
}
return result;
}
/*读入一个简单算术表达式并计算其值. operator和operand分别为运算符栈和运
算数栈, OPS为运算符集合*/
int ExpEvaluation()
{
int a,b,v,temp;
char ch,op;
char *str;
int i=0;
SeqStack operator;
SeqStackOperand operand;
InitStack(&operator);
InitStacknOperand(&operand);
Push(&operator,’#');
printf(“请输入表达式(以#结束):\n”); //表达式输入
str =(char *)malloc(50*sizeof(char));
gets(str);
ch=str
i++;
while(ch!=’#’ || GetTop(&operator)!=’#')
{
if(!Isoperator(ch))
{
temp=ch-’0′; //将字符转换为十进制数
ch=str
i++;
while(!Isoperator(ch))
{
temp=temp*10 + ch-’0′; //将逐个读入运算数的各位转化为十进制数
ch=str
i++;
}
PushOperand(&operand,temp);
}
else
{
switch(Compare(GetTop(&operator),ch))
{
case ‘<’:
Push(&operator,ch);
ch=str
i++;
break;
case ‘=’:
Pop(&operator,&op);
ch=str
i++;
break;
case ‘>’:
Pop(&operator,&op);
PopOperand(&operand,&b);
PopOperand(&operand,&a);
v=Execute(a,op,b); //对a和b进行op运算
PushOperand(&operand,v);
break;
}
}
}//end while
v=GetTopOperand(&operand);
return v;
}
void main()
{
int result;
char mid=’y';
while(mid==’y')
{
result=ExpEvaluation();
printf(“\n表达式结果是%d\n”,result);
printf(“do you want to play again y or n\n”);
mid=getchar();
getchar();//抵消回车号
}
printf(“this is made my Freeze,thank you for your use!\n”);
}
>> 本文固定链接: http://www.vcgood.com/archives/3241
buxing
看一下^^