实验4

 

11111111

//graph.h
#ifndef GRAPH_H
#define GRAPH_H
class Graph{
private:
char b;
int m,n;
public:
Graph(char bb,int mm){
b=bb;
m=mm;
n=2*mm;
}
void draw();
};
#endif

 

//graph.cpp
#include"graph.h"
#include<iostream>
using namespace std;
void Graph::draw(){
char ch[m][n];
for( int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j>=m-i&&j<=m+i)cout<<b;
else cout<<" ";
}
cout<<endl;
}
cout<<endl;
}

 

//main.cpp
#include"graph.h"
#include<iostream>
using namespace std;
int main(){
char m;int n;
while(cin>>m>>n){
Graph graph1(m,n);
graph1.draw();
}
return 0;
}

 

2222222

 

//fraction.h
#ifndef FRACTION_H
#define FRACTION_H
class Fraction{
public:
Fraction(){m=top=0;n=bottom=1;}
Fraction(int top1):top(top1),bottom(1){deal();m=top;n=1;}
Fraction(int top1,int bottom1):top(top1),bottom(bottom1){deal();m=top,n=bottom;}
int Gettop(){return m;}
int Getbot(){return n;}
void deal();//选做,这个是处理部分每次操作完对分数先正负再化简处理
void add(const Fraction b);//基本的加减乘除运算。
void sub(const Fraction b);
void mul(const Fraction b);
void div(const Fraction b);
void compare(const Fraction b);//比较原输入对象大小
void show();
double showswap();//选作,简单的转化十进制
private:
int top,bottom,m,n;//m,n用于每次输出后对分子分母的重置
};
#endif

 

//fraction.cpp
#include"fraction.h"
#include<cmath>
#include<iostream>
using namespace std;
void Fraction::deal(){
int r,a=fabs(top),b=fabs(bottom);
if(bottom<0){
top=-top;
bottom=-bottom;
}
do{
r=a%b;
a=b;
b=r;
}while(r!=0);
top/=a,bottom/=a;
}
void Fraction::add(const Fraction b){
if(bottom==b.bottom){
top+=b.top;
}
else{
top=top*b.bottom+b.top*bottom;
bottom=bottom*b.bottom;
}
deal();
}
void Fraction::sub(const Fraction b){
if(bottom==b.bottom){
top=top-b.top;
}
else{
top=top*b.bottom-b.top*bottom;
bottom=bottom*b.bottom;
}
deal();
}
void Fraction::mul(const Fraction b){
top*=b.top;
bottom*=b.bottom;
deal();
}
void Fraction::div(const Fraction b){
top=top*b.bottom;
bottom=bottom*b.top;
deal();
}
void Fraction::compare(const Fraction b){
sub(b);
if(top<0)cout<<m<<"/"<<n<<"<"<<b.m<<"/"<<b.n;
else if(top>0)cout<<m<<"/"<<n<<">"<<b.m<<"/"<<b.n;
else cout<<m<<"/"<<n<<"=="<<b.m<<"/"<<b.n;
}
void Fraction::show(){cout<<top<<"/"<<bottom<<endl;top=m,bottom=n;deal();}//这里在输出后重置分子分母。另一个好处只要没输出,就是可以进行加减乘除混算
double Fraction::showswap(){double d=double(top/bottom);return d;}

 

 

//main.cpp
#include"fraction.h"
#include<iostream>
using namespace std;
int main(){
Fraction fra1(3);
Fraction fra2(1,2);
Fraction fra3(3,4);
fra3.add(fra2);
fra3.show();
fra3.mul(fra1);
fra3.show();
fra3.compare(fra2);
return 0;
}

 

 333333333

这两个运行环境windows,我用的,用dev写的。

4444444444

总结:很奇妙,很好玩。

 

posted @ 2018-04-23 18:39  鳄鱼加冕  阅读(126)  评论(0编辑  收藏  举报