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

运行结果:

       

 总结:

1、part3中有许多代码是重复的,目前还想不到好的解决方案。

2、在做题时,还是会翻阅书,参考样例,不够熟练。

3、学习了用项目。

评论:

1、https://www.cnblogs.com/shenqidetao/p/10742384.html

2、https://www.cnblogs.com/qsxsc/p/10742704.html

3、https://www.cnblogs.com/csc13813017371/p/10743961.html

转载于:https://www.cnblogs.com/jyf13/p/10745858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值