实验3:类和对象

实验目的

   1. 掌握类的设计、定义、实现和测试

   2. 掌握C++程序以项目文件组织的多文件结构编写形式

   3. 深度理解面向对象编程与结构化编程在编程解决实际问题时思维方式的不同
实验准备
  实验前,请围绕以下几点复习/学习指定内容:
    1. C++程序以项目文件方式组织的多文件结构 浏览学习教材「5.6节 多文件结构和编译预处理指令」。
    2. 类和对象
     类的设计、定义(尤其注意构造函数及其初始化列表书写形式) 
     对象的定义、访问
实验内容
Part1 验证性内容:以多文件结构组织的项目文件示例:在画布上可以上下左右移动的小球
        在devc/codeblocks/vs2017等软件环境中新建空项目,然后添加文件canvas.h, canvas.cpp, ball.h, ball.cpp, main.cpp到项目中。(进课程公邮→文件中心→实验文件夹下载实验3源码示例及程序框架压缩包)
        结合运行浏览效果,理解示例中画布类Canvas, 球类Ball的定义、实现和使用,进一步巩固类和对象的相 关知识,同时,深度理解面向对象编程在处理问题时思维方式的不同。
        结合示例,搜索了解以下函数的用法及所在头文件 
        system("pause)
        让系统暂停,按任意键继续。头文件#include<cstdlib> 
        system("color ××")
        设置屏幕前景色、背景色。system()的参数必须是const char*类型。
        string类的成员函数.c_str()
        以char*类型传回string类对象中的字符串
Part2  基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件:
        graph.h (类Graph的声明)
        graph.cpp (类Graph的实现)
        main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形)
要求如下:
        新建一个空项目,添加上述三个文件到项目中。
        补足graph.h中类的成员函数draw()的实现。
Part3 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)
设计并实现接口,实现以下要求:
       分数类Fraction的基本功能列表 
               定义Fraction类对象时,支持如下形式: Fraction a; // 默认没有提供任何初始化数据时,分数对象的默认值为0/1
                                                                               Fraction b(3,4); // 定义分数对象时,如果提供两个初始化参数,代表分数3/4 
                                                                               Fraction b(5); // 定义分数对象时,如果提供两个初始化参数,代表分数5/1
提示:通过编写构造函数实现,合理使用带有默认值的构造函数,同时,注意对分母为0的边界情况进行判断 
               Fraction类对象能够进行如下操作: 
                        加、减、乘、除运算 
                        对两个分数值进行比较运算,返回其大小关系 分数的输入、输出 
提示:通过定义成员函数实现。设计每一个成员函数时,从以下几个方面考虑: 
                成员函数的功能 
                是否需要形参和返回值?如果需要,需要几个参数,参数的类型是什么?返回值类型是什么? 
                充分考虑分母为0的边界情况处理
选做*:类Fraction的扩展功能 
            对分数进行规范化处理,确保分数在形式上,只有分字为负数,并且,分数值是约简形式,即: 
                     2/-3 经过规范化处理后,转换为 -2/3 
                     15/21 经过规范化处理后,转换为5/7 
                     -2/-6 经过规范化处理后,转换为1/3 
       实现分数到小数的转换。例如,3/4会转换为0.75输出。 设计并实现好Fraction类后,在main()中测试时,要测试到各种情形,以确保你所设计和实现的类的各项 功能都正常运行。 
       以项目文件组织的多文件结构方式编写(fraction.h, fraction.cpp,main.cpp)
 
 
Part 2
#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();     // 绘制图形 
    private:
        char symbol;
        int size;
};


#endif
// 类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 blank,number,original=size;
    for(;size>0;size--){
        for(blank=size-1;blank>0;blank--){
            cout<<" ";}
        for(number=1;number<2*(original-size+1);number++){
            cout<<symbol;
        } 
        cout<<endl;
        blank--;
    }
}
#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; 
} 

运行结果:

Part 3

#ifndef GRAPH_H
#define GRAPH_H
class Fraction{
    public:
        Fraction(int t=0,int b=1){top=t;bottom=b;}
        friend void Judgement(Fraction &a,Fraction &b);
        friend void Addition(Fraction &a,Fraction &b);
        friend void Subtraction(Fraction &a,Fraction &b);
        friend void Multiplication(Fraction &a,Fraction &b);
        friend void Division(Fraction &a,Fraction &b);
        friend void Simplification(int &top,int &bottom);
    private:
        int top;
        int bottom;
};
#endif
#include"Fraction.h"
#include<iostream>
using namespace std;
void Judgement(Fraction &a,Fraction &b){
    while(a.bottom==0||b.bottom==0){
        cout<<"Data error,please enter again"<<endl;
        cout<<"Please enter the numerator and denominator of the first fraction"<<endl;
        cin>>a.top>>a.bottom;
        cout<<"Please enter the numerator and denominator of the second fraction"<<endl;
        cin>>b.top>>b.bottom;
    }
}
void Simplification(int top,int bottom){
    int factor1,factor2,remainder1,remainder2;
    if(bottom<0&&top<0){
        bottom=-bottom;
        top=-top;
    }
    else if(bottom<0&&top>0){
        top=0-top;
        bottom=0-bottom;
    }
    if(top>0){
        for(factor1=top;factor1>1;factor1--){
            remainder1=top%factor1;
            if(remainder1==0){
                for(factor2=bottom;factor2>1;factor2--){
                     remainder2=bottom%factor2;
                        if(remainder2==0){
                         if(factor1==factor2){
                             bottom=bottom/factor1;
                             top=top/factor2;
                             break;
                        }
                   }
                }
            }
        }
    }
    if(top<0){
        for(factor1=top;factor1<-1;factor1++){
            remainder1=top%factor1;
            if(remainder1==0){
                for(factor2=bottom;factor2>1;factor2--){
                     remainder2=bottom%factor2;
                        if(remainder2==0){
                         if(-factor1==factor2){
                             bottom=bottom/-factor1;
                             top=top/factor2;
                             break;
                        }
                   }
                }
            }
        }
    }
    if(top==0)
       cout<<"0";
    else if(bottom!=1)
       cout<<top<<"/"<<bottom;
    else
       cout<<top;
}
void Addition(Fraction &a,Fraction &b){
    int top1,bottom1;
    top1=a.top*b.bottom+b.top*a.bottom;
    bottom1=a.bottom*b.bottom;
    cout<<"The sum of the two fractions is:";
    Simplification(top1,bottom1);
    cout<<endl;
}
void Subtraction(Fraction &a,Fraction &b){
    int top1,bottom1;
    top1=a.top*b.bottom-b.top*a.bottom;
    bottom1=a.bottom*b.bottom;
    cout<<"The difference between the two fractions is:";
    Simplification(top1,bottom1);
    cout<<endl;
}
void Multiplication(Fraction &a,Fraction &b){
    int top1,bottom1;
    top1=a.top*b.top;
    bottom1=a.bottom*b.bottom;
    cout<<"The product of two fractions is:";
    Simplification(top1,bottom1);
    cout<<endl;
}
void Division(Fraction &a,Fraction &b){
    int top1,bottom1;
    top1=a.top*b.bottom;
    bottom1=a.bottom*b.top;
    cout<<"The quotient of the two fractions is:";
    Simplification(top1,bottom1);
    cout<<endl;
}
#include"Fraction.h"
#include<iostream>
using namespace std;
int main(){
    int top1,bottom1,top2,bottom2;
    cout<<"Please enter the numerator and denominator of the first fraction:"<<endl;
    cin>>top1>>bottom1;
    cout<<"Please enter the numerator and denominator of the second fraction:"<<endl;
    cin>>top2>>bottom2;
    Fraction a(top1,bottom1);
    Fraction b(top2,bottom2);
    Judgement(a,b);
    Addition(a,b);
    Subtraction(a,b);
    Multiplication(a,b);
    Division(a,b);
    return 0;
}

运行结果:

 

 

posted @ 2019-04-18 17:54  小啵  阅读(332)  评论(0编辑  收藏  举报