实验4

#include<iostream>
using namespace std;
class Graph{
public:
Graph(char,int);
void draw();
private:
char s;
int z;
};
Graph::Graph(char x,int y)
{
s=x;
z=y;
}
void Graph::draw()
{
char a[z][2*z-1];
int b,c,d,e;
for(b=0;b<z;b++)
{
for(d=0;d<z-b-1;d++)
{
a[b][d]=' ';
}
for(c=z-b-1;c<z+b;c++)
{
a[b][c]=s;
}
for(e=z+b;e<2*z-1;e++)
{
a[b][e]=' ';
}
}
for(b=0;b<z;b++)
{
for(c=0;c<2*z-1;c++)
{
cout<<a[b][c];
}
cout<<endl;
}
}
int main()
{
Graph Graph1('$',7);
Graph1.draw();
Graph Graph2('*',5);
Graph2.draw();
return 0;
}

#include<iostream>
using namespace std;
class Fraction{
public:
Fraction();
Fraction(int t,int b);
Fraction(int c);
void add(Fraction);
void min(Fraction);
void mul(Fraction);
void div(Fraction);
void output();
private:
int top;
int bottom;
};
Fraction::Fraction():top(0),bottom(1){
}
Fraction::Fraction(int t,int b):top(t),bottom(b){
}
Fraction::Fraction(int c):top(c),bottom(1){
}
void Fraction::add(Fraction x)
{
top=top*x.bottom+x.top*bottom;
bottom=bottom*x.bottom;
output();
}
void Fraction::min(Fraction x)
{
top=top*x.bottom-x.top*bottom;
bottom=bottom*x.bottom;
output();
}
void Fraction::mul(Fraction x)
{
top=top*x.top;
bottom=bottom*x.bottom;
output();
}
void Fraction::div(Fraction x)
{
top=top*x.bottom;
bottom=bottom*x.top;
output();

}
void Fraction::output()
{
cout<<top<<'/'<<bottom<<endl;
}
int main()
{
Fraction h;
Fraction g(3,4);
Fraction k(5);
h.output();
g.output();
k.output();

g.add(k);

g.min(k);

g.mul(k);

g.div(k);

return 0;

}

 

posted @ 2018-04-24 13:03  漆黑之光  阅读(82)  评论(0编辑  收藏  举报