分数四则运算器

时间:2020-11-26 20:41:34
好的,先弄出个类来,代码如下:
分数四则运算器分数四则运算器View Code
     
     
     
1 class fraction
2 {
3   private :
4 int numerator; // 分子
5 int denominator; // 分母
6 public :
7 fraction( int x = 0 , int y = 1 );
8 ~ fraction();
9
10 const fraction add( const fraction w);
11 const fraction add( int k); // " + "
12
13 const fraction sub( const fraction w); // " - "
14 const fraction sub( int k);
15
16 const fraction mul( const fraction w); // " * "
17 const fraction mul( int k);
18
19 const fraction div( const fraction w); // " / "
20 const fraction div( int k);
21
22 int commonden( void );
23 void show( void );
24
25 // 增加的成员函数
26 void setn( int x); // 设定分子
27 void setd( int y); // 设定分母
28 int getn( void ) const ;
29 int getd( void ) const ;
30 };

各成员实现如下:
分数四则运算器分数四则运算器View Code
     
     
     
1 // 构造函数
2 fraction::fraction( int x, int y)
3 {
4   numerator = x;
5    if (y == 0 )
6   {
7     cout << " 分母为0 " << endl;
8     exit( 0 );
9   }
10   denominator = y;
11 }
12
13 fraction:: ~ fraction()
14 {
15    // 虚构函数
16 }
17 // 加法
18 const fraction fraction::add( const fraction w)
19 {
20 fraction z;
21 z.numerator = numerator * w.denominator + w.numerator * denominator;
22 z.denominator = denominator * w.numerator;
23 return z;
24 }
25
26 // 成员函数的重载
27 // 分数 + 整数
28 const fraction fraction::add( int k)
29 {
30 fraction z;
31 z.numerator = numerator + k * denominator;
32 z.denominator = denominator;
33 return z;
34 }
35
36 // 减法
37 const fraction fraction::sub( const fraction w)
38 {
39 fraction z;
40 z.numerator = numerator * w.denominator - w.numerator * denominator;
41 z.denominator = denominator * w.numerator;
42 return z;
43 }
44
45 // 成员函数的重载
46 // 分数 - 整数
47 const fraction fraction::sub( int k)
48 {
49 fraction z;
50 z.numerator = numerator - k * denominator;
51 z.denominator = denominator;
52 return z;
53 }
54
55 // 乘法
56 const fraction fraction::mul( const fraction w)
57 {
58 fraction z;
59 z.numerator = numerator * w.numerator;
60 z.denominator = denominator * w.denominator;
61 return z;
62 }
63
64 // 成员函数的重载
65 // 分数 * 整数
66 const fraction fraction::mul( int k)
67 {
68 fraction z;
69 z.numerator = k * numerator;
70 z.denominator = denominator;
71 return z;
72 }
73
74 // 除法
75 const fraction fraction::div( const fraction w)
76 {
77 fraction z;
78 if (numerator == 0 || w.numerator == 0 )
79 {
80 cout << " 两个分子为零的分数相除没有意义 " << endl;
81 exit( 0 );
82 }
83 z.numerator = numerator * w.denominator;
84 z.denominator = denominator * w.numerator;
85 return z;
86 }
87
88 // 成员函数的重载
89 // 分数 / 整数
90 const fraction fraction::div( int k)
91 {
92   fraction z;
93 if (numerator == 0 || k == 0 )
94 {
95 cout << " 两个分子为零的分数相除没有意义 " << endl;
96 exit( 0 );
97 }
98 z.numerator = numerator;
99 z.denominator = k * denominator;
100 return z;
101 }
102
103 // 函数功能:使输出的分数为真分数
104 int fraction::commonden( void )
105 {
106 int m,n,p;
107 m = denominator;
108 n = numerator;
109 do
110 {
111 p = m % n;
112 m = n;
113 n = p;
114 } while (p != 0 );
115 return m;
116 }
117
118 void fraction::show( void )
119 {
120 if (numerator == 0 )
121 {
122 cout << " = 0 " << endl;
123 }
124 else
125 {
126 if (numerator == denominator)
127 {
128 cout << " = 1 " << endl;
129 }
130 else
131 {
132 cout << " = " << numerator / commonden() << " / " << denominator / commonden() << endl;
133 }
134 }
135 }
136
137 void fraction::setn( int x) // 设定分子
138 {
139 numerator = x;
140 }
141 void fraction::setd( int y) // 设定分母
142 {
143 denominator = y;
144 }
145 int fraction::getn( void ) const
146 {
147 return numerator;
148 }
149 int fraction::getd( void ) const
150 {
151 return denominator;
152 }
好了,写个主函数来测试下
分数四则运算器分数四则运算器View Code
     
     
     
void main()
{
  fraction w(
6 , 7 ),t( 9 , 5 ),d;

cout
<< " (6/7) + (9/5) " ;
d
= w.add(t);
d.show();
cout
<< endl;

cout
<< " (6/7) + 1 " ; // 6 13
d = w.add( 1 ); // - + 1 = -
d.show(); // 7 7
cout << endl;

cout
<< " (6/7) - (9/5) " ;
d
= w.sub(t);
d.show();
cout
<< endl;

cout
<< " (6/7) - 1 " ;
d
= w.sub( 1 );
d.show();
cout
<< endl;

cout
<< " (6/7) * (9/5) " ;
d
= w.mul(t);
d.show();
cout
<< endl;

cout
<< " (6/7) * 2 " ;
d
= w.mul( 2 );
d.show();
cout
<< endl;

cout
<< " (6/7) / (9/5) " ;
d
= w.div(t);
d.show();
cout
<< endl;

cout
<< " (6/7) / 2 " ;
d
= w.div( 2 );
d.show();
cout
<< endl;
 
return ;
}

来看看测试结果吧!

分数四则运算器