这个程序自己写的有些局限,可惜只能对0~9的四则运算起作用,我正在对它进行改进,希望各位能提出好的意见,谢谢!!
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#define maxism 15
typedef struct stack
{
char stack[maxism];
int top;
}stack;
stack *L;
char after[maxism];
void initialstack(stack *T)
{
T->top=0;
}
void push(stack *T,int x)
{
T->top++;
T->stack[L->top]=x;
}
void change()
{
int i=0,j,m=0;
char str[maxism],ch;
printf(“please enter the compute expression(only contained +,-,*,/,(,),begin and end with ‘#’):\n”);
gets(str);
i++;
ch=str;
initialstack(L);
while(ch!=’#')
{
switch(ch)
{
case’(‘:
L->top++;
L->stack[L->top]=ch;
break;
case’)':
while(L->stack[L->top]!=’(‘)
{
after[m]=L->stack[L->top];
L->top–;
m++;
}
L->top–;
break;
case’+':
case’-':
while(L->top!=0&&L->stack[L->top]!=’(‘)
{
after[m]=L->stack[L->top];
L->top–;
m++;
}
L->top++;
L->stack[L->top]=ch;
break;
case’*':
case’/':
while(L->stack[L->top]==’*'||L->stack[L->top]==’/')
{
after[m]=L->stack[L->top];
L->top–;
m++;
}
L->top++;
L->stack[L->top]=ch;
break;
case’ ‘:
break;
default:
if(ch>=’0′&&ch<=’9′)
{
after[m]=ch;
m++;
}
else
{
printf(“error!\n”);
exit(1);
}
}
i++;
ch=str;
}
while(L->stack[L->top]!=0)
{
after[m]=L->stack[L->top];
m++;
L->top–;
}
printf(“the changed expression is:\n”);
for(j=0;j<m;j++)
printf(“%c”,after[j]);
after[m]=’#';
printf(“%c”,after[m]);
}
void compute(stack *L)
{
int i=0,result,d;
char ch;
initialstack(L);
ch=after;
while(ch!=’#')
{
while(ch>=’0′&&ch<=’9′)
{
d=ch-’0′;
push(L,d);
i++;
ch=after;
}
switch(ch)
{
case’+':
L->stack[L->top-1]=L->stack[L->top]+L->stack[L->top-1];
L->top–;
break;
case’-':
L->stack[L->top-1]=L->stack[L->top-1]-L->stack[L->top];
L->top–;
break;
case’*':
L->stack[L->top-1]=L->stack[L->top]*L->stack[L->top-1];
L->top–;
break;
case’/':
if(L->stack[L->top-1]==0)
{
printf(“error due to the wrong input format!\n”);
exit(0);
}
else
L->stack[L->top-1]=L->stack[L->top-1]/L->stack[L->top];
L->top–;
break;
default:
break;
}
i++;
ch=after;
}
printf(“\n the final result is:%d”,L->stack[L->top]);
}
main()
{int result;
clrscr();
change();
compute(L);
}
-
近期文章
近期评论
- coolker 发表在《打造最快的Hash表》
- struggle 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- diys 发表在《C语言编程宝典(王大刚) 1.1 C 语言的产生与发展》
文章归档
- 2022 年十月
- 2014 年一月
- 2013 年十二月
- 2012 年十一月
- 2012 年七月
- 2012 年六月
- 2012 年五月
- 2012 年四月
- 2012 年三月
- 2012 年二月
- 2011 年十二月
- 2011 年十月
- 2011 年九月
- 2011 年八月
- 2011 年七月
- 2011 年六月
- 2011 年五月
- 2011 年四月
- 2011 年三月
- 2011 年二月
- 2011 年一月
- 2010 年十二月
- 2010 年十一月
- 2010 年十月
- 2010 年九月
- 2010 年八月
- 2010 年七月
- 2010 年六月
- 2010 年五月
- 2010 年四月
- 2010 年三月
- 2010 年二月
- 2010 年一月
- 2009 年十二月
- 2009 年十一月
- 2009 年十月
- 2009 年九月
- 2009 年八月
- 2009 年七月
- 2009 年六月
- 2009 年五月
- 2009 年四月
- 2009 年三月
- 2009 年二月
- 2009 年一月
- 2008 年十二月
- 2008 年十一月
- 2008 年十月
- 2008 年九月
- 2008 年八月
- 2008 年七月
- 2008 年六月
- 2008 年五月
- 2008 年四月
- 2008 年三月
- 2008 年二月
- 2008 年一月
- 2007 年十二月
- 2007 年十一月
- 2007 年十月
- 2007 年九月
- 2007 年八月
- 2007 年七月
- 2007 年六月
- 2007 年三月
- 2007 年二月
- 2007 年一月
- 2006 年十二月
- 2006 年十一月
- 2006 年十月
- 2006 年九月
- 2006 年八月
- 2006 年七月
- 2006 年六月
- 2006 年五月
- 2006 年四月
- 2006 年三月
- 2006 年二月
- 2006 年一月
- 2005 年十二月
- 2005 年十一月
分类目录
功能