C++后缀计算器,大神来啊

fu153451883 2012-07-02 05:24:32
后缀计算器表达式如下
3 4 5 + + =表示4+5+3=
3 4 5 + * =表示(4+5)*3=
可实现+,-,*,/,^基本运算,另外还有单目运算如sin,cos等,还要添加一些常见函数。
写了好几天都没有头绪,不知如何将几种不同类型的联系在一起,求指导,带代码最好。。。。。。。。
...全文
141 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fu153451883 2012-07-05
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
感觉Lz应该不知道后缀表达式的实现原理,建议找本数据结构的书来看下..
具体来说有两个步骤:
1. 中缀表达式转为后缀表达式
2. 解析后缀表达式

这个主要用stack来实现..
[/Quote]
输入得就是后缀表达式,现在这个不管输入什么,结果都是1.
#include<iostream.h>
#include<math.h>
#include"stack.h"
void main()
{
stack s;
char postexp[100];
double d,x,num;
int i=0;
cout<<"****************************************"<<endl;
cout<<"这个最简单的计算器是采用后缀表达式进行计算的."<<endl;
cout<<"要计算5+6+7,那么输入5 6 7 + + = 即可."<<endl;
cout<<"要计算5+6*7,那么输入5 6 7 * + = 即可."<<endl;
cout<<"要计算5*6+7,那么输入5 6 * 7 + = 即可."<<endl;
cout<<"最后这个式子,(3+4)*(5-6/7)+8可以写成"<<"3 4 + 5 6 7 / - * 8 +"<<endl;
cout<<"****************************************"<<endl;
cout<<"Enter expression:"<<endl;
cin>>postexp;
while(postexp[i]!='=')
{
double op1,op2;
switch(postexp[i])
{
case '+':
s.get_top(op1);
s.pop();
s.get_top(op2);
s.pop();
num=op1+op2;
break;
case '-':
s.get_top(op1);
s.pop();
s.get_top(op2);
s.pop();
num=op1-op2;
break;
case '*':
s.get_top(op1);
s.pop();
s.get_top(op2);
s.pop();
num=op1*op2;
break;
case '/':
s.get_top(op1);
s.pop();
s.get_top(op2);
s.pop();
num=op2/op1;
break;
case '^':
s.get_top(op1);
s.pop();
s.get_top(op2);
s.pop();
num=pow(op2,op1);
break;
default:
d=0; /*将数字字符转换成数值存放到d中*/
while (postexp[i]>='0' &&postexp[i]<='9') /*为数字字符*/
{
d=10*d+(postexp[i]-'0');
i++;
}
x=0.1;
if (postexp[i]=='.')
{
i++;
while (postexp[i]>='0' && postexp[i]<='9') /*为数字字符*/
{
d=d+x*(postexp[i]-'0');
x=x*0.1;
i++;
}
}
s.push(d);
}
i++;
}
cout<<num<<endl;
}

某大一菜鸟 2012-07-04
  • 打赏
  • 举报
回复
感觉Lz应该不知道后缀表达式的实现原理,建议找本数据结构的书来看下..
具体来说有两个步骤:
1. 中缀表达式转为后缀表达式
2. 解析后缀表达式

这个主要用stack来实现..
fu153451883 2012-07-04
  • 打赏
  • 举报
回复
我这样写的不对,麻烦看一下
#include<iostream.h>
#include<string.h>
#include<math.h>
#include"stack.h"
void calculator(char *postexp);
int func(int n);
int main()
{
char postexp[100]; /*存储后缀表达式*/
cout<<"输入一个表达式:";
cin>>postexp;
if(strcmp(postexp,"=")==0)
return 0;
calculator(postexp); /*计算表达式的函数*/

}
void calculator(char *postexp)
{
stack s;
double d,x,num;
while(*postexp!='='){
double dou1,dou2;
switch(*postexp){
case '+':
s.get_top(dou1);
s.get_top(dou2);
num=dou1+dou2;
break;
case '-':
s.get_top(dou1);
s.get_top(dou2);
num=dou1-dou2;
break;
case '*':
s.get_top(dou1);
s.get_top(dou2);
num=dou1*dou2;
break;
case '/':
s.get_top(dou1);
s.get_top(dou2);
num=dou2/dou1;
break;
case '^':
s.get_top(dou1);
s.get_top(dou2);
num=pow(dou2,dou1);
break;
default:
d=0; /*将数字字符转换成数值存放到d中*/
while(*postexp>='0'&&*postexp<='9'){ /*为数字字符*/
d=10*d+(*postexp-'0');
postexp++;
}
x=0.1;
if(*postexp=='.') /*处理小数部分*/
{
postexp++;
while(*postexp>='0'&&*postexp<='9')
{
d=d+x*(*postexp-'0');
x=x*0.1;
postexp++;
}
}
s.push(d);
}
postexp++;
}
cout<<"运算结果为:"<<num<<endl;
}
int func(int n)
{
if(n==1||n==0)
return 1;
else
return n*func(n-1);
}
Bird_On_the_way 2012-07-04
  • 打赏
  • 举报
回复
你合工大的??
fu153451883 2012-07-04
  • 打赏
  • 举报
回复
各位朋友指导一下,急用。。。。
cobra_chen 2012-07-02
  • 打赏
  • 举报
回复
程序怎么想的就怎么写。

[Quote=引用 5 楼 的回复:]

虽然看不懂,还是谢谢了,我目前还是菜鸟
[/Quote]
fu153451883 2012-07-02
  • 打赏
  • 举报
回复
虽然看不懂,还是谢谢了,我目前还是菜鸟
cobra_chen 2012-07-02
  • 打赏
  • 举报
回复
修正GetOp,无法截取小数,以及大于占2byte以上的数字

double CountMath(vector<string> vOp)
{
int nMid = vOp.size()/2;

double dLeft = 0.0;
double dRight = 0.0;
double dLast = 0.0;
const char *pOp = NULL;

dLeft = atof(vOp.at(0).c_str());
for (int i=0 ;i<nMid-1 ;i++) //1 等于号不算
{
dRight = atof(vOp.at(i+1).c_str());
pOp = vOp.at(nMid+i).c_str();
if (0 == strcmp(pOp ,"+"))
dLast = dLeft + dRight;
if (0 == strcmp(pOp ,"-"))
dLast = dLeft - dRight;
if (0 == strcmp(pOp ,"*"))
dLast = dLeft * dRight;
if (0 == strcmp(pOp ,"/"))
dLast = dLeft / dRight;

dLeft = dLast; // 结果赋值给左值
}

return dLast;
}

vector<string> GetOp(char *pInput)
{
vector<string> vOp;
const int MAX_NUM_LEN = 32;
char acTmp[MAX_NUM_LEN] = {0};
while (true)
{
memset(acTmp ,0 ,MAX_NUM_LEN);
sscanf(pInput ,"%s" ,acTmp);
vOp.push_back(acTmp);
pInput += strlen(acTmp);
if (0 == *pInput)
break;
pInput += 1; //1 :space
}

return vOp;
}

void main()
{
//char Input[] = "3 4 5 + + =";
char Input[] = "3 4 5 + * =";
vector<string> vOp = GetOp(Input);

printf("Count = %lf\n" ,CountMath(vOp));
}
cobra_chen 2012-07-02
  • 打赏
  • 举报
回复
校验什么的都没写。
楼主自己写吧。

[Quote=引用 2 楼 的回复:]

简单的模型。
楼主自己参考吧。

C/C++ code
double CountMath(vector<string> vOp)
{
int nMid = vOp.size()/2;

double dLeft = 0.0;
double dRight = 0.0;
double dLast = 0.0;
const char *pOp =……
[/Quote]
cobra_chen 2012-07-02
  • 打赏
  • 举报
回复
简单的模型。
楼主自己参考吧。

double CountMath(vector<string> vOp)
{
int nMid = vOp.size()/2;

double dLeft = 0.0;
double dRight = 0.0;
double dLast = 0.0;
const char *pOp = NULL;

dLeft = atof(vOp.at(0).c_str());
for (int i=0 ;i<nMid-1 ;i++) //1 等于号不算
{
dRight = atof(vOp.at(i+1).c_str());
pOp = vOp.at(nMid+i).c_str();
if (0 == strcmp(pOp ,"+"))
dLast = dLeft + dRight;
if (0 == strcmp(pOp ,"-"))
dLast = dLeft - dRight;
if (0 == strcmp(pOp ,"*"))
dLast = dLeft * dRight;
if (0 == strcmp(pOp ,"/"))
dLast = dLeft / dRight;

dLeft = dLast; // 结果赋值给左值
}

return dLast;
}

vector<string> GetOp(char *pInput)
{
vector<string> vOp;
while (true)
{
char acTmp[8] = {0};
int nRet = sscanf(pInput ,"%s" ,acTmp);
vOp.push_back(acTmp);
pInput += nRet;
if (0 == *pInput)
break;
pInput += 1; //1 :space
}

return vOp;
}

void main()
{
//char Input[] = "3 4 5 + + =";
char Input[] = "3 4 5 + * =";
vector<string> vOp = GetOp(Input);

printf("Count = %lf\n" ,CountMath(vOp));
}
fu153451883 2012-07-02
  • 打赏
  • 举报
回复
要运算的是实数

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧