实验三

Part2 基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形) 要求如下: 新建一个空项目,添加上述三个文件到项目中。 补足graph.h中类的成员函数draw()的实现,使得在main()中对类的测试能够实现以下效果: 主函数中测 

#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");
    system("cls");

    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=1;i<=size;i++)
    {for(j=1;j<=size-i;j++)
        cout<<" ";
     for(k=1;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,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)

#include <iostream>
#include "fraction.h"
int main(){
    Fraction a;
    Fraction b(3,4);
    Fraction c(3);
    
//    if(a.bottom==0||b.bottom==0||c.bottom==0) 
//        cout<<"error!"<<endl;
 //   else{
    a.plus(b);
    b.minus(c);
    a.multiple(b);
    b.divide(c);
    b.comparew(c);
    //}
    
    
    return 0;
}
main.cpp
#ifndef FRACTION_H
#define FRACTION_H

class Fraction
 { public: 
          Fraction(){top=0;bottom=1;
          }
          Fraction(int a,int b);
          Fraction(int c);
          void plus(Fraction p2);
          void minus(Fraction m2);
          void multiple(Fraction n2);
          void divide(Fraction d2);
          void comparew(Fraction c2);
         // 待设计的接口 
   private: 
            int top; // 分子 
            int bottom; // 分母
};

#endif
fraction.h
#include "fraction.h"
#include <iostream> 
using std::cout;
using std::endl;


Fraction::Fraction(int a,int b):top(a),bottom(b){
}

Fraction::Fraction(int c):top(c),bottom(1){
}

void Fraction::plus(Fraction p2){
    cout<<top<<'/'<<bottom<<" + "<<p2.top<<'/'<<p2.bottom;
    cout<<" = "<<top*p2.bottom+p2.top*bottom<<'/'<<bottom*p2.bottom<<endl;
}

void Fraction::minus(Fraction m2){
    cout<<top<<'/'<<bottom<<" - "<<m2.top<<'/'<<m2.bottom;
    cout<<" = "<<top*m2.bottom-m2.top*bottom<<'/'<<bottom*m2.bottom<<endl;
}

void Fraction::multiple(Fraction n2){
    cout<<top<<'/'<<bottom<<" * "<<n2.top<<'/'<<n2.bottom;
    cout<<" = "<<top*n2.top<<'/'<<bottom*n2.bottom<<endl;
}

void Fraction::divide(Fraction d2){
    cout<<top<<'/'<<bottom<<" / "<<d2.top<<'/'<<d2.bottom;
    cout<<" = "<<top*d2.bottom<<'/'<<bottom*d2.top<<endl;
}

void Fraction::comparew(Fraction c2){
    int i,j;
    i=top*c2.bottom;
    j=c2.top*bottom;
    if(i>j)
           cout<<top<<'/'<<bottom<<" > "<<c2.top<<'/'<<c2.bottom;
    if(i==j)
        cout<<top<<'/'<<bottom<<" = "<<c2.top<<'/'<<c2.bottom;
    if(i<j)
        cout<<top<<'/'<<bottom<<" < "<<c2.top<<'/'<<c2.bottom;
}
fraction.cpp

 

 

总结:通过这次实验,发现对于成员函数的使用不是很清楚。还有这次对于分母为零的情况处理没有实现。

 

互评

http://www.cnblogs.com/Yyaoyyy/

http://www.cnblogs.com/KOKODA/

http://www.cnblogs.com/0112Frank/

 

转载于:https://www.cnblogs.com/xuexinyu/p/10743640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值