以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char data1_t; // 重定义char类型和int类型
typedef int data2_t;
typedef struct node1{
data1_t data;
struct node1 *next; // 定义一个栈区结构体,用来存放操作符
}stack1_t;
typedef struct node{
data2_t data;
struct node *next; // 定义一个栈区结构体,用来存放数字
}stack2_t;
stack1_t *symbol_tCreate() // 创建符号栈
{
stack1_t *top=(stack1_t *)malloc(sizeof(stack1_t));
if(top == NULL)
{
return NULL;
}
top->data = -1;
top->next = NULL;
return top;
}
stack2_t *number_tCreate()//创建数字栈
{
stack2_t *top=(stack2_t *)malloc(sizeof(stack2_t));
if(top == NULL)
{
return NULL;
}
top->data = -1;
top->next = NULL;
return top;
}
int stack1IsEmpty(stack1_t *top) // 判空
{
if(top == NULL){
return -1;
}
return top->next ==NULL;
}
int stack2IsEmpty(stack2_t *top) // 判空
{
if(top == NULL){
return -1;
}
return top->next ==NULL;
}
char stack1Enter(stack1_t *top,data1_t data) //入栈操作
{
if(NULL == top)
{
return -1;
}
stack1_t *new =(stack1_t *)malloc(sizeof(stack1_t));
if(NULL == new)
{
return -1;
}
new->data = data;
new->next = top->next;
top->next = new;
return 0;
}
int stack2Enter(stack2_t *top,data2_t data)// 入栈
{
if(NULL == top)
{
return -1;
}
stack2_t *new =(stack2_t *)malloc(sizeof(stack2_t));
if(NULL == new)
{
return -1;
}
new->data = data;
new->next = top->next;
top->next = new;
return 0;
}
data1_t stack1Out(stack1_t *top) //出栈操作
{
if(NULL == top)
{
return (data1_t)-1;
}
if(stack1IsEmpty(top))
{
return (data1_t)-1;
}
stack1_t *p=top->next;
data1_t temp =p->data;
top->next=p->next;
free(p);
return temp;
}
data2_t stack2Out(stack2_t *top)
{
if(NULL == top)
{
return (data2_t)-1;
}
if(stack2IsEmpty(top))
{
return (data2_t)-1;
}
stack2_t *p=top->next;
data2_t temp =p->data;
top->next=p->next;
free(p);
return temp;
}
int calculator(char math[50]) //实现计算器功能的函数
{
stack1_t *top1= symbol_tCreate();
stack2_t *top2= number_tCreate();
char *p =math;
int len= strlen(math);
for(int i=0;i<len;i++)
{
if(p[i]>=48 && p[i]<=57)
{
stack2Enter(top2,(p[i]-48));
}
else
{
stack1Enter(top1,(data1_t)p[i]);
}
}
stack1_t *sy =symbol_tCreate();
stack2_t *num =number_tCreate();
data1_t s=0;
data2_t n=0;
while(1)
{
s=stack1Out(top1);
if(s == '-'||s == '+'){
stack1Enter(sy,s);
stack2Enter(num,stack2Out(top2));
}
else if(s == '*'){
data2_t num1 = stack2Out(top2);
data2_t num2 = stack2Out(top2);
stack2Enter(top2,num1*num2);
}
else if(s == '/'){
data2_t num3 = stack2Out(top2);
data2_t num4 = stack2Out(top2);
stack2Enter(top2,num4/num3);
}
if(stack1IsEmpty(top1))
{
break;
}
}
while(!stack2IsEmpty(top2))
{
stack2Enter(num,stack2Out(top2));
}
while(!stack1IsEmpty(sy))
{
s=stack1Out(sy);
if(s == '+'){
data2_t num5 = stack2Out(num);
data2_t num6 = stack2Out(num);
stack2Enter(num,num5+num6);
}
else if(s == '-'){
data2_t num7 = stack2Out(num);
data2_t num8 = stack2Out(num);
stack2Enter(num,num7-num8);
}
}
data2_t rel =stack2Out(num);
return rel;
}
int main(int argc, char *argv[])//最后主函数实现功能
{
char cal[50]={0};
gets(cal);
printf("结果为:%d\n",calculator(cal));
return 0;
}
这是一个用链式栈实现的计算器,能实现基本的加减乘除,还有一些地方也还需要完善,比如无法计算括号操作,还有就是只能操作正整数。
具体实现的过程:
创建符号栈1和数字栈1,然后把输入式子内的符号和数字分别放入符号栈1和数字栈1
创建符号栈2和数字栈2.
然后符号栈1不停的出栈并判断,如果是加减,就入栈到符号栈2,数字栈1也弹出一个数字放入数字栈2;如果是乘除,数字栈1就弹出两个数字进行计算然后将结果放入数字栈1
第三步的操作一直进行到符号栈为空就结束,并将数字栈1 内的数字全弹出放入数字栈2。
此时符号栈2就只剩加减运算了,于是符号栈2不停弹出符号并判断进行运算,结果放入数字栈2
最后数字栈2弹出结果。