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

实验四

时间:2018-04-23 23:17:56      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:graph   oat   char   space   .com   turn   .cpp   div   std   

编译环境:vs2017

#pragma once
//Graph.h
class Graph {
public:
    Graph(char s, int l);
    void draw();
private:
    char sign;
    int line;
};
//Graph.cpp
#include"graph.h"
#include<iostream>
using namespace std;
Graph::Graph(char s, int l) {
    sign = s;
    line = l;
}
void Graph::draw() {
    int i, j;
    for (i = 1; i <= line; i++) {
        for (j = 1; j <= line - i; j++)
            cout << " ";
        for (j = 1; j <= i * 2 - 1; j++)
            cout << sign;
        for (j = 1; j <= line - 1; j++)
            cout << " ";
        cout << endl;
    }
}
//main.cpp
#include"graph.h"
#include<iostream>
using namespace std;
int main() {
    Graph graph1(*, 5);
    graph1.draw();
    Graph graph2($, 7);
    graph2.draw();
    return 0;
}

技术分享图片

//Fraction.h部分
#pragma once
class Fraction {
public:
    Fraction();         
    Fraction(int t, int b);
    Fraction(int t);       
    void 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;
//Fraction.cpp
#include<iostream>
#include"Fraction.h"
using namespace std;

Fraction::Fraction() {    
    top = 0; bottom = 1;
}
Fraction::Fraction(int t, int b) { 
    top = t; bottom = b;
}
Fraction::Fraction(int t) {       
    top = t; bottom = 1;
}
void Fraction::add(Fraction &f1) { 
    Fraction f2;
    f2.top = top * f1.bottom + f1.top*bottom;
    f2.bottom = bottom * f1.bottom;
    f2.show();
}
void Fraction::sub(Fraction &f1) { 
    Fraction f2;
    f2.top = top * f1.bottom - f1.top*bottom;
    f2.bottom = bottom * f1.bottom;
    f2.show();
}
void Fraction::mul(Fraction &f1) {    
    Fraction f2;
    f2.top = top * f1.top;
    f2.bottom = bottom * f1.bottom;
    f2.show();
}
void Fraction::div(Fraction &f1) {    
    Fraction f2;
    f2.top = top * f1.bottom;
    f2.bottom = bottom * f1.top;
    f2.show();
}
void Fraction::show() {              

    cout << top << "/" << bottom << endl;
}
void Fraction::compare(Fraction f1, Fraction f2) {    
    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(4, 6);
    Fraction f5(3, 8);
    Fraction f6(6, 7);
    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;
}

技术分享图片

实验四

标签:graph   oat   char   space   .com   turn   .cpp   div   std   

原文地址:https://www.cnblogs.com/nuist20178303037/p/8922493.html

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