实验4

实验结论:

1.draw函数算法:设函数为line,则第i行先输出line-1个空格,再输出i*2-1个字符,再输出line-1个空格。

源代码:

//graph.h部分
#pragma once
class graph{
public:
    graph(char ch, int l);//构造函数
    void draw();    //制图
private:
    char sign;int line;
};
//graph.cpp部分
#include<iostream>
#include"graph.h" //头文件
using namespace std;
graph::graph(char ch, int l) { //构造函数的实现
    sign = ch; line = l;
}
void graph::draw() {           //draw函数的实现
    int i, j;
    for (i = 1; i <= line; i++) {        //第i行
        for (j = 1; j <= line - i;j++)    
            cout << " ";                 //每行先打出line(行数)-1个空格
        for (j = 1; j <= i * 2 - 1; j++)
            cout << sign;                //再打出i*2-1个字符
        for (j = 1; j <= line - 1; j++)
            cout << " ";                 //最后再打出line(行数)-1个空格
        cout << endl;
    }
}
//main.cpp部分
#include<iostream>
#include"graph.h"
using namespace std;
int main() {
    char m; int n;
    while (1) {
        cout << "请输入图像的字符与尺寸:";
        cin >> m >> n;
        graph g1(m, n);
        g1.draw();
        cout << "请问您是否需要重新设置图像,是(y),否(n):"; //扩展部分,支持重新设置字符和尺寸
        char c;
        cin >> c;
        if (c == 'n')break;
        if (c == 'y')continue;
    }
    return 0;
}

运行结果:

2.类Fraction的类图:

源代码:

//Fraction.h部分
#pragma once
class Fraction {
public:
    Fraction();            //构造函数
    Fraction(int t, int b);//构造函数(函数重载)
    Fraction(int t);       //构造函数(函数重载)
    void show();           //show函数
    void add(Fraction &f1);//分数相加
    void sub(Fraction &f1);//分数相减
    void mul(Fraction &f1);//分数相乘
    void div(Fraction &f1);//分数相除
    void compare(Fraction f1, Fraction f2);//分数比较
private:
    int top; int bottom;   //top为分子,bottom为分母
};
//Fraction.cpp部分
#include<iostream>
#include"Fraction.h"
using namespace std;
int sim(int t, int j) {    //sim函数,功能为找出分子与分母的最大公约数(扩展部分)
    int a, b, i, m, c, n;
    a = t; b = j;
    if (a >= b)m = b;
    else m = a;
    for (i = 1; i <= m; i++)
    {
        if (a%i == 0 && b%i == 0)c = i;
    }
    return c;               //返回值为最大公约数c
}
Fraction::Fraction() {      //Fraction函数的实现
    top = 0; bottom = 1;
}
Fraction::Fraction(int t, int b) { //Fraction函数的实现(函数重载)
    top = t; bottom = b;
}
Fraction::Fraction(int t) {        //Fraction函数的实现(函数重载)
    top = t; bottom = 1;
}
 void Fraction::add(Fraction &f1) {   //分数加法add函数的实现
     Fraction f2;
     f2.top = top * f1.bottom + f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
}
 void Fraction::sub(Fraction &f1) {    //分数减法sub函数的实现
     Fraction f2;
     f2.top = top * f1.bottom - f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
 }
 void Fraction::mul(Fraction &f1) {    //分数乘法mul函数的实现
     Fraction f2;
     f2.top =top*f1.top;
     f2.bottom =bottom*f1.bottom;
     f2.show();
 }
 void Fraction::div(Fraction &f1) {    //分数除法div函数的实现
     Fraction f2;
     f2.top =top*f1.bottom;
     f2.bottom = bottom * f1.top;
     f2.show();
 }
 void Fraction::show() {               //show函数的实现
     if (top > 0 && bottom > 0) {
         int c;
         c = sim(top, bottom);
         cout << top / c << "/" << bottom / c <<endl;//分子分母同除以最大公约数得到最简形式(扩展)
     }
     if (top < 0 && bottom>0) {
         int c; c = sim(-top, bottom);
         cout << top / c << "/" << bottom / c << endl;//分子分母同除以最大公约数得到最简形式(扩展)
     }
     if (top > 0 && bottom < 0) {
         int c; c = sim(top, -bottom);
         cout << -top / c << "/" << -bottom / c << endl;//将分母的负号移动至分子上(扩展)
     }
     if (top < 0 && bottom < 0) {
         int c; c = sim(-top, -bottom);
         cout << -top / c << "/" << -bottom / c << endl;//分子分母同为负数时去除负号
     }
     if (top == 0) cout << top << "/" << bottom << endl;
 }
 void Fraction::compare(Fraction f1, Fraction f2) {    //分数比较compare函数的实现
     float a = float(f1.top) / float(f1.bottom); float b = float(f2.top) / float(f2.bottom);
     if (a <= b) { cout << f1.top << "/" << f1.bottom << "<="; f2.show(); cout << endl; }
     if (a > b) { cout << f1.top << "/" << f1.bottom << ">"; f2.show(); cout << endl; }
 }
//main.cpp部分
#include<iostream>
#include"Fraction.h"
using namespace std;
int main() {
    Fraction f1;
    Fraction f2(5);
    Fraction f3(3, 6);
    Fraction f4(-5, 6);
    Fraction f5(3, -4);
    Fraction f6(-5, -6);
    f1.show(); f2.show(); f3.show(); f4.show(); f5.show(); f6.show();//不同形式的分数显示
    f3.sub(f4); f3.add(f4); f5.mul(f6); f5.div(f6);   //分数的加减乘除
    f1.compare(f2, f4);  //分数比较
    return 0;
}

运行结果:

实验总结与体会:

        经过本次实验,我加深了对类与对象的了解,但我感觉这不是最大的收获。我觉得本次实验的最大收获是知道了自己与别人的差距,这一点具体体现在扩展内容上,第一个实验的三个扩展我只会写一个,而有些同学能把三个扩展都写出来,这就看出差距了。而我在第二个实验里分数比较的compare部分写得仍旧有漏洞,而我偏偏还绞尽脑汁都修复不了,只能先放这儿再慢慢思考。总的来说,这次实验让我认识到了自己能力的不足,并鞭策我在学习编程的道路上继续前进。

猜你喜欢

转载自www.cnblogs.com/hero-1/p/8906309.html
今日推荐