实验三:类和对象

实验三:类和对象

【实验结论】

#1验证性实验:

#2.graph类的实现

#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1('*',5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2('$',7);
    graph2.draw();
    
    return 0; 
} 
#ifndef GRAPH_H
#define GRAPH_H

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


#endif
// 类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,s,m;
    for(i=1;i<=size;i++){
    for(j=1;j<=size-i;j++){
        cout<<ends;
        
    }    
    for(s=1;s<=2*i-1;s++){
        cout<<symbol;
    }

    cout<<endl;
        
    }// 补足代码
    // ...
}

#3.分数类的实现

#include<iostream>
#include"Fraction.h"
using namespace std;
int main(){
    Fraction a;
    a.show();
    Fraction b(3,-4);
    b.show();
    Fraction c(5);
    c.show();
    Fraction e;
    e.add(b,c);
    e.minus(b,c);
    e.multiply(b,c);
    e.division(b,c);
    e.compare(b,c);
}
#ifndef FRACTION_H
#define FRACTION_H
class Fraction {
    public:
        Fraction(int top0=0,int bottom0=1);
        void judge();
        void add(Fraction a,Fraction b);
        void minus(Fraction a,Fraction b);
        void multiply(Fraction a,Fraction b);
        void division(Fraction a,Fraction b);
        void compare(Fraction a,Fraction b);
        void show();
        
    
    private:
        int top;//分子 
        int bottom;//分母 
};
#endif
#include"Fraction.h"
#include<iostream>
using namespace std;
#include<cmath>
Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){
}

//最大公约数 
int gongyueshu(int m,int n)
{  
   int  x,y,i, t;
   x=abs(m);
   y=abs(n);
   for (i=1;i<=x;i++)  
       if ( x%i == 0 && y%i ==0 )    
       t=i;
   return t;
} 

//最小公倍数 
int gongbeishu(int m,int n)
{  
   int  x,y,i, t;
   x=abs(m);
   y=abs(n);
   for (i=1;i<=x;i++)  
       if ( x%i == 0 && y%i ==0 )    
       t=i;
    return m*n/t;
} 

//判断 
void Fraction::judge(){
    if(bottom<0&&top>0){
        bottom=0-bottom;
        top=0-top;
    }
    else if(bottom==0){
        cout<<"This score makes no sense."<<endl;
    }
    else if(bottom<0&&top<0){
        bottom=0-bottom;
        top=0-top;
    }
}
//加法 
void Fraction::add(Fraction a,Fraction b){
        bottom=gongbeishu(a.bottom,b.bottom);
        top=(bottom/a.bottom)*a.top+(bottom/b.bottom)*b.top;
        judge();
        cout<<"两数相加等于:"; 
        cout<<top/gongyueshu(abs(top),abs(bottom))<<"/"<<bottom/gongyueshu(abs(top),abs(bottom))<<endl;
}

//减法
void Fraction::minus(Fraction a,Fraction b){
        bottom=gongbeishu(a.bottom,b.bottom);
        top=(bottom/a.bottom)*a.top-(bottom/b.bottom)*b.top;
        judge();
        cout<<"两数相减等于:";
        cout<<top/gongyueshu(abs(top),abs(bottom))<<"/"<<bottom/gongyueshu(abs(top),abs(bottom))<<endl;
}

//乘法
void Fraction::multiply(Fraction a,Fraction b){
    top=a.top*b.top;
    bottom=a.bottom*b.bottom;
    judge();
    cout<<"两数相乘等于:";
    cout<<top/gongyueshu(abs(top),abs(bottom))<<"/"<<bottom/gongyueshu(abs(top),abs(bottom))<<endl;
    
} 

//除法
void Fraction::division(Fraction a,Fraction b){
    top=a.top*b.bottom;
    bottom=a.bottom*b.top;
    judge();
    cout<<"两数相除等于:";
    cout<<top/gongyueshu(abs(top),abs(bottom))<<"/"<<bottom/gongyueshu(abs(top),abs(bottom))<<endl;
    
} 

//比较
void Fraction::compare(Fraction a,Fraction b){
    double m,n;
    m=a.top/a.bottom;
    n=b.top/b.bottom;
    if(m>n){
        cout<<"较大的数是"<<a.top<<"/"<<a.bottom;
        cout<<"较小的数是"<<b.top<<"/"<<b.bottom;
    }
    else if(m<n){
        cout<<"较大的数是"<<b.top<<"/"<<b.bottom;
        cout<<"较小的数是"<<a.top<<"/"<<a.bottom;
    }
    else if(m==n){
        cout<<"两个数相等"<<endl; 
    }
}

//输出 
void Fraction::show(){
    judge();
    cout<<top<<"/"<<bottom<<endl;
}

将分子改为负数:

将分母改为负数:

【实验总结】

1.对于第一个程序,属于验证型实验,如果说单拿出来让我写我肯定是不会的,所以在自己运行并验证了程序之后,仔细的看了老师写的程序,虽然还是有些不懂的地方,但是有些地方经过老师那天说的,还是懂了一点。

2.第二个graph类的实现,刚开始写的时候想得太过复杂,导致其在输出的时候,有一些错误,无法得到实验结果,然后后来经过了精简的算法,得到了实验结论,有些时候对于问题的抽象化要将自己的条理理清楚,不要自找麻烦。在第二个程序中接触到了新的system("pause")和system("cls"),这是分别暂停和清屏的操作指令。

3.实现分数类的程序,总觉得自己的程序有很多的可以去精简的地方,基本解决了基本的要求,整个程序如果去编译运行的话会发现程序没有那么快的反映出来,这和自己的程序写的比较繁琐有关,刚开始的时候走了一些弯路,比如没有想到将最大公约数和最小公倍数单另的提出来,在自己开始写的时候发现不提出来会很麻烦,所以单独将其写了出来,节省了很多时间。第一次写完后,并没有加入判断分子分母正负的函数,所以说再换了分子分母正负之后,结果是有误的,所以最后加了判断的函数,避免了上述错误。在这个函数中,也复习了去用绝对值。

猜你喜欢

转载自www.cnblogs.com/mxueyyqx/p/10740652.html