实验3

小球的验证实验图片如下

然后了解到了如何让屏幕冻结。

第二个实验添加的代码及照片如下

void Graph::draw() {
    int i;
    int a=size-1,b=size+1;
    for(i=0;i<size;i++)
    {
        int x,y,z;
        for(x=0;x<a;x++)
            cout<<" ";
        for(y=a;y<b-1;y++)
            cout<<symbol;
        for(z=b;z<size*2;z++)
            cout<<" ";
        cout<<endl;
        a--;
        b++;

    }
}

然后在devc++不太支持,在main函数又加了#include<cstdlib>的头文件运行才正常。

第三个实验代码及截图如下

main.cpp

#include<iostream>
#include"Fraction.h"
using namespace std;
int main()
{
    Fraction a,b(5),c(3,4),d,e(0,0);
    a.shuchu();
    b.shuchu();
    c.shuchu();
    d.xjia(a,b);
    d.shuchu();
    d.xjian(b,c);
    d.shuchu();
    d.xcheng(b,c);
    d.shuchu();
    d.xchu(b,c);
    d.shuchu();
    d.xjiao(a,b);
    d.shuchu();
    d.xcheng(b,e);
    d.shuchu();
    return 0;
}

Fraction.h

#ifndef FRACTION
#define FRACTION
class Fraction
{
    public:
        Fraction(int t=0,int b=1);
        void xjia(Fraction &p,Fraction &q);
        void xjian(Fraction &p,Fraction &q);
        void xcheng(Fraction &p,Fraction &q);
        void xchu(Fraction &p,Fraction &q);
        void xjiao(Fraction &p,Fraction &q);
        void shuchu();
    private:
        int top; // 分子
        int bottom; // 分母
};

#endif

Fraction.cpp

#include "Fraction.h" 
#include <iostream>
using namespace std;
Fraction::Fraction(int t,int b):top(t),bottom(b){
}
void Fraction::xjia(Fraction &p,Fraction &q)
{
    bottom=p.bottom*q.bottom;
    top=p.bottom*q.top+q.bottom*p.top;
    if(bottom==0)
    {
        top==0;
        goto hhh;
    }
    int i,j;
    if(top>bottom)
    j=bottom;
    else
    j=top;
    for(i=j;i>=1;i--)
    {
        if(top%i==0&&bottom%i==0)
        {
            top/=i;
            bottom/=i;
            break;
        }
    }
    hhh:;
}
void Fraction::xjian(Fraction &p,Fraction &q)
{
    bottom=p.bottom*q.bottom;
    top=q.bottom*p.top-p.bottom*q.top;
    if(bottom==0)
    {
        top==0;
        goto hhh;
    }
    int i,j;
    if(top>bottom)
    j=bottom;
    else
    j=top;
    for(i=j;i>=1;i--)
    {
        if(top%i==0&&bottom%i==0)
        {
            top/=i;
            bottom/=i;
            break;
        }
    }
    hhh:;
}
void Fraction::xcheng(Fraction &p,Fraction &q)
{
    bottom=p.bottom*q.bottom;
    top=p.top*q.top;
    if(bottom==0)
    {
        top==0;
        goto hhh;
    }
    int i,j;
    if(top>bottom)
    j=bottom;
    else
    j=top;
    for(i=j;i>=1;i--)
    {
        if(top%i==0&&bottom%i==0)
        {
            top/=i;
            bottom/=i;
            break;
        }
    }
    hhh:;
}
void Fraction::xchu(Fraction &p,Fraction &q)
{
    bottom=p.bottom*q.top;
    top=p.top*q.bottom;
    if(bottom==0)
    {
        top==0;
        goto hhh;
    }
    int i,j;
    if(top>bottom)
    j=bottom;
    else
    j=top;
    for(i=j;i>=1;i--)
    {
        if(top%i==0&&bottom%i==0)
        {
            top/=i;
            bottom/=i;
            break;
        }
    }
    hhh:;
}
void Fraction::xjiao(Fraction &p,Fraction &q)
{
    bottom=p.bottom*q.bottom;
    top=q.bottom*p.top-p.bottom*q.top;
    if(bottom==0)
    {
        top==0;
        goto hhh;
    }
    int i;
    i=top/bottom;
    if(i>0)
    {
        top=p.top;
        bottom=p.bottom;
    }
    else
    {
        top=q.top;
        bottom=q.bottom;
    }
    hhh:;
}
void Fraction::shuchu()
{
    cout<<top<<"/"<<bottom<<endl;
}

截图

加减乘除比较重复,然后不知道如何在void里停止函数,就用了以前看到的大佬的goto,不知道有没有问题。

感觉对类的掌握还是不太好,还要再看书,再写。

 

posted on 2019-04-22 17:05  吵闹机器人  阅读(129)  评论(0编辑  收藏  举报