Practical exercise four

Question 1: graph

Below is a screenshot of the topic:


将draw函数补充如下:
~~~
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - i - 1; j++)
cout << " ";
for (int j = 0; j < 2 * i + 1; j++)
cout << symbol;
for (int j = 0; j < size - i - 1; j++)
cout << " ";
cout << endl;
}
~~~

The following is an optional topic 1: Support to reset the displayed characters and sizes, and automatically redraw the graphics after each reset.

file graph.h

#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();    // 绘制图形 
        void input();   // 录入字符和尺寸
    private:
        char symbol;
        int size;
};
#endif

file graph.cpp

// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
//       size和symbol是类Graph的私有成员数据 
void Graph::draw() {
    // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - i - 1; j++)
            cout << " ";
        for (int j = 0; j < 2 * i + 1; j++)
            cout << symbol;
        for (int j = 0; j < size - i - 1; j++)
            cout << " ";
        cout << endl;
    }       
    }
    
void Graph::input()
{
    cin >> symbol >> size;
    draw();         //自动绘图
}

file main.cpp

#include <iostream>
#include "graph.h"
using namespace std;


int main() {
    Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    while (true)
    {
        cout << "请输入指定的字符及尺寸,中间以空格隔开:";
        graph1.input();
    }

    return 0; 
} 

The screenshot of the result is as follows:

Question 2: fraction

Below is a screenshot of the topic:

fraction.h file

#pragma once
#ifndef FRACTION_H
#define FRACTION_H
class fraction
{
public:
    fraction(int t0,int b0);
    fraction(int t0);
    fraction();
    ~fraction();
    void add(fraction &f1);         //加
    void subtract(fraction &f1);    //减
    void multiply(fraction &f1);    //乘
    void divide(fraction &f1);      //除
    void compare(fraction &f1); //比较大小
    void input();
    void output();

private:
    int top;
    int bottom;
};
#endif // !FRACTION_H

fraction.cpp file

#include "fraction.h"
#include <iostream>
using namespace std;

fraction::fraction(int t0,int b0):top(t0),bottom(b0)
{}

fraction::fraction(int t0):top(t0)
{
    bottom = 1;
}
fraction::fraction()
{
    top = 0;
    bottom = 1;
}

fraction::~fraction()
{}

void fraction::add(fraction &f1)            //加
{
    cout << top * f1.bottom + f1.top * bottom
        << "/" << bottom * f1.bottom << endl;
}

void fraction::subtract(fraction &f1)       //减
{
    cout << top * f1.bottom - f1.top * bottom
        << "/" << bottom * f1.bottom << endl;
}

void fraction::multiply(fraction &f1)       //乘
{
    cout << top * f1.top << "/"
        << bottom * f1.bottom << endl;
}

void fraction::divide(fraction &f1)     //除
{
    cout << top * f1.bottom << "/"
        << bottom * f1.top << endl;
}

void fraction::compare(fraction &f1)        //比较大小
{
    if (top * f1.bottom > bottom * f1.top)
        cout << top << "/" << bottom << endl;
    else if (top * f1.bottom < bottom * f1.top)
        cout << f1.top << "/" << f1.bottom << endl;
    else if (top * f1.bottom == bottom * f1.top)
        cout << "一样大" << endl;
}

void fraction::input()
{
    cin >> top >> bottom;
}
void fraction::output()
{
    cout << top << "/" << bottom << endl;
}

main.cpp file

// Fraction.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction a;
    fraction b(3,4);
    fraction c(5);

    cout << "函数输出测试" << endl;
    cout << "分数a为:";    a.output();
    cout << "分数b为:";    b.output();
    cout << "分数c为:";    c.output();
    
    cout << "加减乘除比较测试" << endl;
    cout << "a + b = ";     a.add(b);
    cout << "a - b = ";     a.subtract(b);
    cout << "b * c = ";     b.multiply(c);
    cout << "b / c = ";     b.divide(c);
    cout << " a和b中较大的是:";    a.compare(b);
    cout << "c和c比较:";       c.compare(c);

    cout << "请输入分数a(分子和分母中间以空格分隔):";
    a.input();
    cout << "a的大小为:";       a.output();

    return 0;
}

The screenshot of the running result is as follows:

I haven't done the optional task of fraction, but there are some ideas

1. Normalization processing: When performing addition, subtraction, multiplication and division operations, the method of outputting one line of cout<<numerator<<denominator is not used, and the numerator and denominator are temporarily stored in temporary variables a, b. Then use the if pair to judge, if b<0; then a becomes -a
2. Fraction simplification: divide the numerator and denominator by their greatest common factor at the same time
3. Convert the fraction to decimal: create a new double type variable with the value divided by the numerator with denominator

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734300&siteId=291194637