实验三:类和对象

Part2

绘制图形

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

Part3

定义并实现分数类Fraction,能够进行加、减、乘、除运算,对两个分数值进行比较运算,返回其大小关系 分数的输入、输出。

#include <iostream>
#include "fraction.h"
using namespace std;
int main(){
    Fraction a;
    cout<<"f1=";
    Fraction f1(3,4);
    cout<<"f2=";
    Fraction f2(5);
    cout<<"f3=";
    Fraction f3(2,0);
    a.bdx(f1,f2);
    cout<<"f1+f2=";
    a.add(f1,f2);
    cout<<"f1-f2=";
    a.minu(f1,f2);
    cout<<"f1*f2=";
    a.mul(f1,f2);
    cout<<"f1/f2=";
    a.div(f1,f2);
    return 0;
}
main.cpp
#include <iostream> 
#include "fraction.h"
using namespace std;

 Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){    
    if(bottom==0) 
       cout<<"Error data,please input again!"<<endl;
    else    
      cout<<top<<"/"<<bottom<<endl;
 }
 void Fraction::add(Fraction x,Fraction y){ 
     int t,r,a,b,gbs,m,n,s1;
       a=x.bottom;
        b=y.bottom;
         {t=x.bottom;
          x.bottom=y.bottom;
          y.bottom=t;
        }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
        }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       s1=(x.top*m+y.top*n);
       cout<<s1<<"/"<<x.bottom<<endl;
     
 }
 
 void Fraction::minu(Fraction x,Fraction y){
     int t,r,a,b,gbs,m,n,s1;
       a=x.bottom;
       b=y.bottom;
       { t=x.bottom;
         x.bottom=y.bottom;
         y.bottom=t;
      }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
       }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       s1=(x.top*m-y.top*n);
       cout<<s1<<"/"<<x.bottom<<endl;
}
 
 void Fraction::bdx(Fraction x,Fraction y){
     int t,r,a,b,gbs,m,n,s1;
        a=x.bottom;
        b=y.bottom;
        {t=x.bottom;
         x.bottom=y.bottom;
         y.bottom=t;
       }
       r=x.bottom%y.bottom;
       while(r!=0) 
        {x.bottom=y.bottom;
         y.bottom=r;
         r=x.bottom%y.bottom;
        }  
       gbs=(a*b)/y.bottom;
       m=gbs/x.bottom;
       n=gbs/y.bottom;
       if(x.top*m>y.top*n)
         cout<<"f1>f2";
       else
         cout<<"f1<f2";
       cout<<endl;
 }
 
 void Fraction::mul(Fraction x,Fraction y){
     int mtop,mbottom;
        mtop=x.top*y.top;
        mbottom= x.bottom*y.bottom;
        cout<<mtop<<"/"<<mbottom<<endl;
 }

void Fraction::div(Fraction x,Fraction y){
     int dtop,dbottom;
        dtop=x.top*y.bottom;
        dbottom= x.bottom*y.top;
        cout<<dtop<<"/"<<dbottom<<endl;
 }
fraction.cpp
#ifndef FRACTION_H
#define FRACTION_H
 
class Fraction{
    public:
        Fraction(int top0=0,int bottom0=1);
        void add(Fraction x,Fraction y);
        void minu(Fraction x,Fraction y);
        void mul(Fraction x,Fraction y);
        void div(Fraction x,Fraction y);
        void bdx(Fraction x,Fraction y);
    private:
        int top;
        int bottom;
 };
fraction.h

 

小结

  • part3中出现了许多重复的代码,不知道怎么简化,才能使得整个程序能够干净利落而非这么复杂冗长,希望大佬不吝赐教,谢谢。
  • 继续努力,多多实践。

转载于:https://www.cnblogs.com/Ann-88/p/10743829.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值