吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3311|回复: 6
收起左侧

[其他转载] C++写的计算器

[复制链接]
SstudentT 发表于 2015-4-28 14:12
本帖最后由 SstudentT 于 2015-4-28 14:17 编辑

全部是自己想的,还没有看牛逼的例子,所以代码有点长。对于类什么的运用,我觉的其实用函数就可以了啊!
但是没办法,为了习惯C++的类,用的再别扭我也得用!可能用多了就懂了怎么正确运用了!


例如
输入:”100-6*(10-(1+3/(1-7)+2.5)*5)=“
输出:130


由于还在编写过程中,所以无法处理非法输入
例如
“12+-12/13=”                                                                      语义不对
“113/(9+1=”                                                                        括号无法匹配
“12-16”                                                                                少了等号
“15516155346465645000000000000000000 -464=”                 数值越界
以上错误还无法处理。

如果发现逻辑错误(就是输入正确的表达式,但是得出错误答案的。)
麻烦您通知我一下,不胜感激!
(因为我一个人测试总有想不到的地方,谢谢你的帮助!)
下面附上源码


expression.h    这个类是处理不带括号的表达式。


[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include<string>
#include<iostream>
#include<vector>
    using std::vector;
    using std::string;
 
class Expression{
    const string Number="0123456789.";
    const string Operator="+-*/";
    string expression;
    string result;
private:
    string oper;
    vector<double> operand;
    string expre;
    void FAlloper();
    void FAlloperand();
public:
    Expression(string s);
    void OutCome(){std::cout<<expression<<"  "<<result<<std::endl;};
    string Theresult(){return result;}
    string compute();
};
#endif // EXPRESSION_H


expression.cpp   实现Expression类
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include"expression.h"
Expression::Expression(string s):expression(s){}
 
void Expression::FAlloper()
{
   string T=expression;
   auto i=T.find_first_of(Operator);
   while(i!=string::npos)
  {
   oper.append(T,i,1);
   if(T[i+1]=='-')
   T.erase(0,i+2);
   else
   T.erase(0,i+1);
   i=T.find_first_of(Operator);
  }
}
 
void Expression::FAlloperand()
{
     size_t p;
     string T=expression;
     do
     {
         operand.push_back(stod(T,&p));
         if(T[p]=='=')
            break;
         T.erase(0,p+1);
     }while(1);
}
 
string Expression::compute()
{
    FAlloper();
    FAlloperand();
    while(operand.size()!=1){
 auto i=oper.find_first_of("/*");
 if(i==string::npos)
    while(oper.size()!=0)
 {
     if(oper[0]=='+')
     {
        double r=operand[0]+operand[1];
        auto ins=operand.erase(operand.begin(),operand.begin()+2);
        operand.insert(ins,r);
        oper.erase(oper.begin());
     }
     else if(oper[0]=='-')
     {
         double r=operand[0]-operand[1];
         auto ins=operand.erase(operand.begin(),operand.begin()+2);
         operand.insert(ins,r);
         oper.erase(oper.begin());
     }
 }
 else if(oper[i]=='*')
 {
     double r=operand[i]*operand[i+1];
     auto ins=operand.erase(operand.begin()+i,operand.begin()+i+2);
     operand.insert(ins,r);
     oper.erase(i,1);
 }
 else if(oper[i]=='/')
 {
     if(operand[i+1]==0)
     {
         result="Error! Divisor can't be zero!";
         return result;
     }
     double r=operand[i]/operand[i+1];
     auto ins=operand.erase(operand.begin()+i,operand.begin()+i+2);
     operand.insert(ins,r);
     oper.erase(i,1);
 }
    }
 result=std::to_string(operand[0]);
 return result;
}


calculator.h   计算带括号的表达式,里面用到Expression类
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
#include"expression.h"
class Expression;
class Calculator{
private:
    string expree,p,l;
    size_t phr,phl;
    size_t phrInter,phlInter;
    bool Ispure(string s);
    void markShell(string s);
    void markShellInter(string s);
    string Filiter(string s);
    string Calculate();
public:
    Calculator(string s);
    string TheEnd(){return Calculate();}
};


calculator.cpp   Calculator类的实现
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include"calculator.h"
 
Calculator::Calculator(string s)
:expree(s),p(s),l(s)
{
    TheEnd();
}
 
bool Calculator::Ispure(string s)
{
    auto pornot=s.find_first_of("()");
    return (pornot==string::npos)?true:false;
}
 
void Calculator::markShell(string s)
{
    int n=0;
    size_t p=0;
    phl=s.find("(");
    while(true)
    {
        p=s.find_first_of("()",p);
        if(s[p]=='(')
            n++;
        else if(s[p]==')')
            n--;
        if(n==0)
        {
          phr=p;
          break;
        }
        else
            p++;
    }
}
 
string Calculator::Filiter(string s)
{
    if(!Ispure(s))
    {
    const string Identical("=");
    markShell(s);
    string T=s;
    T.assign(s,phl+1,phr-phl-1);
    T=T+Identical;
    s=Filiter(T);
    return s;
    }
    else
    {
    Expression exp(s);
    return exp.compute();
    }
}
 
string Calculator::Calculate()
{
    string s=expree;
   while(!Ispure(s))
   {
       markShellInter(s);
       s.replace(phlInter,phrInter-phlInter+1,Filiter(s));
   }
   Expression exp(s);
   return exp.compute();
}
 
void Calculator::markShellInter(string s)
{
    size_t p=0;
    while(true)
    {
    p=s.find_first_of("()",p);
    if(s[p]=='(')
    {
        phlInter=p;
        p++;
    }
    else if(s[p]==')')
    {
        phrInter=p;
        break;
    }
    }
}


main    函数驱动
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
#include"expression.h"
#include"calculator.h"
int main()
{
   string s;
   std::cin>>s;
   Calculator c0(s);
   std::cout<<c0.TheEnd();
   return 0;
}



发现逻辑错误求告知,不胜感激!谢谢!!!

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| SstudentT 发表于 2015-4-29 13:27
yysniper 发表于 2015-4-29 06:46
咱坛百分之八九十都是易语言,看看C++还是挺好的

同感啊!不是我黑易语言!
真的觉得它因为入门门槛过低,学的人又急于求成,
为了眼前的成果,放弃打基础,几乎没有易语言的大作。
除非自己想明白,不然我觉得就是误入歧途了!
kingone 发表于 2015-4-28 14:22
越峥嵘越从容 发表于 2015-4-28 14:23
大爱吾爱℃ 发表于 2015-4-28 14:42
楼主 辛苦了、、。。。
yysniper 发表于 2015-4-29 06:46
咱坛百分之八九十都是易语言,看看C++还是挺好的
yysniper 发表于 2015-4-29 18:09
SstudentT 发表于 2015-4-29 13:27
同感啊!不是我黑易语言!
真的觉得它因为入门门槛过低,学的人又急于求成,
为了眼前的成果,放弃打基 ...

不能再同意了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-16 13:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表