实验三:类和对象

Posted ann-88

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验三:类和对象相关的知识,希望对你有一定的参考价值。

Part2

绘制图形

技术图片
#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1(*,5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2($,7);
    graph2.draw();

    system("pause");
    
    return 0; 
} 
main.cpp
技术图片
// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
void Graph::draw() {
    int i,j,k;
    for(i=0;i<size;i++)
    { for(j=0;j<=size-i-1;j++)
       cout<<" ";
      for(k=0;k<2*i+1;k++)
        cout<<symbol;
      cout<<endl;
    }
}
graph.cpp
技术图片
#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();     // 绘制图形 
    private:
        char symbol;
        int size;
};


#endif
graph.h

技术图片

技术图片

Part3

定义并实现分数类Fraction,能够进行加、减、乘、除运算,对两个分数值进行比较运算,返回其大小关系 分数的输入、输出。

技术图片
#include <iostream>
#include "fraction.h"
using namespace std;
int main(){
    Fraction a;
    cout<<"f1=";
    Fraction f1(3,4);
    cout<<"f2=";
    Fraction f2(5);
    cout<<"f3=";
    Fraction f3(2,0);
    a.bdx(f1,f2);
    cout<<"f1+f2=";
    a.add(f1,f2);
    cout<<"f1-f2=";
    a.minu(f1,f2);
    cout<<"f1*f2=";
    a.mul(f1,f2);
    cout<<"f1/f2=";
    a.div(f1,f2);
    return 0;
}
main.cpp
技术图片
#include <iostream> 
#include "fraction.h"
using namespace std;

 Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){    
    if(bottom==0) 
       cout<<"Error data,please input again!"<<endl;
    else    
      cout<<top<<"/"<<bottom<<endl;
 }
 void Fraction::add(Fraction x,Fraction y){ 
     int t,r,a,b,gbs,m,n,s1;
       a=x.bottom;
        b=y.bottom;
         {t=x.bottom;
          x.bottom=y.bottom;
          y.bottom=t;
        }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
        }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       s1=(x.top*m+y.top*n);
       cout<<s1<<"/"<<x.bottom<<endl;
     
 }
 
 void Fraction::minu(Fraction x,Fraction y){
     int t,r,a,b,gbs,m,n,s1;
       a=x.bottom;
       b=y.bottom;
       { t=x.bottom;
         x.bottom=y.bottom;
         y.bottom=t;
      }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
       }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       s1=(x.top*m-y.top*n);
       cout<<s1<<"/"<<x.bottom<<endl;
}
 
 void Fraction::bdx(Fraction x,Fraction y){
     int t,r,a,b,gbs,m,n,s1;
        a=x.bottom;
        b=y.bottom;
        {t=x.bottom;
         x.bottom=y.bottom;
         y.bottom=t;
       }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
        }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       if(x.top*m>y.top*n)
         cout<<"f1>f2";
       else
         cout<<"f1<f2";
       cout<<endl;
 }
 
 void Fraction::mul(Fraction x,Fraction y){
     int mtop,mbottom;
        mtop=x.top*y.top;
        mbottom= x.bottom*y.bottom;
        cout<<mtop<<"/"<<mbottom<<endl;
 }

void Fraction::div(Fraction x,Fraction y){
     int dtop,dbottom;
        dtop=x.top*y.bottom;
        dbottom= x.bottom*y.top;
        cout<<dtop<<"/"<<dbottom<<endl;
 }
fraction.cpp
技术图片
#ifndef FRACTION_H
#define FRACTION_H
 
class Fraction{
    public:
        Fraction(int top0=0,int bottom0=1);
        void add(Fraction x,Fraction y);
        void minu(Fraction x,Fraction y);
        void mul(Fraction x,Fraction y);
        void div(Fraction x,Fraction y);
        void bdx(Fraction x,Fraction y);
    private:
        int top;
        int bottom;
 };
fraction.h

技术图片

 

小结

  • part3中出现了许多重复的代码,不知道怎么简化,才能使得整个程序能够干净利落而非这么复杂冗长,希望大佬不吝赐教,谢谢。
  • 继续努力,多多实践。

以上是关于实验三:类和对象的主要内容,如果未能解决你的问题,请参考以下文章

UML建模技术实验三 类和各种关系

实验五 类和对象-3 zxt

Java实验项目三——平面图形和立体图形抽象类

实验三

实验3 类和对象

实验5 类和对象3