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

实验3

时间:2019-04-20 21:14:06      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:规则   ios   draw   程序   com   ==   lap   nbsp   play   

part2 graph

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

展示效果如下

技术图片技术图片

part3  Fraction描述分数

技术图片
 1 #ifndef FRACTION_H
 2 #define FRACTION_H
 3 
 4 class Fraction {
 5 public:
 6     Fraction(int x = 0, int y = 1) : top(x), bottom(y) { if (bottom == 0) exit(0); };
 7     void show();
 8     void simplyFraction();
 9     friend Fraction addf(Fraction &a,Fraction &b);
10     friend Fraction minf(Fraction &a,Fraction &b);
11     friend Fraction mulf(Fraction &a,Fraction &b);
12     friend Fraction divf(Fraction &a,Fraction &b);
13     
14 private:
15     int top;
16     int bottom;
17 };
18 
19 #endif
fraction.h
技术图片
#include <iostream>
#include "fraction.h"
using namespace std;
void Fraction::show()
{
    if (bottom < 0) top = -top;
    if (top == 0)
        cout << 0 << endl;
    else if (bottom == 1)
        cout << top << endl;
    else
        cout << top << "/" << bottom << endl;
}
void Fraction::simplyFraction()
{
    int m=1;
    for (int i = 2;i <= top&&i <= bottom;i++)
    {
        if (top%i == 0 && bottom%i == 0) m = i;
    }
    top = top / m;
    bottom = bottom / m;
}
Fraction addf(Fraction &a, Fraction &b)
{
    Fraction aresult;
    aresult.top = a.top*b.bottom + b.top*a.bottom;
    aresult.bottom = a.bottom*b.bottom;
    aresult.simplyFraction();
    return aresult;
}
Fraction minf(Fraction &a, Fraction &b)
{
    Fraction minresult;
    minresult.top = a.top*b.bottom - b.top*a.bottom;
    minresult.bottom = a.bottom*b.bottom;
    minresult.simplyFraction();
    return minresult;
}
Fraction mulf(Fraction &a, Fraction &b)
{
    Fraction mulresult;
    mulresult.top = a.top*b.top;
    mulresult.bottom = a.bottom*b.bottom;
    mulresult.simplyFraction();
    return mulresult;
}
Fraction divf(Fraction &a, Fraction &b)
{
    Fraction dresult;
    dresult.top = a.top*b.bottom;
    dresult.bottom = a.bottom*b.top;
    dresult.simplyFraction();
    return dresult;
}
fraction.cpp
技术图片
 1 #include<iostream>
 2 #include"fraction.h"
 3 using namespace std;
 4 int main()
 5 {
 6     Fraction a;
 7     a.show();
 8     Fraction b(3, 4);
 9     b.show();
10     Fraction c(5);
11     c.show();
12     addf(a, b).show();
13     minf(a, c).show();
14     mulf(b, c).show();
15     divf(b, c).show();
16     return 0;
17 }
main.cpp

技术图片

总结

1在规则性显示时,要找到规律才能编写程序。

2在没有参数的赋值时,我写成了Fraction a(); 多了个小括号导致报错。

3最近看到this指针,不小心使用了this,却发现报错,类中函数this指针是自带的,似乎并不需要写。

实验3

标签:规则   ios   draw   程序   com   ==   lap   nbsp   play   

原文地址:https://www.cnblogs.com/cwj0505/p/10742493.html

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