#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#define MAX 15
#define bool int
typedef struct stack
{
int top;
char cap[MAX];
} *Stack;
Stack initStack()
{
Stack s = (Stack)malloc(sizeof(Stack));
if(s==0)
{
printf("no enough room!/n");
exit(0);
}
s->top=-1;
return s;
}
void destory(Stack s)
{
free(s);
}
bool isEmpty(Stack s)
{
if(s->top==-1)return 1;
return 0;
}
bool isFull(Stack s)
{
if(s->top==MAX-1)return 1;
return 0;
}
void push(Stack s, char c)
{
if(s->top!=MAX-1)
{
s->cap[++(s->top)]=c;
return;
}
printf("stack full!/n");
system("pause");
}
void pop(Stack s)
{
if(s->top!=-1)
{
s->top -= 1;
return;
}
printf("stack empty!/n");
system("pause");
}
char top(Stack s)
{
if(s->top!=-1)
{
return (s->cap)[s->top];
}
printf("stack no element!/n");
return '/0';
}
void match(Stack s, char c)
{ //一个匹配括号的函数
switch(c)
{
case '{':
case '(':
case '[':push(s,c);break;
case '}':if(top(s)=='{'){pop(s);break;}else{printf("wrong matching!/n");system("pause");}
case ')':if(top(s)=='('){pop(s);break;}else{printf("wrong matching!/n");system("pause");}
case ']':if(top(s)=='['){pop(s);break;}else{printf("wrong matching!/n");system("pause");}
default :;
}
}
typedef struct stack_int
{
int cap[MAX];
int top;
} *Stack_int;
Stack_int initStack_int()
{
Stack_int s = (Stack_int)malloc(sizeof(Stack_int));
if(s==0)
{
printf("no enough room!/n");
exit(0);
}
s->top=-1;
return s;
}
void pop_int(Stack_int s)
{
if(s->top!=-1)
{
s->top -= 1;
return;
}
printf("stack empty!/n");
system("pause");
}
void destory_int(Stack_int s)
{
free(s);
}
void push_int(Stack_int s, int c)
{
if(s->top!=MAX-1)
{
s->cap[++(s->top)]=c;
return;
}
printf("stack full!/n");
system("pause");
}
int top_int(Stack_int s)
{
if(s->top!=-1)
{
return (s->cap)[s->top];
}
printf("stack no element!/n");
return 0;
}
int power(int n1,int n2)
{
int i,rs;
for(i=0,rs=1;i<n2;i++)
{
rs*=n1;
}
return rs;
}
int compute(int n1,char c,int n2)
{
switch(c)
{
case '+':return n1+n2;
case '-':return n1-n2;
case '*':return n1*n2;
case '/':
if(n2!=0)return n1/n2;
else{
printf("0 can not be devided!/n");
system("pause");
}
case '^':return power(n1,n2);
case '%':return n1%n2;
}
}
void computer()
{
Stack s_oper = initStack();
Stack_int s_num = initStack_int();
char a,temp;
char str[10] = {'/0'};
int i=0,n1,n2,result;
bool one=0;
printf("please input your string to compute!/n");
a=getchar();
while(a!='/n'&&a!='=')
{
//match(s_oper,a);
if(a>='0'&&a<='9')
{
if(one)
{
pop_int(s_num);
str[i++]=a;
str[i]='/0';
push_int(s_num,atoi(str));
}else
{
one = 1;
str[i++]=a;
str[i]='/0';
push_int(s_num,atoi(str));
}
}else
{
if(one)
{ one=0; i=0;}
switch(a)
{
case '+':
case '-':
if(isEmpty(s_oper)||top(s_oper)=='(')
{
push(s_oper,a);
break;
}else
{
while(!(isEmpty(s_oper)||top(s_oper)=='('))
{
temp=top(s_oper);
pop(s_oper);
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n1=top_int(s_num);
pop_int(s_num);
}
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n2=top_int(s_num);
pop_int(s_num);
}
push_int(s_num,compute(n2,temp,n1));
}
push(s_oper,a);
break;
}
case '*':
case '/':
case '^':
case '%':
if(isEmpty(s_oper)||top(s_oper)=='+'||top(s_oper)=='-'||top(s_oper)=='(')
{
push(s_oper,a);
break;
}else
{
while(!(isEmpty(s_oper)||top(s_oper)=='+'||top(s_oper)=='-'||top(s_oper)=='('))
{
temp=top(s_oper);
pop(s_oper);
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n1=top_int(s_num);
pop_int(s_num);
}
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n2=top_int(s_num);
pop_int(s_num);
}
push_int(s_num,compute(n2,temp,n1));
}
push(s_oper,a);
break;
}
case '(':
push(s_oper,a);break;
case ')':
{
while(top(s_oper)!='(')
{
temp=top(s_oper);
pop(s_oper);
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n1=top_int(s_num);
pop_int(s_num);
}
if(s_num->top == -1)
{
printf("wrong input!/n");
system("pause");
}else
{
n2=top_int(s_num);
pop_int(s_num);
}
push_int(s_num,compute(n2,temp,n1));
}
pop(s_oper);
break;
}
default :
{
printf("wrong input!/n");
system("pause");
}
}
}
a=getchar();
}
while(!isEmpty(s_oper))
{
n1=top_int(s_num);
pop_int(s_num);
n2=top_int(s_num);
pop_int(s_num);
temp=top(s_oper);
pop(s_oper);
push_int(s_num,compute(n2,temp,n1));
}
result = top_int(s_num);
pop_int(s_num);
if(s_num->top != -1)
{
printf("wrong input!/n");
system("pause");
}
printf("the result is %d! /n",result);
//free(s_oper);
//free(s_num);
//destory(s_oper);
//destory_int(s_num);
}
int main()
{
computer();
}
//这个计算器是在学完栈以后写的,内容比较简单,标准C程序。
//但是调试起来并不是很轻松,就几个小的错误竟然弄得很郁闷。
//其中一处是在match();哪里,这是原来测试栈用的,后来异想天开向一边测试一下括号匹配,结果好不容易找到
//还有str数组本来就是一个临时存储空间用于转化为数字,不过刚开始str[i]='/0';里面的i写成了i++,s所以很郁闷。呵呵
//幸亏有这个错误,让我学会了debug。
//还是有收获的!所以记录一下!
//最后想请高手帮忙解决一个问题,就是最后注释的空间释放的部分,如果不注释掉每次执行到此处都会发生错误,真不知道是为什么。
//希望高手能够指点,谢谢了!