expresscalculate

//本程序接受任意含有+、-、*、/和()的数值表达式,数值允许带小数,输入以#结束。如:((2.4+3.5)*2-(2+3.5)/2)*(2+3)#
#include <stdio.h>
#include "G:\express\mystack.h" char piror[7][7]={'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=','f',
'>','>','>','>','f','>','>',
'<','<','<','<','<','f','='};
//保存算符优先矩阵 int chartoint(char ch)//将运算符变成数组(算符优先矩阵)下标
{
switch(ch)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '(':return 4;
case ')':return 5;
case '#':return 6;
}
} int numchar(char ch)//判断是否为数字符号
{
if (ch>='0' && ch<='9')
return 1;
else return 0;
} int isoptr(char ch)//判断是否为合法的运算符号
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':return 1;
default :return 0;
}
} float operate(float n1,char ch,float n2)//对操作数按运算符进行运算
{
switch(ch)
{
case '+':return n1+n2;
case '-':return n1-n2;
case '*':return n1*n2;
case '/':return n1/n2;
}
} main()
{
char str[80];
gets(str);
int i=0;
int number1,exp10;
char ch1,ch2,curroptr1;
float number,number2,n1,n2,n3,curroptr,ch3; sqstack optr,opnd;
initstack(optr);push(optr,'#');
initstack(opnd); while(str[i]!='\0')
{
if(numchar(str[i]))
{
number1=0;
while(numchar(str[i]))
{
number1=number1*10+(str[i]-'0');
i++;
}
//已经读取了一个整数 if (str[i]=='.')
{
i++;
number2=0.0;exp10=10;
while(numchar(str[i]))
{
number2=number2+((str[i]-'0')*1.0)/exp10;
exp10=exp10*10;
i++;
printf("number2_____decimal part=%f\n",number2);
}
number=number1+number2;
printf("number_float=%f\n",number);// 将实数压入堆栈
push(opnd,number);
}
else
{
push(opnd,number1);// 将整数压入堆栈
printf("number_____int=%d\n",number1);
}
}
else if(!isoptr(str[i]))
{
printf("express error---1!\n");
exit(0);
}
else
{
ch1=(char)gettop(optr);
ch2=str[i];
printf("ch1=%c,ch2=%c\n",ch1,ch2); if(piror[chartoint(ch1)][chartoint(ch2)]=='>')
{
while(piror[chartoint(ch1)][chartoint(ch2)]=='>')
{
pop(opnd,n2);
pop(opnd,n1);
pop(optr,curroptr);
curroptr1=(char)curroptr;
printf("\n---%f %c %f=====%f\n",n1,curroptr1,n2,operate(n1,curroptr1,n2));
push(opnd,operate(n1,curroptr1,n2));
ch1=(char)gettop(optr);
}
}
if(piror[chartoint(ch1)][chartoint(ch2)]=='<') push(optr,ch2);
if(piror[chartoint(ch1)][chartoint(ch2)]=='=') pop(optr,ch3);
if(piror[chartoint(ch1)][chartoint(ch2)]=='f')
{
printf("express error---2!\n");
exit(0);
}
i++;
}
}
pop(opnd,n3);
printf("\nresult=%f\n",n3);
}
#include <stdlib.h>
#include <stdio.h> #define stackinitsize 50
#define stackincrement 8 typedef float elemtype; typedef struct{
elemtype *base;
elemtype *top;
int stacksize;
}sqstack; int initstack(sqstack &s)
{s.base=(elemtype * ) malloc(stackinitsize*sizeof(int));
s.top=s.base;
s.stacksize=stackinitsize;
return 1;
} int push(sqstack &s,elemtype e)
{
*(s.top)=e;
s.top++;
return 1;
} elemtype gettop(sqstack s)
{
return *(s.top-1);
} int emptystack(sqstack s)
{if (s.top==s.base) return 1;
else return 0;
} int pop(sqstack &s,elemtype &e)
{ if (emptystack(s)) return 0;
--s.top;
e=*(s.top);
return 1;
}
expresscalculate的更多相关文章
随机推荐
- U3D Trigger事件触发
使用Trigger事件触发,可以达到虽然触发了,可是不改变任何效果. 这个是进入时候触发的: void OnTriggerEnter2D(Collider2D other) { print (othe ...
- jni使用
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 简介 详解 JNI 元素 JNI函数实战 AndroidmkApplicationmk Androidmk Applicat ...
- VSS Admin 清除密码
[参阅链接]http://www.cnblogs.com/Zealot/archive/2004/09/18/44309.html the secret is to hack the um.dat f ...
- reflact中GetMethod方法的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- IOS UIColor 自定义颜色
使用 UIColor定义颜色 和 同 CIColor 与 CGColor 之间的联系.转换 1. 利用UIColor展现 #F6F6F6 这个传统的颜色 #F6F6F6 为一个 16 进制表示的RP ...
- 如何在xcode下面同时安装cocos2d-iphone 和 cocos2d-x模板,其实是因为很喜欢C++的缘故,当时学习的是前者,现在自己摸着石头过河了就(cocos2d-x安装失败 出错)
首先在Xcode下面配置两个模板的开发环境,其实一个开源库,一个C++移植,学习需要也是,我的mac上一直用的是cocos2d-iphone, 今天想试下cocos2d-x,安装的时间发现安装成功(我 ...
- 数据挖掘相关的10个问题[ZZ]
NO.1 Data Mining 和统计分析有什么不同? 硬要去区分Data Mining和Statistics的差异其实是没有太大意义的.一般将之定义为Data Mining技术的CART.CHAI ...
- 《bunzip2命令》-linux命令五分钟系列之八
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- javascript为目标位置div等设置高度
应该是DOM的东西: document.getElementById("目标id").style.height = 多高(数值)+"px";
- .NET 4.0 使用 asyn await
.NET 4.0 也可以使用asyn await 使用nuget 搜索await即可发现微软官方包,安装导项目就可以使用了. 参考:http://stackoverflow.com/questio ...