您的位置:首页 > 其它

文章标题

2015-09-09 20:35 323 查看
逆波兰法求算术运算结果(PS:只能求+-*/四则运算)

1)首先是中缀转后缀

遇到数字输出,遇到符号入栈,若要入栈的符号的优先级小于栈顶符号,则栈中优先级大于该符号的所有符号都出栈,然后该符号入栈。

2)后缀表达式求值

遇到数字入栈,入到符号,则将相应的两个数出栈进行计算

RPN.c

#include<stdio.h>
#include<stdlib.h>
#include"linknode.h"
#include<string.h>

void main()
{
int brace;
int result;
int len,i,j;//数组长度,计数器
char equ[MAXSIZE] ="9+2*(6-9)+3";
char infexa[MAXSIZE]={'\0'};//中缀数组
printf("the origin expression is:\n",equ);

strcpy(infexa,convert(equ));//后缀转中缀

printf("infex expression is:%s\n",infexa);

//后缀计算表达式
result = calculate(infexa);
printf("the result is:%d\n",result);
system("pause");
}


linknode.h

#define MAXSIZE 20
#define datatype char//预编译不用;
#define format "%c,%p,%p\n"

typedef struct stacknode//定义struct stacknode 类型 结点的结构体
{
datatype num;//存放字符型数据(数字和符号)
struct stacknode * next;
}stacknode,*stacknodep;

typedef struct stack
{
stacknodep top;//指向栈顶
int count;//计数
}stack,*stackp;

void init(stackp head);//初值化栈

int isempty(stackp head);//检查栈是否为空

void push(stackp head,datatype data);//将数据压入栈中

void pop(stackp head,datatype * datap);//出栈

void showstack(stackp head);//显示栈

int isdigit(char input);//判断是否为数字

char * convert(char *  equ);//中缀转后缀

int calculate(char *infexa);//后缀表达式,计算结果


linknode.c

#include"linknode.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void init(stackp head)//初值化栈
{
head->top = NULL;//指向0
head->count =0;//表示没有元素
}

int isempty(stackp head)//判断栈是否为空
{
if(head->top == NULL&& head ->count == 0)
{
return 0;//空栈
}
else
return 1;
}

void push(stackp head,datatype data)//将数据压入栈中
{
stacknodep  newnode;
if(head->count==MAXSIZE)//满栈
{
printf("the stack is full\n");
return;
}
else if(head->count==0)
{
newnode = (stacknodep)malloc(sizeof(stacknode));//开辟空间,创建结点
newnode->num = data;//初始化结点
newnode->next = NULL;
head->top = newnode;//指向栈顶
head->count++;
return;
}
else
{
newnode = (stacknodep)malloc(sizeof(stacknode));
newnode->num = data;//初值化结点
newnode->next = head->top;//建立链接关系
head->top = newnode;//指向栈顶
head->count++;
return;
}

}

void showstack(stackp head)//显示栈
{
stack label1;
label1.top = head->top;
label1.count = head->count;
if (label1.top==NULL&&label1.count==0)
{
return;
}
else
{
while(label1.top!=NULL)
{
printf(format,label1.top->num,label1.top,label1.top->next);
label1.top = label1.top->next;//向栈底推进
label1.count--;
}
//return;
}
}

void pop(stackp head,datatype * datap)//出栈
{
stacknodep p1;
if (head->top==NULL&&head->count==0)
{
printf("the stack has been empty\n\n");
return;
}
else
{
*datap = head->top->num;//传递出栈数据
p1 = head->top;
head->top = head->top->next;
free(p1);
head->count--;
}
}

int isdigit(char input)//判断是否为数字
{
if(input>='0'&&input<='9')
{
return 1;
}
else
return 0;

}

char * convert(char *  equ)//中缀转后缀
{

int brace;//记录'('的个数
int len,i,j;//数组长度,计数器
char infexa[MAXSIZE]={'\0'};//中缀数组
stack sign,temp;
stackp signp;

signp = & sign;

init(signp);//初值化栈
len =strlen(equ);

j=0;
brace =0;
for(i=0;i<len;i++)
{
if(isdigit(equ[i]))
{
infexa[j]=equ[i];//遇到数字输出数字
j++;
}
else
{
if(signp->top==NULL)//空栈
{
push(signp,equ[i]);//符号进栈
}
else if(equ[i]=='(')
{
push(signp,equ[i]);//'('入栈
brace++;//记录左括号的个数
}
else if (brace>=1)//有多于一个
{
if (equ[i]==')')
{

while(signp->top->num!='(')//输出到'('
{
pop(signp,&infexa[j]);
j++;
}
temp.top = signp->top;//删除‘)’
signp->top=signp->top->next;
free(temp.top);
brace--;
}
else
{
push(signp,equ[i]);
}
}
else
{
if (equ[i]=='+'||equ[i]=='-')
{

while(signp->top!=NULL)
{
pop(signp,&infexa[j]);
j++;
}
push(signp,equ[i]);

}

else if(equ[i]=='*'||equ[i]=='/')
{
if (signp->top->num=='+'||signp->top->num=='-')//若栈顶为低优先级符号
{
push(signp,equ[i]);
}
else
{
while(signp->top->num=='*'||signp->top->num=='/')
{
pop(signp,&infexa[j]);
j++;
}
push(signp,equ[i]);
}
}
}

}

}
pop(signp,&infexa[j]);//对输入完成扫描后输出最后的符号
return infexa;
}

int calculate(char *infexa)//后缀表达式,计算结果
{
int len,i,j;//数组长度,计数器
int num1,num2,num3;
int temp[MAXSIZE];//缓存中间结果

len =strlen(infexa);
j=0;
for(i=0;i<len;i++)//扫描后缀表达式数组
{
if(isdigit(infexa[i]))
{
temp[j]=infexa[i]-48;
j++;
}
else
{
switch(infexa[i])
{
case '+':
{
num1 = temp[--j];
num2 = temp[--j];
num3 = num2 + num1;
temp[j] = num3;
j++;
break;
}
case '-':
{
num1 = temp[--j];
num2 = temp[--j];
num3 = num2 - num1;
temp[j] = num3;
j++;
break;
}
case '*':
{
num1 = temp[--j];
num2 = temp[--j];
num3 = num2 * num1;
temp[j] = num3;
j++;
break;
}
case '/':
{
num1 = temp[--j];
num2 = temp[--j];
num3 = num2 / num1;
temp[j] = num3;
j++;
break;
}
}
}
}
return temp[0];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: