The fourth experiment report of c++

one·

1. Introduction to the draw() function algorithm: Using the nesting of for, the outer for is used for line breaks, the first for statement in the inner layer is used to print spaces, and the second for statement is used to print symbols. The number of spaces in each line is the number of lines minus the ordinal number of the line, and the number of symbols in each line is the number of lines multiplied by 2 minus 1.

The following are the source codes in the three files (graph.h, graph.cpp, main.cpp), among which the class definition file and the main function implementation file have not been changed compared with the courseware.

#ifndef GRAPH_H
#define GRAPH_H

// Declaration of class Graph 
class Graph {
     public :
        Graph( char ch, int n);    // Constructor with parameters 
        void draw();      // Draw graph 
    private :
         char symbol;
         int size;
};


#endif
// The implementation of class graph 
 
#include " graph.h "  
#include <iostream>
 using  namespace std;

// Implementation of constructor with parameters 
Graph::Graph( char ch, int n): symbol(ch), size(n) {
}


// implementation of member function draw()
 // Function: draw size line, display the specified graphic style with the character as symbol 
 //        size and symbol are private member data of class Graph 
void Graph::draw() {
     for ( int i = 1 ; i <= size; i++ ) {
         for ( int j = 1 ; j <= size - i; j++ ) {
            cout << " ";
        }
        for (int k = 1; k <= 2*i-1; k++) {
            cout << symbol;
        }
        cout << endl;
    }
}
#include <iostream>
#include "graph.h"
using namespace std;


int main() {
    Graph graph1( ' * ' , 5 ), graph2( ' $ ' , 7 ) ;   // Define Graph class objects graph1, graph2 
    graph1.draw(); // Call the public interface draw() through the object graph1 to draw the graph on the screen 
    graph2.draw(); // Draw the graph on the screen by calling the public interface draw() through the object graph2
    
    return 0; 
}

3. The program compile and run environment is vs2017

 


 

two.

1.

Fraction

-top:int

-bottom:int

+Fraction()

+Fraction(top0:int,bottom:int)

+Fraction(top0:int)

+Fraction(f0:Fraction)

+Add(f0:Fraction):void

+Subtract(f0:Fraction) :void

+Multiply(f0:Fraction) :void

+Divide(f0:Fraction):void

+Compare(f0:Fraction):void

+set_fraction():void

+get_fraction():void

+~Fraction()

 

2.Fracton.h

class Fraction {
public:
    Fraction();
    Fraction(int top0, int bottom0);
    Fraction(int top0);
    Fraction(Fraction&f0);
    void Add(Fraction&f0);
    void Subtract(Fraction&f0);
    void Multiply(Fraction&f0);
    void Divide(Fraction&f0);
    void Compare(Fraction&f0);
    void set_fraction();
    void get_fraction();
    ~Fraction();

private:
    int top;
    int bottom;
};

 

 

 

 Fraction.cpp

#include"Fraction.h"
#include<iostream>
using namespace std;
Fraction::Fraction():top(0),bottom(1) {}
Fraction::Fraction(int top0) : top(top0), bottom(1){}
Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){}
Fraction::Fraction(Fraction&f0):top(f0.top),bottom(f0.bottom){}
Fraction::~Fraction() {}
void::Fraction::set_fraction() {
    cin >> top>> bottom;
}
void::Fraction::get_fraction() {
    cout << top << "/" << bottom << endl;
}
void::Fraction::Add(Fraction&f0) {
    int a=0, b=0;
    b = f0.bottom*bottom;
    a = f0.top*bottom + top * f0.bottom;
    cout << a << "/" << b << endl;
}
void::Fraction::Subtract(Fraction&f0) {
    int a=0, b=0;
    b = f0.bottom*bottom;
    a = top * f0.bottom - f0.top*bottom;
    cout << a << "/" << b << endl;
}
void::Fraction::Multiply(Fraction&f0) {
    int a=0, b=0;
    a = top * f0.top;
    b = f0.bottom;
    cout << a << "/" << b << endl;
}
void::Fraction::Divide(Fraction&f0) {
    int a=0, b=0;
    a = top * f0.bottom;
    b = bottom * f0.top;
    cout << a << "/" << b << endl;
}
void::Fraction::Compare(Fraction&f0) {
    if((top * f0.bottom - f0.top*bottom)>0)
        cout << top << "/" << bottom << ">" << f0.top << "/" << f0.bottom << endl;
    else if((top * f0.bottom - f0.top*bottom)<0)
        cout << top << "/" << bottom << "<" << f0.top << "/" << f0.bottom << endl;
    else
        cout << top << "/" << bottom << "=" << f0.top << "/" << f0.bottom << endl;
}

main.cpp

#include<iostream>
#include"Fraction.h"
using namespace std;
int main() {
    Fraction f1;
    Fraction f2(1,2);
    Fraction f3(5);
    f1.Add(f2);
    f1.Subtract(f2);
    f1.Multiply(f2);
    f1.Divide(f2);
    f2.get_fraction();
    f3.get_fraction();
    f1.Compare(f2);
    f1.set_fraction();
    f1.get_fraction();
    return 0;
}

运行环境为vs2017

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781817&siteId=291194637