1. 程式人生 > >c++實驗3類和對象

c++實驗3類和對象

code 實現 pub -s char display art 圖片 lose

實 驗 3:

part 1:驗證

技術分享圖片

part 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();
    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() {
 for(int i=1;i<=size;i++)
 {for(int
j=0;j<=size-i;j++) cout<<" "; for(int 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

運行結果:

技術分享圖片

part 3:分數的加減乘除

技術分享圖片
#ifndef Fraction_H
#define Fraction_H
// 類Fraction的聲明 
class Fraction {
    public:
        Fraction(int t=0, int b=1):top(t),bottom(b){
        }// 帶有參數的構造函數
        void add(Fraction a, Fraction b);
        void sub(Fraction a, Fraction b);
        void mul(Fraction a, Fraction b);
        void div(Fraction a, Fraction b); 
        void compare(Fraction a, Fraction b);
        void show();
    private:
        int top;
        int bottom;
};

#endif
fraction.h 技術分享圖片
#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{int x,y,m,n;
 Fraction a;
 a.show();
 Fraction b(3,4);
 b.show();
 Fraction c(5);
 c.show();
 cout<<"Input two score: "<<endl;
 cin>>x>>y;
 cin>>m>>n;
 while(y==0||n==0)
   {cout<<"ERROR!Please try again!"<<endl;
    cin>>x>>y;
    cin>>m>>n;
   }
 Fraction d(x,y);
 Fraction e(m,n);
 a.add(d,e);
 a.sub(d,e);
 a.mul(d,e);
 a.div(d,e);
 a.compare(d,e);
 return 0;
}
main.cpp 技術分享圖片
#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show(){
    if (top == 0) cout << 0<<endl;
    else if (bottom == 1) cout << top << endl;
    else cout << top << "/" << bottom << endl;
}
void Fraction::add(Fraction a, Fraction b){
    int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
    if(m<n)
    {t=m;m=n;n=t;}
    r=m%n;
    while(r!=0)
    {m=n;n=r;r=m%n;}
    gbs=a.bottom*b.bottom/n;
    fz=a.top*(gbs/a.bottom)+b.top*(gbs/b.bottom);
    fm=gbs;
    cout<<"add: "<<fz<<"/"<<fm<<endl;    
}
void Fraction::sub(Fraction a, Fraction b){
    int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
    if(m<n)
    {t=m;m=n;n=t;}
    r=m%n;
    while(r!=0)
    {m=n;n=r;r=m%n;}
    gbs=a.bottom*b.bottom/n;
    fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
    fm=gbs;
    cout<<"sub: "<<fz<<"/"<<fm<<endl;    
}
void Fraction::mul(Fraction a, Fraction b){
    int fz,fm;
    fz=a.top*b.top;
    fm=a.bottom*b.bottom;
    cout<<"mul: "<<fz<<"/"<<fm<<endl;
}
void Fraction::div(Fraction a, Fraction b){
    int fz,fm;
    fz=a.top*b.bottom;
    fm=b.top*a.bottom;
    cout<<"div: "<<fz<<"/"<<fm<<endl;
}

void Fraction::compare(Fraction a, Fraction b){
    int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
    if(m<n)
    {t=m;m=n;n=t;}
    r=m%n;
    while(r!=0)
    {m=n;n=r;r=m%n;}
    gbs=a.bottom*b.bottom/n;
    fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
    if(fz<0)
      cout<<"compare: "<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    else if(fz>0)
            cout<<"compare: "<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
         else
            cout<<"compare: "<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}
fraction.cpp

運行結果:

技術分享圖片

c++實驗3類和對象