简易计算器
//头函数
#ifndef __1JISUANQI_H__
#define __1JISUANQI_H__
#define TRUE 1
#define FLASE 0
#define SIZE 100
typedef struct _numstack
{
float data;
struct _numstack *next;
}NumNode;
typedef struct _atorstack
{
char data;
struct _atorstack *next;
}AtorNode;
typedef struct _liststack
{
NumNode *topnum;
AtorNode *topator;
}LinkStack;
//创建头节点
LinkStack *creat_list();
//求结果
int Result(int x, int y, char *ch);
//判断字符栈空
int TopatorEmpty(LinkStack *s);
//数字进栈
int numpush (LinkStack *s, int x);
//字符进栈
int strpush (LinkStack *s, char x);
//置空栈
int StackEmpty(LinkStack *s);
//数字出栈
int numPop (LinkStack *s, int *x);
//字符出栈
int strPop (LinkStack *s, char *x);
//取栈顶元素
int GetTop (LinkStack *s, char *ch);
//取栈顶数字
int GetTopnum (LinkStack *s, int x);
//运算函数
void computation(LinkStack *s, char *ch);
#endif //__1JISUANQI_H__
//主要代码
#include "1jisuanqi.h"
#include <stdlib.h>
LinkStack *creat_list()
{
LinkStack *s = (LinkStack *)malloc(sizeof(LinkStack)/sizeof(char));
if(s == NULL)
{
return NULL;
}
return s;
}
int StackEmpty(LinkStack *s)
{
if(s == NULL)
{
return FLASE;
}
s->topator = NULL;
s->topnum = NULL;
return TRUE;
}
int TopatorEmpty(LinkStack *s)
{
if(s == NULL)
{
return FLASE;
}
return s->topator == NULL;
}
int numpush (LinkStack *s, int x)
{
if(s == NULL)
{
return FLASE;
}
NumNode *node = (NumNode *)malloc(sizeof(NumNode)/sizeof(char));
if(node == NULL)
{
return FLASE;
}
node->data = x;
node->next = s->topnum;
s->topnum = node;
return TRUE;
}
int strpush (LinkStack *s, char x)
{
if(s == NULL)
{
return FLASE;
}
AtorNode *node = (AtorNode *)malloc(sizeof(AtorNode)/sizeof(char));
if(node == NULL)
{
return FLASE;
}
node->data = x;
node->next = s->topator;
s->topator = node;
return TRUE;
}
int numPop (LinkStack *s, int *x)
{
if(s == NULL)
{
return FLASE;
}
if(s->topnum == NULL)
{
return FLASE;
}
NumNode *p = s->topnum;
*x = p->data;
s->topnum = p->next;
free(p);
return TRUE;
}
int strPop (LinkStack *s, char *x)
{
if(s == NULL)
{
return FLASE;
}
if(TopatorEmpty(s))
{
return FLASE;
}
AtorNode *p = s->topator;
*x = p->data;
s->topator = p->next;
free(p);
return TRUE;
}
int GetTop (LinkStack *s, char *ch)
{
if(s == NULL)
{
return FLASE;
}
if(TopatorEmpty(s))
{
return FLASE;
}
*ch = s->topator->data;
return TRUE;
}
int Result(int x, int y, char *ch)
{
switch(*ch)
{
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
default :
break;
}
return FLASE;
}
int GetTopnum (LinkStack *s, int x)
{
if(s == NULL)
{
return FLASE;
}
if(s->topnum == NULL)
{
return FLASE;
}
x = s->topnum->data;
return x;
}
void computation(LinkStack *s, char *ch)
{
int x,y,result;
numPop(s,&y);
numPop(s,&x);
strPop(s,ch);
result = Result(x, y, ch);
numpush(s, result);
GetTop(s,ch);
}
//主函数
#include "1jisuanqi.h"
#include <stdio.h>
#include <string.h>
int main()
{
LinkStack *s = creat_list();
char str[SIZE];
char num[10];
int i = 0;
int j = 0;
int result = 0;
StackEmpty(s);
printf("请输入表达式:\n",str);
scanf("%s",str);
char ch = 0;
while(str[i])
{
if(str[i] >= '0' && str[i] <= '9')
{
num[j++] = str[i];
if(str[i+1] == '\0')
{
int z;
num[j] = '\0';
z = atoi(num);
numpush(s, z);
j = 0;
}
}
else if(j != 0)
{
int z;
num[j] = '\0';
z = atoi(num);
numpush(s, z);
j = 0;
}
switch(str[i])
{
case '+':
if(GetTop(s,&ch) == FLASE || ch == '(')
{
strpush (s, str[i]);
}
else
{
while( GetTop(s,&ch) != FLASE && ch != '(')
{
computation(s,&ch);
}
strpush (s, str[i]);
}
break;
case '-':
if(GetTop(s,&ch) == FLASE || ch == '(')
{
strpush (s, str[i]);
}
else
{
while( GetTop(s,&ch) != FLASE && ch != '(')
{
computation(s,&ch);
}
strpush (s, str[i]);
}
break;
case '*':
if(GetTop(s,&ch) == FLASE || ch != '/' && ch != '*')
{
strpush(s, str[i]);
}
else
{
while( ch == '/' || ch == '*')
{
computation(s,&ch);
}
strpush(s, str[i]);
}
break;
case '/':
if(GetTop(s,&ch) == FLASE || ch != '/' && ch != '*')
{
strpush(s, str[i]);
}
else
{
while( ch == '/' || ch == '*')
{
computation(s,&ch);
}
strpush(s, str[i]);
}
break;
case '(':
strpush (s, str[i]);
break;
case ')':
GetTop(s,&ch);
while(ch != '(')
{
computation(s,&ch);
}
strPop(s,&ch);
break;
default:
break;
}
i++;
}
while(GetTop(s,&ch) != FLASE)
{
computation(s,&ch);
}
printf("result = %d\n",GetTopnum(s,result));
return 0;
}