较高级算术表达式

#include <stdio.h>
#include <math.h>
#include <string>                                       /*引用pow,计算a^b*/

struct{
char ch[50];
int top,length;
}str;                                                             /*字符串栈*/
struct{
char ch[30];
int top;
}code;                                                            /*操作符栈*/
struct{
double num[30];
int top;
}number;                                                           /*操作数栈*/

void Getnumber()                             /*读取一个浮点数到number.num[]中*/
{float t,result=0;
int i=0,j;
while(str.top<str.length&&str.ch[str.top]>='0'&&str.ch[str.top]<='9')
result=result*10+str.ch[str.top++]-'0';//"str.ch[str.top++]-'0'"实现字符串与起对应的整型之间的转换
if(str.top<str.length&&str.ch[str.top]=='.'&&++str.top)
while(str.top<str.length&&str.ch[str.top]>='0'&&str.ch[str.top]<='9')
{i++,t=str.ch[str.top++]-'0';//"str.ch[str.top++]-'0'"实现字符串与起对应的整型之间的转换
for(j=0;j<i;j++)
t=t/10.0;
result=result+t;
}
number.num[++number.top]=result;
}

float Solve(float a,float b,char ch)               /*计算并返回表达式'a ch b'*/
{switch(ch)
{case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
case '^': return pow(a,b);                                      /*计算a^b*/
}
}

void Pop(void)            /*当遇到优先级低的操作码时,从操作码栈弹出一操作码,*/
{if(number.top<1)         /*从操作数栈弹出两个操作数,计算其值后压入操作数栈 */
printf("输入的表达式有误 !/n"),exit(0);
else
number.num[number.top-1]=Solve(number.num[number.top-1],number.num[number.top],
code.ch[code.top--]),number.top--;
}

main()
{printf("请输入表达式:");
scanf("%s",str.ch);
str.length=strlen(str.ch),str.top=0,code.top=number.top=-1;          /*初始化*/
while(str.top<str.length)                /*处理整个字符串,一边处理,一边判错*/
switch(str.ch[str.top])
{case '+':
case '-':
while(code.top>=0&&code.ch[code.top]!='(')
Pop();
code.ch[++code.top]=str.ch[str.top++];
break;
case '*':
case '/':
while(code.top>=0&&code.ch[code.top]!='('&&code.ch[code.top]!='+'&&code.ch[code.top]!='-')
Pop();
code.ch[++code.top]=str.ch[str.top++];
break;
case '(':
code.ch[++code.top]=str.ch[str.top++];
break;
case ')':
while(code.top>=0&&code.ch[code.top]!='(')
Pop();
if(code.top<0)
printf("输入的表达式有误 !/n"),exit(0);
else
code.top--;
str.top++;
break;
case '^':
code.ch[++code.top]=str.ch[str.top++];
break;
default:
if(str.ch[str.top]<'0'&&str.ch[str.top]!='.'||str.ch[str.top]>'9')
printf("输入的字符有误!/n"),exit(0);
else
Getnumber();
}
while(code.top>=0)                      /*处理剩余操作码,优先级由高到低(栈序)*/
if(code.ch[code.top]=='(')
printf("输入的表达式有误 !/n"),exit(0);
else if(code.ch[code.top]=='+'||code.ch[code.top]=='-'||code.ch[code.top]=='*'||
code.ch[code.top]=='/'||code.ch[code.top]=='^')
Pop();
if(code.top>1)                                       /*操作数多于操作码的匹配数*/
printf("输入的表达式有误!/n");
else                                                                 /*输出结果*/
printf("The result is:%f/n",number.num[number.top]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值