当前位置:首页 » 编程语言 » c语言栈的表达式求值

c语言栈的表达式求值

发布时间: 2025-03-28 22:12:04

A. C语言关于表达式求值

这是我以前做的一个表达式求值的程序,要求和实现的功能是一样的:
#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]={'+','-','*','/','(',')','#'};
int PriorityTable[7][7]={{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, NO},
{ 1, 1, 1, 1,NO, 1, 1},
{-1,-1,-1,-1,-1,NO, 0}};
int menu(void);
void InputExpression(char str[])
{
int len;
printf("Input expression string:\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}
int GetCharType(char ch)
{
int i;
for(i=0;i<NUM;i++) if(ch==a[i]) return(i);
if(ch>='0' && ch<='9') return(DIGIT);
if(ch=='.') return(POINT);
return(-1);
}
double Operate(double a,int theta,double b)
{
double x;
switch(theta)
{
case 0:x=a+b;break;
case 1:x=a-b;break;
case 2:x=a*b;break;
case 3:x=a/b;break;
}
return (x);
}
int EXCUTE(char *str,double *Result)
{
int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE];
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))
{
CharType=GetCharType(str[pp]);
switch(CharType)
{
case -1:
return(0);
case DIGIT:
number=0;
while(str[pp]>='0' && str[pp]<='9')
{
number=number*10+(str[pp]-48);
pp++;
}
if(str[pp]=='.')
{
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{
number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
}
OPND[topNd]=number;
topNd++;
break;
case POINT:
number=0;
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')
{
number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
OPND[topNd]=number;
topNd++;
break;
case PLUS:
case MINUS:
case POWER:
case DIVIDE:
if(PriorityTable[OPTR[topTr-1]][CharType]==-1)
{
OPTR[topTr]=CharType;
topTr++;
pp++;
}
else
{
OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;
}
break;
case LEFTP:
OPTR[topTr]=CharType;
topTr++;
pp++;
break;
case RIGHP:
while(OPTR[topTr-1]!=LEFTP)
{
if(OPTR[topTr-1]==STARTEND)return(0);
if(PriorityTable[OPTR[topTr-1]][CharType]==1)
{
OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;
}
else
break;
}
topTr--;
pp++;
break;
case STARTEND:
while(OPTR[topTr-1]!=STARTEND)
{
OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;
}
if(topNd==1)
{
*Result=OPND[0];
return(1);
}
else
return(0);
}
}
return(1);
}
void main()
{ int num,flag;
double result;
char str[256];
str[0]='0';
while(1)
{ num=menu();
switch(num)
{
case 1:
InputExpression(str);
flag=0;
printf("%s\n",str);
getchar();
break;
case 2:
if(str[0]=='0')
{ printf("Expression is Empty!");
getchar();
break;
}
if(!EXCUTE(str,&result))
{ printf("The expression has error!\n");
getchar();
}
else
{ printf("calulation has finished!\n");
getchar();
flag=1;
}
break;
case 3:
if(flag)
{ printf("#%s=%lf\n",str,result);
getchar();
}
break;
case 4:
break;
}
if(num==4) break;
}
}
int menu(void)
{
int num;
printf("%20c1--input expression\n",' ');
printf("%20c2--calculation expression\n",' ');
printf("%20c3--print result\n",' ');
printf("%20c4--Quit\n",' ');
printf(" please select 1,2,3,4:");
do
{
scanf("%d",&num);
}while(num<1 || num>4);
return(num);
}

B. C语言 任意表达式求值。(栈的应用

/*** 只适合整数的表达式求值 ***/
/***其中部分可作修改,表达式也可是输入的***/
#include "iostream.h"
const int n0=30;
int s1[n0+1]; //操作数栈
char s2[n0+1]; //运算符栈
int t1,t2;
int num[4]; //提取表达式中的整数

void calcu() //一次计算
{
int x1,x2,x;
char p;
//弹出一个运算符
p=s2[t2--];
//弹出两个操作数
x2=s1[t1--];
x1=s1[t1--];
//进行一次运算
switch(p) {
case '+':x=x1+x2;break;
case '-':x=x1-x2;break;
case '*':x=x1*x2;break;
case '/':x=x1/x2;
}
//结果压入操作数栈
s1[++t1]=x;
}

int calculator(char *f)
{
int v,i=0;
char *p=f;
t1=t2=0; //设置空栈
while (*p!='\0')
switch(*p) {
case '+': case '-':
while (t2&&(s2[t2]!='('))
//执行先遇到的加、减、乘、除运算
calcu();
//当前运算符进栈
s2[++t2]=*p;
//读下一个字符
p++;
break;
case '*': case '/':
if (t2&&(s2[t2]=='*')||(s2[t2]=='/'))
//执行先遇到的乘、除运算
calcu();
//当前运算符进栈
s2[++t2]=*p;
//读下一个字符
p++;
break;
case '(':
//左括号进栈
s2[++t2]=*p;
//读下一个字符
p++;
break;
case ')':
while (s2[t2]!='(')
//执行括号内的加、减、乘、除运算
calcu();
//弹出左括号
t2--;
//读下一个字符
p++;
break;
default:
//把字符串转换成整数值
v=0;
do {
v=10*v+*p-'0';
p++;
} while((*p>='0')&&(*p<='9'));
//操作数进栈
s1[++t1]=v;
num[i++]=v;
};
//执行先遇到的加、减、乘、除运算
while (t2) calcu();
//返回结果
return s1[t1];
}

void main()
{
char a[]="5*(40+6)-39";
cout<<calculator(a)<<endl;
cout<<"其中的数字为:\n";
for (int i=0;i<4;i++)
{
cout<<num[i]<<' ';
}
cout<<endl;
}
原来做过的东西,是C++的,VC++6.0环境下调试运行成功。

热点内容
oppo手机为什么不能用谷歌服务器 发布:2025-03-31 17:12:31 浏览:212
sql条件判断if 发布:2025-03-31 17:03:27 浏览:667
网银密码怎么找回 发布:2025-03-31 16:59:17 浏览:480
goc的编译运行的快捷键是什么 发布:2025-03-31 16:58:19 浏览:442
技能触发脚本 发布:2025-03-31 16:58:14 浏览:704
如何知道自己安卓的具体版本 发布:2025-03-31 16:39:37 浏览:895
杂牌电脑怎么查看配置 发布:2025-03-31 16:27:34 浏览:199
linux27 发布:2025-03-31 16:26:51 浏览:150
个人电脑怎么搭建邮政服务器 发布:2025-03-31 16:22:33 浏览:692
安卓短信铃声在哪里设置 发布:2025-03-31 16:22:28 浏览:725