第三次实验

本文介绍了使用C++实现图形绘制和分数运算的过程。通过创建Graph类,实现了基于字符的图形绘制;并通过Fraction类,实现了分数的加减乘除及比较操作。作者分享了在多文件结构编程和处理负数分数时遇到的挑战。

1.基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形) 要求如下: 新建一个空项目,添加上述三个文件到项目中。 

#ifndef GRAPH_H
#define GRAPH_H
#include<iostream>
using namespace std;
class graph{
public:
    graph(char ch,int n);
    void draw();
private:
    char symbol;
    int size;
};
graph::graph(char ch,int n):symbol(ch),size(n){
}
void graph::draw(){
int i,j,k,m;
for(k=1;k<=size;k++)
{
for(i=k;i<size;i++)
{
    cout<<' ';
}
for(j=0;j<=k-1;j++)
{
    cout<<symbol;
}
for(m=0;m<k-1;m++)
{
    cout<<symbol;
}
cout<<endl;
}
}
#end if
graph.h
#include"graph.h"
#include<iostream>
using namespace std;
int main()
{
    graph graph1('*',5);
    graph1.draw();
    system("pause");
    graph graph2('$',7);
    graph2.draw();
    return 0;
}
main.cpp

 2.基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)

#ifndef FRACTION_H
#define FRACTION_H
#include<iostream>
using namespace std;
class fraction {
public:
    fraction(int a = 0, int b = 1) :top(a), bottom(b) {}
    fraction(fraction &c):top(c.top),bottom(c.bottom){}
    void jia(fraction &d, fraction &e);
    void jian(fraction &d, fraction &e);
    void cheng(fraction &d, fraction &e);
    void chu(fraction &d, fraction &e);
    void compare(fraction &d, fraction &e);
public:
    int top;
    int bottom;
};
void yue(int, int);
void yue(int x, int y)
{
    int p, q,i=0;
    if (x > y)
    {
        i = y;
    }
    else i = x;
    while (x%i != 0 || y%i != 0)
    {
        i--;
    }
    p = x / i;
    q = y / i;
    cout << p << "/" << q << endl;
}
void fraction::jia(fraction &d, fraction &e)
{
    int x, y, k;
    x = d.top*e.bottom + e.top*d.bottom;
    y = d.bottom * e.bottom;
    k = x;
    if (x < 0) {
        x = x - 2 * k;
        cout << "-";
        yue(x, y);
    }
    else if (x == 0)
        cout << '0' << endl;
    else
        yue(x, y);
}
void fraction::jian(fraction &d, fraction &e)
{
    int x, y, k;
    x = d.top*e.bottom - e.top*d.bottom;
    y = d.bottom * e.bottom;
    k = x;
    if (x < 0) {
        x = x - 2 * k;
        cout << "-";
            yue(x, y);
    }
    else if (x == 0)
        cout << '0' << endl;
    else
        yue(x, y);
}
void fraction::cheng(fraction &d, fraction &e) {
    int x, y, k;
    x = d.top*e.top;
    y = d.bottom*e.bottom;
    k = x;
    if (x < 0) {
        x = x - 2 * k;
        cout << "-";
        yue(x, y);
    }
    else if (x == 0)
        cout << '0' << endl;
    else
        yue(x, y);
}
void fraction::chu(fraction &d, fraction &e) {
    int x, y, k;
    x = d.top*e.bottom;
    y = d.bottom*e.top;
    if (y < 0)
    {
        x = -x;
        y = -y;
    }
    k = x;
    if (x < 0) {
        x = x - 2 * k;
        cout << "-";
        yue(x, y);
    }
    else if (x == 0)
        cout << '0' << endl;
    else
        yue(x, y);
}
void fraction::compare(fraction &d, fraction &e) {
    int x, y;
    x = d.top*e.bottom;
    y = e.top*d.bottom;
    if (x > y)
        cout << "第一个数大" << endl;
    else if (x < y)
        cout << "第二个数大" << endl;
    else
        cout << "两个数一样大" << endl;
}
#endif 
fraction.h
#include<iostream>
#include"fraction.h"
using namespace std;
int main() {
    int t1, b1, t2, b2;
    for (;;)
    {
        cout << "请输入第一个分数" << endl;
        cin >> t1 >> b1;
        cout << "请输入第二个分数" << endl;
        cin >> t2 >> b2;
        if (b1 < 0)
        {
            b1 = -b1;
            t1 = -t1;
        }
        if (b2 < 0)
        {
            b2 = -b2;
            t2 = -t2;
        }
        while (b1 == 0 || b2 == 0)
            return 0;
        fraction a(t1, b1);
        fraction b(t2, b2);
        cout << "两数相加为";
        a.jia(a, b);
        cout << "两数相减为";
        a.jian(a, b);
        cout << "两数相乘为";
        a.cheng(a, b);
        cout << "两数相除为";
        a.chu(a, b);
        a.compare(a, b);
        
    }
    return 0;
}
main.cpp

实验总结与体会:

1.这次实验我对类的应用感觉比上次进步了一点,主要是终于知道了怎么用多文件结构,之前一直是#include<xxx>,应该是#include"xxx"。

2.写第二题分数的时候浪费了许多时间,磕磕绊绊写了一个半小时,主要是一开始没考虑负数的问题,还有就是不知道结果应该是怎样的,一直在纠结,最后就提交了这样的结果。

转载于:https://www.cnblogs.com/linjiahao1035/p/10744857.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值