利用栈来编写计算器
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
typedef struct snode {
char data;
struct snode* next;
}snode, * linkstack;
typedef struct snode_ {
float data;
struct snode_* next;
}snode_, * linkstack_;
void init(linkstack& s);
void pushstack(linkstack& s, char x);
void popstack(linkstack& s, char& x);
void pushstack_(linkstack_& s, char x);
void popstack_(linkstack_& s, char& x);
void nifsuf(char* nif, char* suf);//将中缀表达式转为后缀式
float cal(char* suf);
void main()
{
char nif[100], suf[100];
linkstack s;
init(s);
scanf("%s", nif);
nifsuf(nif, suf);
cal(suf);
}
void init(linkstack& s)
{
s = (linkstack)malloc(sizeof(snode));
s->data = '#';
s->next = NULL;
//s = NULL;
}
void pushstack(linkstack& s, char x)
{
linkstack p = (linkstack)malloc(sizeof(snode));
p->data = x;
p->next = s;
s = p;
}
void pushstack_(linkstack_& s, float x)
{
linkstack_ p = (linkstack_)malloc(sizeof(snode_));
p->data = x;
p->next = s;
s = p;
}
void nifsuf(char* nif, char* suf)
{
int i = 0, j = 0;
char c, t;
linkstack s;
init(s);
c = nif[i];
while (c != '#')
{
while (isdigit(c) || c == '.')
{
suf[j++] = c;
c = nif[++i];
if (c < '0' || c>'9')
{
if(c != '.')
{
suf[j++] = ' ';
}
}
suf[j] = '\0';
}
//pushstack(s, '#');
if (c == '+' || c == '-')
{
if (s->data=='#')
{
pushstack(s, c);
}
else
{
popstack(s, t);
if (t == '(')
{
pushstack(s,t);
pushstack(s,c);
}
else
{
suf[j++] = t;
suf[j++] = ' ';
suf[j] = '\0';
}
}
c = nif[++i];
}
if(c == '*' || c == '/')
{
if (s->data=='#')
{
pushstack(s, c);
}
else
{
if(s->data=='*'||s->data=='/')
{
popstack(s, t);
suf[j++] = t;
suf[j++] = ' ';
suf[j] = '\0';
pushstack(s,c);
}
else{
if(s->data=='+'||s->data=='-')
{
pushstack(s,c);
}
}
}
c = nif[++i];
}
if (c == '(')
{
pushstack(s, c);
c = nif[++i];
}
if (c == ')')
{
popstack(s, t);
while (t != '(')
{
suf[j++] = t;
suf[j++] = ' ';
popstack(s, t);
}
suf[j] = '\0';
c = nif[++i];
}
}
if (c == '#')
{
while (s->next)
{
popstack(s, t);
suf[j++] = t;
suf[j++] = ' ';
}
suf[j++] = '#';
suf[j] = '\0';
free(s);
}
}
void popstack(linkstack& s, char& x)
{
linkstack p = s;
x = p->data;
if(s->next==NULL)
{
s->next = NULL;
}
else {
s = s->next;
}
free(p);
}
void popstack_(linkstack_& s, float& x)
{
linkstack_ p = s;
x = p->data;
if (s->next == NULL)
{
s->next = NULL;
}
else {
s = s->next;
}
free(p);
}
float cal(char* suf)
{
int i = 0, j = 0;
char c;
float f, r, d1, d2;
linkstack_ s;
s = (linkstack_)malloc(sizeof(snode_));
s = NULL;
char t[100];
c = suf[j++];
while (c != '#')
{
while (isdigit(c) || c == '.')
{
t[i++] = c;
t[i] = '\0';
c = suf[j++];
if (c == ' ')
{
f = atof(t);
pushstack_(s, f);
i = 0;
}
}
switch (c) {
case'+':
popstack_(s, d2);
popstack_(s, d1);
pushstack_(s, d1 + d2);
break;
case'-':
popstack_(s, d2);
popstack_(s, d1);
pushstack_(s, d1 - d2);
break;
case'*':
popstack_(s, d2);
popstack_(s, d1);
pushstack_(s, d1 * d2);
break;
case'/':
popstack_(s, d2);
if (d2 == 0)
{
printf("error");
return -1;
}
popstack_(s, d1);
pushstack_(s, d1 / d2);
break;
}
c = suf[j++];
}
popstack_(s, r);
printf("result:%f", r);
free(s);
return r;
}