c++实验3

1.graph类

#ifndef GRAPH_H
#define GRAPH_H
// 类Graph的声明
class Graph {
public:
    Graph(char ch, int n); // 带有参数的构造函数
    void draw(); // 绘制图形
private:
    char symbol;
    int size;
};
#endif
graph.h
// 类graph的实现
#include "graph.h"
#include <iostream>
using namespace std;
// 带参数的构造函数的实现
Graph::Graph(char ch, int n) : symbol(ch), size(n) {
}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
void Graph::draw() {
    int i, j;
    for (i = 1;i <= size;i++) {
        for (j = 0;j < size - i;j++)
            cout << " ";

        for (j = 1;j < 2 * i;j++)
            cout << symbol;
        cout << endl;
    }

}
graph.cpp
#include <iostream>
#include "graph.h"
#include <cstdlib>
using namespace std;
int main() {
    Graph graph1('*', 5);
    graph1.draw();
    system("pause");
    system("cls");
    Graph graph2('$', 7);
    graph2.draw();
    system("pause");
    return 0;
}
main.cpp

2.分数运算

#ifndef FRACTION_H
#define FRACTION_H

#include<iostream>
class Fraction {
public:
    Fraction(int ntop=0, int nbottom=1);
    void add(Fraction f1, Fraction f2);//f1+f2结果存放在f3
    void minus(Fraction f1, Fraction f2);//f1-f2结果存放在f3
    void mult(Fraction f1, Fraction f2);//f1*f2结果存放在f3
    void divide(Fraction f1, Fraction f2);//f1/f2结果存放在f3
    void simplify();//将f1化简
    void input();//分数的输入
    void output();//输出
    void compare(Fraction f1, Fraction f2);//比较f1,f2的大小并输出
private:
    int top;
    int bottom;
};
#endif
fraction.h
#include<iostream>
#include"fraction.h"
using std::cout;
using std::cin;
using std::endl;
Fraction::Fraction(int ntop, int nbottom):top(ntop),bottom(nbottom)
{
}

void Fraction::add(Fraction f1, Fraction f2)
{
    Fraction f3;
    int top1,top2,bottom1;
    bottom = f1.bottom*f2.bottom;
    top1 = f1.top*f2.bottom;
    top2 = f2.top*f1.bottom;
    f3.top = top1 + top2;
    f3.bottom = bottom;
    f3.simplify();
    top = f3.top;
    bottom = f3.bottom;

}

void Fraction::minus(Fraction f1, Fraction f2)
{
    Fraction f3;
    int top1, top2, bottom1;
    bottom = f1.bottom*f2.bottom;
    top1 = f1.top*f2.bottom;
    top2 = f2.top*f1.bottom;
    f3.top = top1 - top2;
    f3.bottom = bottom;
    f3.simplify();
    top = f3.top;
    bottom = f3.bottom;
}

void Fraction::mult(Fraction f1, Fraction f2)
{
    Fraction f3;
    f3.top = f1.top*f2.top;
    f3.bottom = f1.bottom*f2.bottom;
    f3.simplify();
    top = f3.top;
    bottom = f3.bottom;

}

void Fraction::divide(Fraction f1, Fraction f2)
{
    if (f2.top == 0)
        cout << "error" << endl;
    else {
        Fraction f3;
        f3.top = f1.top*f2.bottom;
        f3.bottom = f1.bottom*f2.top;
        f3.simplify();
        top = f3.top;
        bottom = f3.bottom;
    }
}

void Fraction::simplify()
{
    int i,a=1;
    for (i = 1;i <= top;i++) {
        if (top%i == 0 && bottom%i == 0)
            a = i;
    }
    top /= a;
    bottom /= a;
}


void Fraction::input()
{
    cout << "input top:";
    cin >> top;
    cout << "input bottom:";
    cin >> bottom;
}

void Fraction::output()
{
    cout << top << "/" << bottom << endl;
}

void Fraction::compare(Fraction f1, Fraction f2)
{
    if (f1.top*f2.bottom < f1.bottom*f2.top) {
        f1.output();
        cout << "<"<<endl;
        f2.output();
        cout << endl;
    }
    else if (f1.top*f2.bottom > f1.bottom*f2.top) {
        f1.output();
        cout << ">"<<endl;
        f2.output();
        cout << endl;
    }
    else if (f1.top*f2.bottom == f1.bottom*f2.top) {
        f1.output();
        cout << "="<<endl;
        f2.output();
        cout << endl;
    }
}
fraction.cpp
#include<iostream>
#include"fraction.h"
#include<cstdlib>
using namespace std;
int main() {
    //加法
    Fraction f1, f2, f3;
    cout << "add f1 and f2" << endl;
    cout << "f1:" << endl;
    f1.input();
    cout << "f2:" << endl;
    f2.input();
    f3.add(f1, f2);
    f3.output();
    system("pause");
    system("cls");
    //减法
    cout << "f1 minus f2" << endl;
    cout << "f1:" << endl;
    f1.input();
    cout << "f2:" << endl;
    f2.input();
    f3.minus(f1, f2);
    f3.output();
    system("pause");
    system("cls");
    //乘法
    cout << "f1 multiply f2" << endl;
    cout << "f1:" << endl;
    f1.input();
    cout << "f2:" << endl;
    f2.input();
    f3.mult(f1, f2);
    f3.output();
    system("pause");
    system("cls");
    //除法
    cout << "f1 divide f2" << endl;
    cout << "f1:" << endl;
    f1.input();
    cout << "f2:" << endl;
    f2.input();
    f3.divide(f1, f2);
    f3.output();
    system("pause");
    system("cls");
    //比较大小
    cout << "compare f1 and f2" << endl;
    cout << "f1:" << endl;
    f1.input();
    cout << "f2:" << endl;
    f2.input();
    f3.compare(f1, f2);
    system("pause");
    system("cls");
    
}
main.cpp

 

转载于:https://www.cnblogs.com/Yyaoyyy/p/10751510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值