实验三-类的实现

part1:

 

 

void Graph::draw() {

   int i,a,b,t;

   a=size;

   b=size;

   t=1;

   while(a--)

   {

       for(i=0;i<b-1;i++)

           cout<<' ';

       for(i=0;i<t;i++)

       cout<<symbol;

       for(i=0;i<b-1;i++)

           cout<<' ';

       cout<<endl;

       b=b-1;

       t=t+2;

   }

}

part3:

#ifndef FRACTION_H

#define FRACTION_H

#include<iostream>

using namespace std;

class Fraction

{

public:

     Fraction (int x = 0, int y = 1);

     friend void jia(Fraction a,Fraction b);

     friend void jian(Fraction a, Fraction b);

     friend void cheng(Fraction a, Fraction b);

     friend void chu(Fraction a, Fraction b);

     friend void bijiao(Fraction a, Fraction b);

private:

    int top;

    int bottom;

};

#endif

#include"Fraction.h"

#include<iostream>

Fraction::Fraction(int x, int y) {

    top = x;

    bottom = y;

}

 

#include"Fraction.h"

#include<iostream>

using namespace std;

void jia(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom + b.top*a.bottom;

    n = a.bottom*b.bottom;

    cout << m << '/' << n << endl;

}

void jian(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom - b.top*a.bottom;

    n = a.bottom*b.bottom;

    cout << m << '/' << n << endl;

}

void cheng(Fraction a, Fraction b){

    int m, n;

    m = a.top*b.top;

    n = a.bottom*b.bottom;

    cout << m << '/' << n << endl;

}

void chu(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom;

    n = a.bottom*b.top;

    cout << m << '/' << n << endl;

}

void bijiao(Fraction a, Fraction b) {

    int m;

    m = a.top*b.bottom - b.top*a.bottom;

    if (m > 0)

        cout << a.top << '/' << a.bottom;

    else

        cout << b.top << '/' << b.bottom;

}

int main() {

    Fraction a;

    Fraction b(3, 4);

    Fraction c(5);

    jia(c, b);

    jian(c, b);

    cheng(c, b);

    chu(c, b);

    bijiao(c, b);

    return 0;

}

实验反思:

1、小球的函数设计十分精妙,要仔细再多看几次。

2、类的定义及实现时要注意各种小细节,这次第三部分就因为一个分号的缺少导致了卡壳,还是掌握的不够熟练。

3、Fraction中要考虑函数的友元用法,刚开始没有想到,怎么也无法实现。就只在fraction.cpp中在类下定义了函数,最后在室友及同学的帮助下想到了友元函数。

猜你喜欢

转载自www.cnblogs.com/yfwg/p/10739568.html
今日推荐