graph项目:
graph.h
#ifndef GRAPH_H#define GRAPH_Hclass Graph { public: Graph(char ch,int n); void draw(); private: char symbol; int size;}; #endif
graph.cpp
#include "graph.h"#includeusing namespace std;Graph::Graph(char ch,int n):symbol(ch),size(n){}void Graph::draw() { int i,j; for(i=1;i<=size;i++) {for(j=1;j<=size-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<
main.cpp
#include#include "graph.h"using namespace std;int main() { Graph graph1('*',5); graph1.draw(); system("pause"); system("cls"); Graph graph2('$',7); graph2.draw(); return 0;}
Fraction项目:
fraction.h
#ifndef FRACTION_H#define FRACTION_Hclass Fraction{ public: Fraction(int top1=0,int bottom1=1):top(top1),bottom(bottom1) { } void add(Fraction a,Fraction b); void min(Fraction a,Fraction b); void mul(Fraction a,Fraction b); void div(Fraction a,Fraction b); void compare(Fraction a,Fraction b); void show(); private: int top; int bottom;};#endif
fraction.cpp
#include "fraction.h"#includeusing namespace std;void Fraction::add(Fraction a,Fraction b) { Fraction sum; sum.top=a.top*b.bottom+b.top*a.bottom; sum.bottom=a.bottom*b.bottom; cout<<"加:"< <<"/"< < b.top) cout<<"两数比较:"< <<"/"< <<">"< <<"/"< < b.top) cout<<"两数比较:"< <<"/"< <<">"< <<"/"< <
main.cpp
#include#include "Fraction.h"using namespace std;int main() { Fraction a; a.show(); Fraction b(3,4); b.show(); Fraction c(5); b.show(); Fraction example; example.add(b,c); example.min(b,c); example.mul(b,c); example.div(b,c); example.compare(b,c);}
实验总结与体会:
通过本次实验,我对类的应用更加熟练,但是part1中让小球左右移动还是不知道如何编写,在改变x和y的坐标的过程中遇到瓶颈,看了同学的程序还是不明白如何实现。
part3中将分数最简化也还未做到,我会再尝试一下的。