Calculator 2
新增:
计算类(拥有计算功能)
采用符号优先级计算方法
对符号不匹配的如 -2 ,自动补为 0-2
计算内核信息
输入 -a 可以显示等式与结果
输入 -p 可以显示计算逐步过程
传入 -a 参数 且 传入 -p 参数

传入 -a 参数 且 不传入 -p 参数

规定并非万无一失,但暂未发现有悖原意的bug。
如有发现bug,但求一知,万分感谢。
//
// main.cpp
// Calculator
//
// Created by admin on 16/2/25.
// Copyright © 2016年 YooRarely. All rights reserved.
//
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <sstream>
using namespace std;
class Scan{
private:
bool err; // 输入是否出错
public:
// 字符是否为运算符
bool isOper(char c){
if ( c == '+' || c == '-' || c == '*' || c == '/' ) return true;
return false;
}
// 字符是否为数字
bool isCount(char c){
if (c>='0' && c<='9') return true;
return false;
}
queue <string> ToStringQueue(string input){
err=false;
queue <string > a;
int sum_kuohao = 0;
// 当输入未出错时遍历输入字符串
for (int i = 0; i < input.size() && ! err; i ++){
string s;
// 如果为 运算符 或 括号
if ( isOper( input[i] ) || input[i] == '(' || input[i] == ')' ) {
s = input[i];
if (!a.empty()){
string k=a.back();
if (input[i] == '(' && ( isCount( k[0] ) || k[0] == ')' ) ) err = true;
if ( isOper( k[0] ) && input[i] != '(' && input[i] != '-' ) err = true;
}
if (input[i] == '(') sum_kuohao ++;
if (input[i] == ')') sum_kuohao --;
if ( sum_kuohao < 0 ) err = true;
}// 否则 如果为数字
else if ( isCount( input[i] ) ){
bool dian=false;
while ( ! err && i < input.size() ){
if ( isCount( input[i] ) || input[i] == '.') s=s+input[i];
if ( input[i] == '.' ) {
if ( dian ) err = true;
dian = true;
}
if ( isOper( input[i+1] ) || input[i+1] == '(' || input[i+1] == ')') break;
i++;
}
if (dian && s[ s.size() - 1 ] == '.') err = true;
if ( s.size() > 10 ) err = true;
}
// 入队
a.push(s);
s="";
}
if ( sum_kuohao ) err = true;
if ( ! err && ! a.empty() ){
string k = a.front();
if ( isOper( k[0] ) && k[0] != '-' ) err = true;
k = a.back();
if ( isOper( k[0] ) ) err = true;
}
if ( err ) {
while ( ! a.empty() ) a.pop();
a.push("Error Input !");
}
return a;
}
};
class Print{
public:
void PrintStringQueue(queue<string> a){
while ( ! a.empty() ) {
cout << a.front() << endl;
a.pop();
}
}
};
class Calculate{
private:
bool print;
int tried (stack <char> &oper,stack <int> &important,stack <double> &count, int k){
while ( ! important.empty() && k <= important.top()){
double amazing = count.top();
count.pop();
switch (oper.top()) {
case '+':
if (print) cout<<count.top()<<"+"<<amazing;
count.top() += amazing;
if (print) cout<<"="<<count.top()<<endl;
break;
case '-':
if (print) cout<<count.top()<<"-"<<amazing;
count.top() -= amazing;
if (print) cout<<"="<<count.top()<<endl;
break;
case '*':
if (print) cout<<count.top()<<"*"<<amazing;
count.top() *= amazing;
if (print) cout<<"="<<count.top()<<endl;
break;
case '/':
if (amazing == 0) { return -1;}
if (print) cout<<count.top()<<"/"<<amazing;
count.top() /= amazing;
if (print) cout<<"="<<count.top()<<endl;
break;
}
oper.pop();
important.pop();
}
return 0;
}
public:
// 字符是否为运算符
bool isOper(char c){
if ( c == '+' || c == '-' || c == '*' || c == '/' ) return true;
return false;
}
// 字符是否为数字
bool isCount(char c){
if (c>='0' && c<='9') return true;
return false;
}
double getAns(queue<string> a,bool p){
stack <char> oper;
stack <int> important;
stack <double> count;
int k=0;
print=p;
while ( ! a.empty() ){
if (a.front()[0] == '(') k += 2;
if (a.front()[0] == ')') k -= 2;
if (isOper( a.front()[0] )){
if ( oper.size() == count.size() ){
count.push(0);
important.push(10000);
}
else if ( a.front()[0] == '*' || a.front()[0] == '/'){
if ( tried(oper, important, count, k+1) == -1 ) return 0.000000;
important.push(k+1);
}
else{
if ( tried(oper, important, count, k) == -1 ) return 0.000000;
important.push(k);
}
oper.push( a.front()[0] );
}
if (isCount( a.front()[0] )){
count.push( atof( a.front().c_str() ) );
}
a.pop();
}
if ( ! oper.empty() )
if ( tried(oper, important, count, -1) == -1 ) return 0.000000;
return count.top();
}
};
int main(int argc, const char * argv[]) {
string input;
bool p,a;
double ans;
int i;
Scan scan;
Print print;
Calculate Calculate;
p=false;
a=false;
i=1;
// getline(cin,input);
if (argc<2) return 0;
input=argv[i];
while (1){
if (input=="-a") {
input=argv[++i];
a=true;
continue;
}
if (input=="-p"){
input=argv[++i];
p=true;
continue;
}
break;
}
// print.PrintStringQueue( scan.ToStringQueue(input) );
ans=Calculate.getAns( scan.ToStringQueue(input) , p);
if (a) cout<<input<<"=";
cout<<ans<<endl;
return 0;
}
Calculator 2的更多相关文章
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Windows Universal 应用 – Tip Calculator
声明 以下内容取材于 Bob Tabor 的课程<Windows Phone 8.1 Development for Absolute Beginners>,链接地址为:http://ww ...
- Calculator(1.5)
Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...
- Calculator(1.0)
Calculator(1.0) Github链接 解题过程中遇到的困难 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序. 不会使用<queue>,在网上查 ...
- 数据结构与算法(1)支线任务2——Basic Calculator
题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...
- calculator
#include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...
- Basic Calculator
本博客介绍leetcode上的一道不难,但是比较经典的算法题. 题目如下: Implement a basic calculator to evaluate a simple expression s ...
- Matrix Calculator
表达式分析+矩阵+计算器+寄存器=矩阵计算器 怎么想起来搞这个呢.. //刚看龙书兴致勃勃要搞表达式分析 这个寄存器比较简陋,26字母+4缓存,//字母不分大小写 当然,不只能算矩阵,还能算数= = ...
随机推荐
- c语言指针的指针
c语言在函数传递时常常使用如下的形式. void get(int **p) 对于这个形式,我想过为什么不能够使用 *p 作为形参呢.下面我们看一下代码和执行结果 void get(int **p) { ...
- C、C++混合调用
在项目中,C和C++代码相互调用是很常见的,但在调用时,究竟应该如何编写代码和头文件,有一些讲究,不然就可能出现编译时链接不通过的问题,典型的编译错误日志是: undefined reference ...
- POJ3259_Wormholes_KEY
题目传送门 题目大意:有F组数据,N表示有N点,M表示有M条边,走一遍边需要花费Ti个时间,还有W个虫洞,可以向前回溯Ti时间,求能否从1点出发,经过一些路或虫洞回到1点后时间为负. 建图后用SPFA ...
- cakephp中find('list')的使用
运用一.快速实现下拉菜单 控制器中,使用find('list')返回的是键值对的数组,键名是array的第一个参数id,键值就是第二个参数content. public function list_s ...
- SQLite FTS3/FTS4与一些使用心得
此文已由作者王攀授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 简介 对于今天的移动.桌面客户端应用而言,离线全文检索的需求已经十分强烈,我们日常使用的邮件客户端.云音乐.云 ...
- Visual Studio 2015 Test Explorer does not show anything
Problem After install Visual Studio 2015 community and NUnit Test Adapter, I cannot find test cases ...
- 「日常训练」 Genghis Khan the Conqueror(HDU-4126)
题意 给定\(n\)个点和\(m\)条无向边(\(n\le 3000\)),需要将这\(n\)个点连通.但是有\(Q\)次(\(Q\le 10^4\))等概率的破坏,每次破坏会把\(m\)条边中的某条 ...
- hdu1050Moving Tables(贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Selenium(Python)页面对象+数据驱动测试框架
整个工程的目录结构: 常用方法类: class SeleniumMethod(object): # 封装Selenium常用方法 def __init__(self, driver): self.dr ...
- selenium自动追踪微信小程序审核方案
小程序随着腾讯的不断推广,变的越来越普及,同时更新迭代的速度也越来越快,种类越来越多,那么在如何保证时效性就显得尤为重要,其中很重要一个环节就在于小程序审核通过之后,能否立刻通知到相关技术人员进行发布 ...