实验 4 类和对象-2
类graph的声明
#ifndef graph_h
#define graph_h
class Graph {
public:
Graph(char ch, int n);
void draw();
private:
char symbol;
int size;
};
#endif
类graph的实现
#include "graph.h"
#include <iostream>
using namespace std;
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
void Graph::draw() {
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size+i;j++)
{
if(j<size-i-1)
{
cout<<" ";
}
else cout<<symbol;
}
cout<<endl;
}
}
类graph的测试
#include <iostream>
#include "graph.h"
using namespace std;
int main()
{
Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2
graph1.draw();
graph2.draw();
return 0;
}
运行结果截图为:
fraction.h
#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
#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
#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{
Fraction a(1,2),b(1,3),x;
x.multiply(a,b);
x.show();
x.divide(a,b);
x.show();
a.roft(b);
x.plus(a,b);
x.roaf();
x.show();
x.subtract(a,b);
x.roaf();
x.show();
a.compare(b);
return 0;
}
运行结果截图为:
感受:
这次实验真的好难啊,花的时间是以前实验的好几倍,感觉完全跟不上老师的进度了,感觉已经花了很大的工夫了,感觉自己可能是没希望了,而且凭我现在的脑子是肯定不可能单独做完这个作业的,就算不是选做题,也要跟同学学长讨论好久,而且就算他们讲的头头是道,我也云里雾里,我不知道该怎么办了,可能还要花更多的时间吧,请老师手下留情。。。或者放缓一点进度。。。或者多给点指点吧,自学真的太难了,谢谢老师了。
根本不知道该对实验的内容说点什么了,就是感觉自己不是很适合这种难度的实验。。。