码迷,mamicode.com
首页 > 其他好文 > 详细

实验3

时间:2019-04-21 17:30:37      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:void   add   har   .sh   use   out   private   for   比较   

graph项目:

graph.h

技术图片
#ifndef GRAPH_H
#define GRAPH_H
class Graph {
    public:
        Graph(char ch,int n);
        void draw();
    private:
        char symbol;
        int size;
}; 
#endif
graph.h

 

graph.cpp

#include "graph.h"
#include <iostream>
using namespace std;
Graph::Graph(char ch,int n):symbol(ch),size(n){
}

void Graph::draw() {
	int i,j;
	for(i=1;i<=size;i++)
	{for(j=1;j<=size-i;j++)
	     cout<<" ";
	 for(j=1;j<=2*i-1;j++)
	     cout<<symbol;
	 cout<<endl;
	}
}

main.cpp

技术图片
#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1(*,5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2($,7);
    graph2.draw();
    
    return 0;
}
main.cpp

 技术图片

 

Fraction项目:

fraction.h

技术图片
#ifndef FRACTION_H
#define FRACTION_H
class Fraction{
    public:
        Fraction(int top1=0,int bottom1=1):top(top1),bottom(bottom1) {
        }
        void add(Fraction a,Fraction b);
        void min(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

fraction.cpp

技术图片
#include "fraction.h"
#include <iostream>
using namespace std;

void Fraction::add(Fraction a,Fraction b) {
    Fraction sum;
    sum.top=a.top*b.bottom+b.top*a.bottom;
    sum.bottom=a.bottom*b.bottom;
    cout<<"加:"<<sum.top<<"/"<<sum.bottom<<endl;
}

void Fraction::min(Fraction a,Fraction b) {
    Fraction min;
    min.top=a.top*b.bottom-b.top*a.bottom;
    min.bottom=a.bottom*b.bottom;
    cout<<"减:"<<min.top<<"/"<<min.bottom<<endl;
}

void Fraction::mul(Fraction a,Fraction b) {
    Fraction mul;
    mul.top=a.top*b.top;
    mul.bottom=a.bottom*b.bottom;
    cout<<"乘:"<<mul.top<<"/"<<mul.bottom<<endl;
}

void Fraction::div(Fraction a,Fraction b) {
    Fraction div;
    div.top=a.top*b.bottom;
    div.bottom=a.bottom*b.top;
    cout<<"除:"<<div.top<<"/"<<div.bottom<<endl;
}

void Fraction::compare(Fraction a,Fraction b) {
    if(a.bottom==b.bottom)
    {if(a.top>b.top)
         cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
     else
         cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    }
    else
    {a.bottom=a.bottom*b.bottom;
     a.top=a.top*b.bottom;
     b.bottom=a.bottom*b.bottom;
     b.top=b.top*a.bottom;
     if(a.top>b.top)
         cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
     else
         cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    }
}

void Fraction::show() {
    cout<<top<<"/"<<bottom<<endl;
}
fraction.cpp

main.cpp

技术图片
#include <iostream>
#include "Fraction.h"
using namespace std;
int main() {
    Fraction a;
    a.show();
    Fraction b(3,4);
    b.show();
    Fraction c(5);
    b.show();
    Fraction example;
    example.add(b,c);
    example.min(b,c);
    example.mul(b,c);
    example.div(b,c);
    example.compare(b,c);
}
main.cpp

技术图片

 

 实验总结与体会:

通过本次实验,我对类的应用更加熟练,但是part1中让小球左右移动还是不知道如何编写,在改变x和y的坐标的过程中遇到瓶颈,看了同学的程序还是不明白如何实现。

part3中将分数最简化也还未做到,我会再尝试一下的。

实验3

标签:void   add   har   .sh   use   out   private   for   比较   

原文地址:https://www.cnblogs.com/mzy-1229/p/10745732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!