实验三

一、Fraction

 1 ifndef FRACTION_H
 2 #define FRACTION_H
 3 using namespace std;
 4 class Fraction
 5 {
 6     public:
 7         Fraction(int x=0,int y=1){
 8     top=x;
 9     bottom=y;
10 }
11         void myadd(Fraction &a,Fraction &b);
12         void mymin(Fraction &a,Fraction &b);
13         void mymul(Fraction &a,Fraction &b);
14         void mydiv(Fraction &a,Fraction &b);
15         void mycom(Fraction &a,Fraction &b);
16         
17     private:
18         int top; // 分子
19         int bottom; // 分母
20 };
21 #endif
 1 #include<iostream>
 2 #include"fraction.h"
 3 using namespace std;
 4 void Fraction::myadd(Fraction &a,Fraction &b)
 5 {
 6     double m,n,sum;
 7     m= a.top*b.bottom+b.top*a.bottom;
 8     n= a.bottom*b.bottom;sum=m/n;cout<<m<<"/"<<n<<endl;cout<<sum<<endl;
 9 }
10 void Fraction::mymin(Fraction &a,Fraction &b)
11 {
12     double m,n,sum;
13     m=a.top*b.bottom-b.top*a.bottom;
14     n=a.bottom*b.bottom;sum=m/n;cout<<m<<"/"<<n<<endl;cout<<sum<<endl;
15 }
16 void Fraction::mymul(Fraction &a,Fraction &b)
17 {
18     double m,n,sum;
19     m=a.top*b.top;
20     n=a.bottom*b.bottom;sum=m/n;cout<<m<<"/"<<n<<endl;cout<<sum<<endl;
21 }
22 void Fraction::mydiv(Fraction &a,Fraction &b)
23 {
24     double m,n,sum;
25     m=a.top*b.bottom;
26     n=a.bottom*b.top;sum=m/n;
27     cout<<m<<"/"<<n<<endl;cout<<sum<<endl;cout<<sum<<endl;
28 }
29 void Fraction::mycom(Fraction &a,Fraction &b)
30 {
31     int m,n;
32     m=a.top*b.bottom-b.top*a.bottom;
33     n=a.bottom*b.bottom;
34     if(m<0)
35     {
36         cout<<"Max:"<<b.top<<"/"<<b.bottom<<endl;cout<<"Max:"<<a.top<<"/"<<a.bottom<<endl;
37     }
38     if(m==0)
39     {
40         cout<<"一样大"<<endl;
41     }
42     if(m>0)
43     {
44         cout<<"Max:"<<a.top<<"/"<<a.bottom<<endl;cout<<"Max:"<<b.top<<"/"<<b.bottom<<endl;
45     }
46 }
 1 #include<iostream>
 2 #include"fraction.h"
 3 using namespace std;
 4 int main()
 5 {
 6     int t1,b1,t2,b2;    while(cin>>t1>>b1>>t2>>b2)
 7     {    if(b1==0||b2==0)
 8         {cout<<"请重新输入"<<endl;continue;}
 9         else{
10         
11     Fraction a1(t1,b1);
12     Fraction a2(t2,b2);
13     Fraction result;
14 
15 
16     {
17         result.myadd(a1,a2);
18         result.mydiv(a1,a2);
19         result.mymin(a1,a2);
20         result.mymul(a1,a2);
21         result.mycom(a1,a2);
22     }}}
23     return 0;
24     
25     
26 }

二、graph

 1 // 类graph的实现
 2  
 3 #include "graph.h" 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 // 带参数的构造函数的实现 
 8 Graph::Graph(char ch, int n): symbol(ch), size(n) {
 9 }
10 
11 
12 // 成员函数draw()的实现
13 // 功能:绘制size行,显示字符为symbol的指定图形样式 
14 void Graph::draw() {
15     int i,t;
16     for(i=1;i<=size;i++)
17     {
18         for(t=1;t<=size-i;t++)
19         {
20             cout<<" ";
21         }
22         for(t=1;t<=2*i-1;t++)
23         {
24             cout<<symbol;
25         }
26         cout<<endl;
27     }
28 }

 

三、ball

实验总结:

1、最近一直在复习类,感觉这一次熟练度的比以前好很多。

2、定义函数时,使用&引用和不使用&区别好像不大。

3、不知道如何上传视频,只好截图了。

4、Fraction在输出方面感觉还可以有一些改善,但暂时没想到特别好的解决方案。

 

posted @ 2019-04-20 22:16  唱国歌也跑调  阅读(217)  评论(1)    收藏  举报