language: C++ 4.7.2 (gcc-4.7.2)
date: 327 days 23 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    #ifndef FIXED_HPP_
    #define FIXED_HPP_
 
    typedef float value_type ;   
 
    class fixed
    {   
     public:
       fixed();
       fixed(value_type integer, value_type fraction); 
       fixed(double val);
       void as_string();
       value_type integer();
       value_type fraction();
       value_type value() const;
       //~fixed();
       fixed& operator+=(const fixed &other);
 
       static const int places=4;   
       static const int places10=10000;  
 
     private:
       value_type integer_;
       value_type fraction_;   
       value_type value_;
    };  
 
    fixed operator+(fixed a, const fixed &b);
 
    #endif
 
    #include <iostream>
    #include <ostream>
    #include <stdexcept>
    #include <string>
    #include <algorithm>
 
 
    // using namespace std;
 
    fixed::fixed():integer_(0), fraction_(0), value_(0){}
 
 
    fixed::fixed(value_type integer,  value_type                 fraction):integer_(integer),         fraction_(fraction)
           {try
       {   
        if (fraction_ <0)    
           throw std::invalid_argument("Invalid argument. Must be positive.");
       }   
            catch (std::exception& e)
              {
                std::cout <<"\n"<< e.what() << std::endl;
      }
             while (fraction_>= places10)
                {
                 if(int(fraction_)%10 >=5 && fraction_< (places10*10) )
            fraction_=int(fraction_/10+1);
         else
            fraction_ =int(fraction_/10);
        }
 
     value_ = integer_*places10 + fraction_;
           }
 
    fixed::fixed(double val):integer_(int (val)), fraction_( (val- int(val))*places10)
           { if (val <0)
                {    val = val*(-1);
             if ( int(val*places10*10)%10>=5)
              fraction_ = (fraction_*(-1) +1)*(-1);
        }
     else
         {
          if (int(val*places10*10)%10>=5)
              fraction_ = fraction_ +1;
         }
 
     value_ = integer_*places10 + fraction_;
           }
 
    void fixed::as_string()
           {    std::string str;
                std::string str2;
        while( (int(integer_)/10) >=0 and int(integer_)>0 )
             {
             str.push_back(int(integer_)%10 + 48);
             integer_ = integer_/10;
             //std::cout<<str<<std::endl;
             }
        //std::cout<<"String format: "<<str<<std::endl;
        reverse(str.begin(), str.end());
        //std::cout<<"Reversed format: "<<str<<std::endl;
 
        str.push_back('.');
        //std::cout<<"New std::string: "<<str<<std::endl;
        while( (int(fraction_)/10 )>=0 and int(fraction_)>0)
             {
             str2.push_back(int(fraction_)%10 + 48);
             fraction_ = fraction_/10;
             //std::cout<<str<<std::endl;
             }
        //std::cout<<"String format: "<<str<<std::endl;
        reverse(str2.begin(), str2.end());
        str.append(str2);
        std::cout<<"String representation: "<<str<<std::endl;
           }
 
    value_type fixed::value() const
         {
          return   value_;
         }
    value_type fixed::integer()
         {
          return integer_;
         }
    value_type fixed::fraction()
                {
          return fraction_;
         }
    fixed& fixed::operator+=(const fixed &other)
           { value_ += other.value();
             return *this;
           }
 
    fixed operator+(fixed a, const fixed &b) // temporary fix
          { a+=b;
          return a;}
 
int main() {}