实验内容2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 类graph的实现 #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=1;i<=size;i++) { for(int j=1;j<=2*size-1;j++) { if(j > size-i && j < size+i) cout<<symbol; else cout<<' '; } cout<<endl; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2 graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形 return 0; }
运行环境:Dev C++
运行截图:
算法思路:分行输出,计算前导空格个数、字符输出次数,依次输出即可。
实验内容3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "fraction.h" #include <iostream> using namespace std; int gcd(int a,int b) { for(int i=min(a,b);i>=1;--i) if(a%i == 0 && b%i == 0) return i; } //最大公约数 int mcm(int a,int b) { return a*b/gcd(a,b); } //最小公倍数 void Fraction::check() { if(bottem < 0) { bottem *= -1; top *= -1; } } //检查正负 void Fraction::show() { int flag=1; //判断正负:正 1;负 -1。 if(top != 0) { if(top < 0) { top *= -1; flag = -1; } int new_top = top , new_bottem = bottem ; int k=gcd(new_top,new_bottem); new_top /= k; new_bottem /= k; cout<<flag*new_top<<"/"<<new_bottem<<" = "; //分数化简 cout<<flag*(float(new_top))/(float(new_bottem))<<endl; //分数化小数 } else cout<<"0"<<endl; } void Fraction::compare(Fraction &f) { cout<<top<<"/"<<bottem; if((float(top))/(float(bottem)) > (float(f.top))/(float(f.bottem))) cout<<" > "; else if((float(top))/(float(bottem)) < (float(f.top))/(float(f.bottem))) cout<<" < "; else cout<<" == "; cout<<f.top<<"/"<<f.bottem<<endl; } void Fraction::add(Fraction &f) { if(f.top != 0) { if(top != 0) { int x=mcm(bottem,f.bottem); top = top*x/bottem + f.top*x/f.bottem; bottem=x; } else { top = f.top; bottem = f.bottem; } } } void Fraction::multiply(Fraction &f) { top *= f.top; bottem *= f.bottem; } void Fraction::subtract(Fraction &f) { Fraction f0(-1); f.multiply(f0); this->add(f); } void Fraction::divide(Fraction &f) { Fraction f0(f.bottem,f.top); this->multiply(f0); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Fraction { private: int top,bottem; //分子,分母 public: Fraction(int a=0,int b=1) : top(a) , bottem (b) {check();}; Fraction(Fraction &f) : top(f.top) , bottem (f.bottem) {check();}; //复制构造函数 void show(); void compare(Fraction &f); void add(Fraction &f); //加 void subtract(Fraction &f); //减 void multiply(Fraction &f); //乘 void divide(Fraction &f); //除 void check(); };
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include "fraction.h" using namespace std; int main(int argc, char** argv) { Fraction f1; Fraction f2(-3,-12); Fraction f3(5); f1.show(); f2.show(); f3.show(); f1.compare(f2); f2.compare(f3); Fraction f4(f1); f4.subtract(f3); f4.divide(f2); f4.show(); return 0; }
运行环境:Dev C++
运行截图:
Fraction |
- top : int - bottem : int |
+ Fraction( a : int=0 , b : int=0 ) + Fraction( f : Fraction & ) + show() : void + compare( f : Fraction & ) : void + add( f : Fraction & ) : void + subtract( f : Fraction & ) : void + multiply( f : Fraction & ) : void + divide( f : Fraction & ) : void + check() : void |
总结:类与对象将现实中的所有事物全部对应到对象中,使面向对象程序设计变成了可能。并且,还能兼顾到部分数据的安全属性。