c++实验3类和对象

 实 验 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

运行结果:

       

猜你喜欢

转载自www.cnblogs.com/jyf13/p/10745858.html
今日推荐