opp实验二

任务一

问题一、

 不能,显示没有声明,原因可能是friend函数并没有声明功能

问题二、

1     T(int x = 0, int y = 0);   // 普通构造函数:初始化,带默认值 
2     T(const T &t);  // 复制构造函数:调用引用类型,减少资源浪费 
3     T(T &&t);       // 移动构造函数:直接把T的资源转移给该对象 
4     ~T();           // 析构函数:释放空间

问题三、

不可以编译成功

任务二、

Complex.hpp

#pragma once

#include <string>

class Complex
{
private:
double real,imag;
public:
static std::string doc;
Complex(double r=0,double i=0);
Complex(const Complex& c);
double get_real();
double get_imag();
Complex add(const Complex& c);
friend Complex add(const Complex& c1,const Complex &c2);
friend bool is_equal(const Complex&c1,const Complex&c2);
friend bool is_not_equal(const Complex&c1,const Complex&c2);
friend void output(const Complex&c1);
friend double abs(const Complex&c);
};

Complex add(const Complex& c1,const Complex &c2);
bool is_equal(const Complex&c1,const Complex&c2);
bool is_not_equal(const Complex&c1,const Complex&c2);
void output(const Complex&c1);
double abs(const Complex&c);

 

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <math.h>
 5 
 6 using namespace std;
 7 
 8 std::string Complex::doc={"a simple class"};
 9 
10 Complex::Complex(double r,double i):real(r),imag(i){}
11 Complex::Complex(const Complex& c):real(c.real),imag(c.imag){};
12 
13 Complex add(const Complex& c1,const Complex &c2)
14 {
15     Complex c3;
16     c3.real=c1.real+c2.real;
17     c3.imag=c1.imag+c2.imag;
18     return c3;
19 }
20 
21 double Complex::get_real()
22 {
23     return real;
24 }
25 double Complex::get_imag()
26     {
27     return imag;
28     }
29 Complex Complex::add(const Complex& c)
30     {
31         Complex c2;
32         c2.real=real+c.real;
33         c2.imag=imag+c.imag;
34         return c2;
35     }
36 
37 bool is_equal(const Complex&c1,const Complex&c2)
38     {
39         if(c1.real==c2.real)
40          if(c1.imag==c2.imag)return true;
41          
42         return false; 
43     }
44 bool is_not_equal(const Complex&c1,const Complex&c2)
45     {
46         if(is_equal(c1,c2))return false;
47         
48         return true;
49     }
50 void output(const Complex&c1)
51     {
52         cout<<c1.real<<"+"<<c1.imag<<"i"<<endl;
53     }
54     
55 double abs(const Complex&c)
56     {
57         return sqrt(c.real*c.real+c.imag*c.imag);
58     }

 任务三、

 

 

 1 #include <iostream>
 2 #include <complex>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 using std::complex;
 8 
 9 void test() {
10     cout << "标准库函数Complex: " << endl;
11     complex<double> c1;//可以指定数据类型
12     complex<double> c2(3, -4);
13     const complex<double> c3(3.5);
14     complex<double> c4(c3);
15 
16     cout << "c1 = " << c1 << endl;
17     cout << "c2 = " << c2 << endl;
18     cout << "c3 = " << c3 << endl;
19     cout << "c4 = " << c4 << endl;
20     cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;//用函数调用,不用
21     cout << endl;
22 
23     cout << "复数运算测试: " << endl;
24     cout << "abs(c2) = " << abs(c2) << endl;
25     c1 += c2;
26     cout << "c1 += c2, c1 = " << c1 << endl;
27     cout << boolalpha;
28     cout << "c1 == c2 : " << (c1 == c2) << endl;//运算符重载
29     cout << "c1 != c3 : " << (c1 != c3) << endl;
30     c4 = c2 + c3;
31     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
32 }
33 
34 int main() {
35     test();
36 }

任务四、

  1 #include <iostream>
  2 #include<algorithm>
  3 #include<string>
  4 
  5 class Fraction
  6 {
  7 private:
  8     int up;
  9     int down;
 10 public:
 11     Fraction(int upp=0,int downn=1);
 12     Fraction(const Fraction& Ff);
 13     static std::string doc;
 14     
 15     int get_up();
 16     int get_down();
 17     Fraction negative();
 18     
 19     friend void output(const Fraction& a);
 20     friend Fraction add(const Fraction& a,const Fraction& b);
 21     friend Fraction sub(const Fraction& a,const Fraction& b);
 22     friend Fraction mul(const Fraction& a,const Fraction& b);
 23     friend Fraction div(const Fraction& a,const Fraction& b);
 24     friend int gcd(const int x,const int y);
 25     
 26 };
 27 int gcd(int m,int n)
 28 {
 29     while(n!=0)
 30     {
 31         int temp=m%n;
 32         m=n;
 33         n=temp;
 34     }
 35     return m;
 36 }
 37 
 38 
 39 std::string Fraction::doc={"a simple Fraction"};
 40 Fraction::Fraction(int upp,int downn){    
 41     if(downn!=0)
 42      {
 43       if(upp!=0)
 44       {
 45     int num =gcd(upp,downn);
 46      upp/=num;
 47      downn/=num;
 48      up=upp;
 49      down=downn;
 50      }
 51       else
 52      {
 53         up=0;
 54         down=downn;
 55       }
 56       }
 57     else{
 58         std::cout<<"down Can't be given 0!";
 59     }
 60 }
 61 Fraction::Fraction(const Fraction& Ff):up(Ff.up),down(Ff.down){}
 62 
 63 
 64 int Fraction::get_up()
 65 {
 66     return up;
 67 }
 68 int Fraction::get_down()
 69 {
 70     return down;
 71 }
 72 Fraction Fraction::negative()
 73 {
 74     Fraction a;
 75     a.up=up;
 76     a.down=-down;
 77     return a;
 78 }
 79 
 80 void output(const Fraction& a)
 81 {
 82     if(a.down==1 || a.up==0)
 83     {
 84         std::cout<<a.up<<std::endl;
 85     }
 86     else
 87     {
 88     std::cout<<a.up<<"/"<<a.down<<std::endl;
 89     }
 90 }
 91 Fraction add(const Fraction& a,const Fraction& b)
 92 {
 93     Fraction c;
 94     Fraction x(a);
 95     Fraction y(b);
 96     int G_B_num =(x.down*y.down)/gcd(x.down,y.down);
 97     x.up=x.up*(G_B_num/x.down);
 98     y.up=y.up*(G_B_num/y.down);
 99     
100     c.up=x.up+y.up;
101     
102     G_B_num=gcd(c.up,c.down);
103     c.up/=G_B_num;
104     c.down/=G_B_num;
105     
106     return c;
107 }
108 Fraction sub(const Fraction& a,const Fraction& b)
109 {
110     Fraction c;
111 //这是一个同分母程序 
112     Fraction x(a);
113     Fraction y(b);
114     int G_B_num =(x.down*y.down)/gcd(x.down,y.down);
115     x.up=x.up*(G_B_num/x.down);
116     y.up=y.up*(G_B_num/y.down);
117 //这是计算过程 
118     c.up=x.up-y.up;
119 //约分
120      G_B_num=gcd(c.up,c.down);
121      c.up/=G_B_num;
122      c.down/=G_B_num;
123      
124     return c;
125 }
126 Fraction mul(const Fraction& a,const Fraction& b)
127 {
128     Fraction c;
129     Fraction x(a);
130     Fraction y(b);
131     int G_B_num;
132 
133     c.up=x.up*y.up;
134     c.down=x.down*y.down;
135     
136     G_B_num=gcd(c.up,c.down);
137     c.up/=G_B_num;
138     c.down/=G_B_num;
139     
140     return c;
141     
142 }
143 Fraction div(const Fraction& a,const Fraction& b)
144 {
145     Fraction c;
146     Fraction x(a);
147     Fraction y(b);
148     int G_B_num;
149 
150     c.up=x.up*y.down;
151     c.down=x.down*y.up;
152     
153     G_B_num=gcd(c.up,c.down);
154     c.up/=G_B_num;
155     c.down/=G_B_num;
156     
157     return c;
158 }
159 
160 using std::cout;
161 using std::endl;
162 
163 void test1() {
164     cout << "FractionÀà²âÊÔ: " << endl;
165     cout << Fraction::doc << endl << endl;
166 
167     Fraction f1(5);
168     Fraction f2(3, -4), f3(-18, 12);
169     Fraction f4(f3);
170     cout << "f1 = "; output(f1); cout << endl;
171     cout << "f2 = "; output(f2); cout << endl;
172     cout << "f3 = "; output(f3); cout << endl;
173     cout << "f4 = "; output(f4); cout << endl;
174 
175     Fraction f5(f4.negative());
176     cout << "f5 = "; output(f5); cout << endl;
177     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
178 
179     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
180     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
181     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
182     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
183     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
184 }
185 
186 void test2() {
187     Fraction f6(42, 55), f7(0, 3);
188     cout << "f6 = "; output(f6); cout << endl;
189     cout << "f7 = "; output(f7); cout << endl;
190     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
191 }
192 
193 int main() {
194     cout << "²âÊÔ1: FractionÀà»ù´¡¹¦ÄܲâÊÔ\n";
195     test1();
196 
197     cout << "\n²âÊÔ2: ·ÖĸΪ0²âÊÔ: \n";
198     test2();
199 }

 

就是编码有点问题……

 

posted @ 2024-10-23 09:34  Mthe  阅读(10)  评论(0编辑  收藏  举报