Classes and Objects (2)

#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; 
}
class Graph {
    public:
        Graph( char ch, int n);    // Constructor with parameters 
        void draw();      // Draw graph 
    private :
         char symbol;
         int size;
};
// 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() {
     int i,j;
     for (i= 0 ;i<size;i++ ){
      for (j= 1 ;j<=size-i;j++ )
     cout<<" ";
     for(j=1;j<=2*i-1;j++)
     cout<<symbol;
     cout<<endl;
}
cout<<endl;
}

Fraction

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

int main()
{

    int a,b,c,d;
    cout << " Enter the numerator a and denominator b of the first number: " << endl;
    cin>>a>>b;
    Fraction p4(a,b);
    cout << " Enter the numerator c and denominator d of the second number: " << endl;
    cin>>c>>d;
    Fraction p5(c,d);
    
    cout<<endl;
    p4.compare(p5);
    cout << " Enter the desired calculation method: " << endl;
     char x;
    cin>>x;
    switch(x)
    {
        case '+':
           p4.add(p5);
          break;
        case '-':
          p4.minus (p5);
          break;
        case '*':
          p4.multiply(p5);
          break;    
        case '/':
          p4.eliminate(p5);
          break;
        default:
         cout<<"输错了。"<<endl;
         break;     
     }
    return 0; 
}
// Declaration of class Fraction 
class Fraction{
     public :
        Fraction( int a= 0 , int b= 1 ):top(a),bottom(b){}
         void add(Fraction &p);   // add 
        void minus(Fraction &p);   // subtract 
        void multiply(Fraction &p) ;   // multiply by 
        void eliminate(Fraction &p);   // eliminate 
        void compare (Fraction &p); // compare two fractions 
        void show();
     private :
         int top;
         int bottom;
};
#include<iostream>
#include"Fraction.h"

using namespace std;

int yueshu(int a,int b){ //求最大公约数 
    int temp;
    for(int i=1;i<=a&&i<=b;i++){
        if(a%i==0&&b%i==0){
            temp=i;
        }
    }
    return temp;
}

void Fraction::add(Fraction &p){
    Fraction q;
    q.top=top*p.bottom+bottom*p.top;
    q.bottom=bottom*p.bottom;
    cout<<q.top/yueshu(q.top,q.bottom)<<"/"<<q.bottom/yueshu(q.top,q.bottom)<<endl;    
}    // add scores

void Fraction::minus(Fraction &p){
    Fraction q;
    q.top=top*p.bottom-bottom*p.top;
    q.bottom=bottom*p.bottom;
    cout<<q.top/yueshu(q.top,q.bottom)<<"/"<<q.bottom/yueshu(q.top,q.bottom)<<endl;    
}   // Subtract fractions

void Fraction::multiply(Fraction &p){
    Fraction q;
    q.top=top*p.top;
    q.bottom=bottom*p.bottom;
    cout<<q.top/yueshu(q.top,q.bottom)<<"/"<<q.bottom/yueshu(q.top,q.bottom)<<endl;    
}   // Multiply fractions

void Fraction::eliminate(Fraction &p) {
    Fraction q;
    q.top=top*p.bottom;
    q.bottom=bottom*p.top;
    cout<<q.top/yueshu(q.top,q.bottom)<<"/"<<q.bottom/yueshu(q.top,q.bottom)<<endl;    
}   // divide the fractions

void Fraction::compare(Fraction &p)
{
    if(top*p.bottom>bottom*p.top)
    cout << "The first number is greater than the second number. " << endl; 
     else  if (top*p.bottom<bottom* p.top)
    cout << "The first number is greater than the second number. " << endl;
     else  if (top*p.bottom==bottom* p.top)
    cout << "The two numbers are the same size. " << endl;
}   // Score comparison size

Guess you like

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