实验四

声明

#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};

2.cpp

#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() {
for(int i=0;i<size;i++)
{
for(int j=1;j<size-i;j++)
{
cout<<' ';
}
for(int k=1;k<2*(i+1);k++)
{
cout<<symbol;
}
cout<<endl;
}
// 补足代码,实现「实验4.pdf」文档中展示的图形样式
}

3.mian

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


int main() {
    Graph graph1('*',), graph2('$',) ;  // 定义Graph类对象graph1, graph2 
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    return ; 
}
实验四

FRACTION

1.mian

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


int main() {
    Fraction a;
    Fraction b(,);
    Fraction c();
    Fraction d;
    d.plus(b,c);
    d.output();
    d.minus(b,c);
    d.output();
    d.multiply(b,c);
    d.output();
    d.divide(b,c);
    d.output();    
    d.compare(b,c);    
    return ; 
}

2.cpp

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


Fraction::Fraction(): top(), bottom(){
}
Fraction::Fraction(int a): top(a),bottom(){
}
Fraction::Fraction(int a, int b): top(a),bottom(b){
}
void Fraction::output(){cout<<top<<'/'<<bottom<<endl;
}
void Fraction::plus(Fraction &p,Fraction &q){
    top=p.top*q.bottom+p.bottom*q.top;
    bottom=p.bottom*q.bottom;
}
void Fraction::minus(Fraction &p,Fraction &q){
    top=p.top*q.bottom-p.bottom*q.top;
    bottom=p.bottom*q.bottom;
}
void Fraction::multiply(Fraction &p,Fraction &q){
    top=p.top*q.top;
    bottom=p.bottom*q.bottom;
}
void Fraction::divide(Fraction &p,Fraction &q){
    top=p.top*q.bottom;
    bottom=p.bottom*q.top;
}
void Fraction::compare(Fraction &p,Fraction &q){
    if(p.top/p.bottom>q.top/q.bottom)
    cout<<"a>b"<<endl;
    else if (p.top/p.bottom<q.top/q.bottom)
    cout<<"a<b"<<endl;
    else if (p.top/p.bottom==q.top/q.bottom)
    cout<<"a=b"<<endl;
}

3.声明

class Fraction {
    public:
        Fraction();
        Fraction(int a);
        Fraction(int a, int b);   
        void plus(Fraction &p,Fraction &q);
        void minus(Fraction &p,Fraction &q);
        void multiply(Fraction &p,Fraction &q);
        void divide(Fraction &p,Fraction &q);     
        void compare(Fraction &p,Fraction &q);
        void output();
    private:
        int top;
        int bottom;
};

相关推荐