C++四则计算器源码
来源:互联网 发布:淘宝阿里旺旺号那里看 编辑:程序博客网 时间:2024/05/17 08:39
我建议每个像我一样的小白,都自己动手写一下,四则计算器,因为这里面既有队列的知识,又有栈的知识,又有字符串处理的知识,而且还可以对自己的逻辑思维进行一定强度的锻炼,可以说有百利而无一害!我这里写的代码仅供大家参考,因为比较简单我也没怎么写注释。talking is cheapshow me the code
#include <iostream>#include <queue>#include <stack>using namespace std;const int INF=1000;//调度场算法 中缀表达式转后缀表达式template <typename T> queue<T>& dispatch(pair<T,int> infix[],long size){ //var can't use to convert type,but we can use it to define a type; stack<pair<T, int>> operators; queue<T>* result=new queue<T>; for (int i=0; i<size; i++) { if (infix[i].second==0) {//if it is a number; result->push(infix[i].first); }else if(infix[i].second!=INF){//or it is a operator; if(operators.empty()){ operators.push(infix[i]); }else if(infix[i].second>operators.top().second){ operators.push(infix[i]); }else{ do{ result->push(operators.top().first); operators.pop(); } while (!operators.empty()&&infix[i].second<=operators.top().second); operators.push(infix[i]); } } } while(!operators.empty()){ result->push(operators.top().first); operators.pop(); } return *result;}//创建带有优先级的操作符pairpair<string, int>* init_infix_pair(vector<string> infix,long size) { pair<string, int>* opers=new pair<string,int>[size]; int current_priority=1; for (int i=0; i<size; i++) { pair<string,int> oper(infix[i],0); if(infix[i]=="("){current_priority+=2;oper.second=INF;} else if(infix[i]==")"){current_priority-=2;oper.second=INF;} else if(infix[i]=="*"||infix[i]=="/"){ oper.second=current_priority+1; }else if(infix[i]=="+"||infix[i]=="-"){ oper.second=current_priority; } opers[i]=oper; } return opers;}//把字符串转换成字符串数组,以运算符作为分隔符,可以处理小数点vector<string>& convert_string_to_array(string exp){ int start,end; vector<string>* vec=new vector<string>; for (int i=0; i<exp.size(); i++) { if(exp[i]>='0'&&exp[i]<='9'){ if(!isdigit(exp[i-1])&&exp[i-1]!='.'){ start=i; } }else{ if(exp[i]=='.')continue; if(i==0||!isdigit(exp[i-1])){ vec->push_back(exp.substr(i,1)); } else{ end=i; vec->push_back(exp.substr(start,end-start)); vec->push_back(exp.substr(i,1)); } } } return *vec;}//构建一个vector,元素为pair,first标识是数字还是运算符,second为具体内容vector< pair<bool, double> >& convert_queue_to_vector(queue<string> result){ vector<pair<bool,double>>* vec=new vector<pair<bool,double>>; while(!result.empty()){ char oper=*result.front().c_str(); if(oper=='+'||oper=='-'||oper=='*'||oper=='/'){ vec->push_back(pair<bool,double>(true,oper)); result.pop(); continue; } vec->push_back(pair<bool,double>(false,atof(result.front().c_str()))); result.pop(); } return *vec;}//利用栈计算后缀表达式double caculate_string_vector(vector<pair<bool, double>> vec){ stack<double> result_stack; for (int i=0; i<vec.size(); i++) { if(vec[i].first==false){ result_stack.push(vec[i].second); }else{ double TF,TS; double result; TF=result_stack.top(); result_stack.pop(); TS=result_stack.top(); result_stack.pop(); if(vec[i].second=='+') result=TS+TF; if(vec[i].second=='-') result=TS-TF; if(vec[i].second=='*') result=TS*TF; if(vec[i].second=='/') result=TS/TF; result_stack.push(result); } } return result_stack.top();}int main(){ string exp="(123.0+(45.75-20*(10-9)))*56.23+(3.2-100)"; vector<string>& vec=convert_string_to_array(exp); vector<string>::iterator it=vec.begin(); while (it!=vec.end()) { cout<<*it<<" "; it++; } cout<<endl; pair<string,int>* ps=init_infix_pair(vec,vec.size()); queue<string>& result=dispatch(ps,vec.size()); vector< pair<bool,double> >& final=convert_queue_to_vector(result); cout<< caculate_string_vector(final)<<endl; while(!result.empty()){ cout<<result.front()<<" "; result.pop(); } cout<<endl; return 0;}
- C++四则计算器源码
- 四则计算器
- 简单的四则计算器
- C语言:(新)四则计算器(支持括号和次方运算)
- c 语言简单计算器源码
- 计算器源码~控制台C语言
- C语言四则运算器
- 复数计算器(单步,四则)
- 四则混合运算器 计算器 (Android版)
- 设计一个简单的四则计算器
- ruby实现四则计算器(输入优化)
- C++实现计算器(四则混合运算)
- 四则运算计算器 C 语言完美版源码
- C语言版 计算器-词法分析器-源码分析
- 纯C语言 四则计算
- 四则
- 计算器源码
- 计算器源码
- maven 仓库地址收集
- 项目管理实践【六】自动同步数据库【Using Visual Studio with Source Control System to synchronize database automaticall
- Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 问题,写了id语句仍出错的解决办法
- Linux文件系统的隐藏权限:chattr lsattr
- nux-2.6.32在mini2440开发板上移植(22)之qtopia4.4.3手机版移植(english version)
- C++四则计算器源码
- linux进程和线程
- PHP 与文件系统处理
- 如何判断子进程已启动
- 高性能前端~高性能HTML
- 关于android 签名系统的理解
- linux文件truncate命令
- MYSQL-常用SQL
- Estimated and Actual Execution Plans